前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >4.1 数值积分、高等函数绘制

4.1 数值积分、高等函数绘制

作者头像
周星星9527
发布2018-08-08 15:53:38
5790
发布2018-08-08 15:53:38
举报

[郑重声明],本文大量引用了维基百科内容。

什么是积分?维基百科关于积分的说明:

Integral From Wikipedia, the free encyclopedia Jump to navigationJump to search This article is about the concept of definite integrals in calculus. For the indefinite integral, see antiderivative. For the set of numbers, see integer. For other uses, see Integral (disambiguation). In mathematics, an integral assigns numbers to functions in a way that can describe displacement, area, volume, and other concepts that arise by combining infinitesimal data. Integration is one of the two main operations of calculus, with its inverse operation, differentiation, being the other. Given a function f of a real variable x and an interval [a, b] of the real line, the definite integral

is defined informally as the signed area of the region in the xy-plane that is bounded by the graph of f, the x-axis and the vertical lines x = a and x = b. The area above the x-axis adds to the total and that below the x-axis subtracts from the total. The operation of integration, up to an additive constant, is the inverse of the operation of differentiation. For this reason, the term integral may also refer to the related notion of the antiderivative, a function F whose derivative is the given function f. In this case, it is called an indefinite integral and is written:

The integrals discussed in this article are those termed definite integrals. It is the fundamental theorem of calculus that connects differentiation with the definite integral: if f is a continuous real-valued function defined on a closed interval [a, b], then, once an antiderivative F of f is known, the definite integral of f over that interval is given by

The principles of integration were formulated independently by Isaac Newton and Gottfried Wilhelm Leibniz in the late 17th century, who thought of the integral as an infinite sum of rectangles of infinitesimal width. Bernhard Riemann gave a rigorous mathematical definition of integrals. It is based on a limiting procedure that approximates the area of a curvilinear region by breaking the region into thin vertical slabs. Beginning in the nineteenth century, more sophisticated notions of integrals began to appear, where the type of the function as well as the domain over which the integration is performed has been generalised. A line integral is defined for functions of two or more variables, and the interval of integration [a, b] is replaced by a curve connecting the two endpoints. In a surface integral, the curve is replaced by a piece of a surface in three-dimensional space.

一句话。被积函数在积分区间的积分就是该区间内曲线与x周所围成的面积,如下图所示:

那么问题来了。怎么求解曲线与x轴包围的面积?在把积分区间分成若干段,每一段和曲线近似围城一个矩形,把所有矩形面积求出并相加,就是整个积分了,是不是?当然有一定误差,如下图所示:

毫无疑问,分块越小,精度越高.仔细体验如下两张图(建议到原文查看):

好了,该自己动手实现程序了,我们计算一个函数的积分了,函数y(x)=sqrt(1-x^2),这个函数熟悉吗?其实就是一个圆心位于原点的半径为1的圆,积分区间为0到1,积分是多少?1/4的圆面积,也就是1/4×π×1×1.程序如何实现呢?首先看下被积函数在js中的定义:

代码语言:javascript
复制
1. var Fun=function(x){ //函数
2.  return Math.sqrt(1-x*x);
3. }

好简单吧,积分函数怎么写?函数参数应该有被积函数fun、积分起点start、积分终点end、和积分区间分割的份数nDivided。定义如下把:

代码语言:javascript
复制
1. var calculous=function(fun,start,end,nDivided){
2.  var x,dx=(end-start)/nDivided;
3.  for(varsum=0,i=0;i<nDivided;i++){
4.       x=start+(i+0.5)*dx;
5.       sum+=dx*Fun(x);
6.    }
7.  return sum;
8. }

好像程序写完了。第2行里的dx是各小区间的长度,第3行是循环,用于将各个矩形面积相加、第4行算各个矩形的高度,第5行算矩形面积并相加到sum,第7行返回结果。

好简单,我们给出所有代码:

代码语言:javascript
复制
1. <!DOCTYPEhtml>
2. <htmlstyle="height: 100%">
3.    <head>
4.        <metacharset="utf-8">
5.    </head>
6.    <bodystyle="height: 100%; margin: 0">
7.        <divid="container" style="height: 100%"></div>
8.        <scripttype="text/javascript">
9.  
10. var Fun=function(x){ //函数
11.  return Math.sqrt(1-x*x);
12. }
13.  
14. var calculous=function(fun,start,end,nDivided){
15.  var x,dx=(end-start)/nDivided;
16.  for(varsum=0,i=0;i<nDivided;i++){
17.       x=start+(i+0.5)*dx;
18.       sum+=dx*Fun(x);
19.    }
20.  return sum;
21. }
22.  
23. console.log("分割10次的计算结果:",calculous(Fun,0,1,10));
24. console.log("分割100次的计算结果:",calculous(Fun,0,1,100));
25. console.log("分割200次的计算结果:",calculous(Fun,0,1,200));
26. console.log("分割1000次的计算结果:",calculous(Fun,0,1,1000));
27. console.log("标准答案是:",Math.PI/4);
28.  
29.        </script>
30.    </body>
31. </html>

点击网页运行结果如下:

局部放大看结果:

可见,分区越多,计算越精确。然而,这样计算积分在实际科研中并没什么用,因为效率太低。有兴趣的同学可以查看数值积分进一步了解。

等等,标题里还有高等函数的绘制内容,这里就不介绍了,请参考第3章曲线绘制。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2018-07-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 传输过程数值模拟学习笔记 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档