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

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

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

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中的定义:

1. var Fun=function(x){ //函数
2.  return Math.sqrt(1-x*x);
3. }

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

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行返回结果。

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

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章曲线绘制。

原文发布于微信公众号 - 传输过程数值模拟学习笔记(SongSimStudio)

原文发表时间:2018-07-26

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏PPV课数据科学社区

强大的PyTorch:10分钟让你了解深度学习领域新流行的框架

摘要: 今年一月份开源的PyTorch,因为它强大的功能,它现在已经成为深度学习领域新流行框架,它的强大源于它内部有很多内置的库。本文就着重介绍了其中几种有特色...

38190
来自专栏数据魔术师

论文拾萃 | 基于树表示法的变邻域搜索算法求解考虑后进先出的取派货旅行商问题(附C++代码和详细代码注释)

本文参考期刊论文信息如下: "The Tree Representation for the Pickup and Delivery Traveling Sa...

57440
来自专栏数说工作室

机器学习/深度学习代码速查:6大工具库 &27种神经网络图览

Kailash Ahirwar,Mate Lab 联合创始人,Github的一位资深作者,也是一位活雷锋,近日在其Github个人主页上发表了一个机器学习/深度...

52250
来自专栏数据结构与算法

1775. [国家集训队2010]小Z的袜子

【题目描述】     作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色的袜子中找出一双来穿。终于有一天,小Z再也无法忍受这恼人的找袜子过程,于是他...

36760
来自专栏黑豆梨的曲线机器学习路线

牛顿法(Newton Method)求解f(x)=0

https://en.wikipedia.org/wiki/Newton%27s_method

33590
来自专栏Vamei实验室

绘图: matplotlib核心剖析

matplotlib是基于Python语言的开源项目,旨在为Python提供一个数据绘图包。我将在这篇文章中介绍matplotlib API的核心对象,并介绍如...

23570
来自专栏章鱼的慢慢技术路

Direct3D 11 Tutorial 6:Lighting_Direct3D 11 教程6:灯光

在之前的教程中,世界看起来很无聊,因为所有对象都以相同的方式点亮。 本教程将介绍简单照明的概念及其应用方法。 使用的技术将是朗伯照明。

9120
来自专栏编程心路

算法妙应用-算法的复杂度

对于任何一个程序来说,都可以从三个方面进行分析,分别是 输入、处理、输出,也即 IPO(Input、Process、Output),这种分析方法对硬件和软件程序...

12330
来自专栏数据科学与人工智能

【陆勤践行】最流行的4个机器学习数据集

机器学习算法需要作用于数据,而数据的本质则决定了应用的机器学习算法是否合适,而数据的质量也会决定算法表现的好坏程度。所以会研究数据,会分析数据很重要。本文作为学...

222100
来自专栏人工智能

将图像转换位mnist数据格式

利用mnist数据对数字符号进行识别基本上算是深度学习的Hello World了。在我学习这个“hello world”的过程中,总感觉缺点什么,于是发现无论是...

363100

扫码关注云+社区

领取腾讯云代金券