C++ 中的集成(数学)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2982167/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Integration (math) in C++
提问by Chris Thompson
I'm looking for a library to find the integral of a given set of random data (rather than a function) in C++ (or C, but preferably C++). There is another question asking about integration in Cbut the answers discuss more how to integrate a function (I think...). I understand that this can be done simply by calculating the area under the line segment between each pair of points from start to finish, but I'd rather not reinvent the wheel if this has already been done. I apologize in advance if this is a duplicate; I searched pretty extensively to no avail. My math isn't as strong as I'd like it so it's entirely possible I'm using the wrong terminology.
我正在寻找一个库来查找 C++(或 C,但最好是 C++)中给定的一组随机数据(而不是函数)的积分。还有另一个问题询问 C 中的集成,但答案更多地讨论了如何集成一个函数(我认为......)。我知道这可以通过计算从开始到结束的每对点之间的线段下的面积来完成,但如果已经完成,我宁愿不重新发明轮子。如果这是重复的,我提前道歉;我进行了相当广泛的搜索,但无济于事。我的数学没有我想要的那么强,所以我完全有可能使用了错误的术语。
Thanks in advance for any help!
在此先感谢您的帮助!
Chris
克里斯
Edit:In case anybody is interested, I feel like an idiot. Even adding in a bunch of OO abstraction to make my other code easier to use, that was maybe 30 lines of code. This is what 3 years away from any sort of math will do to you...thanks for all of the help!
编辑:如果有人感兴趣,我觉得自己像个白痴。即使添加了一堆 OO 抽象以使我的其他代码更易于使用,也可能只有 30 行代码。这就是 3 年后远离任何类型的数学会对你做的事情......感谢所有的帮助!
回答by Andreas Rejbrand
This is trivial. If the points are (x0, y0), (x1, y1), ..., (xN, yN), and the points are ordered so that x0 <= x1 <= ... <= xN, then the integral is
这是微不足道的。如果点是 (x0, y0), (x1, y1), ..., (xN, yN),并且这些点是有序的,使得 x0 <= x1 <= ... <= xN,那么积分是
- y0 * (x1 - x0) + y1 * (x2 - x1) + ...
- y0 * (x1 - x0) + y1 * (x2 - x1) + ...
using no interpolation (summing areas of rectangles), and
不使用插值(矩形的面积求和),以及
- (y0 + y1)/2 * (x1 - x0) + (y1 + y2)/2 * (x2 - x1) + ...
- (y0 + y1)/2 * (x1 - x0) + (y1 + y2)/2 * (x2 - x1) + ...
using linear interpolation (summing areas of trapezia).
使用线性插值(对梯形区域求和)。
The problem is especially simple if your data is y0, y1, ..., yN and the corresponding x values are assumed to be 0, 1, ..., N. Then you get
如果你的数据是 y0, y1, ..., yN 并且对应的 x 值被假定为 0, 1, ..., N,那么问题就特别简单了。 那么你得到
- y0 + y1 + ...
- y0 + y1 + ...
using no interpolation (summing areas of rectangles), and
不使用插值(矩形的面积求和),以及
- (y0 + y1)/2 + (y1 + y2)/2 + ...
- (y0 + y1)/2 + (y1 + y2)/2 + ...
using linear interpolation (summing areas of trapezia).
使用线性插值(对梯形区域求和)。
Of course, using some simple algebra, the trapezia formulae can be simplified. For instance, in the last case, you get
当然,使用一些简单的代数,可以简化梯形公式。例如,在最后一种情况下,你得到
- y0/2 + y1 + y2 + ...
- y0/2 + y1 + y2 + ...
回答by Ahmed Kotb
i have just had my numerical exam today :) and i have 3 rules for you
我今天刚刚参加了数值考试 :) 我有 3 条规则给你
Trapezoidal rule :
梯形法则:
integral = h/2 * ( y0 + 2y1 + 2y2 + 2y3 ....... + yn)
积分 = h/2 * ( y0 + 2y1 + 2y2 + 2y3 ...... + yn)
Mid-point rule :
中点规则:
integral = h * ( y0.5 + y1.5 + y2.5 + .... y(n-0.5) )
积分 = h * ( y0.5 + y1.5 + y2.5 + .... y(n-0.5) )
y0.5 means the value of y at the point between x0 and x1
y0.5 表示 x0 和 x1 之间点处的 y 值
Simpsons Rule :
辛普森规则:
integral = h/3 * ( y0 + 4y1 + 2y2 + 4y3 + 2y4 ....... + yn)
积分 = h/3 * ( y0 + 4y1 + 2y2 + 4y3 + 2y4 .... + yn)
where h is the step you take which is usually small number ( but not too small to avoid round off error) and n is the number of periods you take
其中 h 是您采取的步骤,通常是小数(但不能太小以避免舍入错误),n 是您采取的周期数
those were the easy to apply ... you can read more about gauss quadrature also
那些很容易应用......你也可以阅读更多关于高斯正交的信息
references :
参考 :
回答by Hans Passant
Yeah sure, it is that simple. Just sum the areas of the trapezoids formed by the data points you have. You cannot make it more complicated than that. Looking for a library to do it is fairly pointless, you'll just write code to whack the data into the format that the library needs. Calculating it yourself will be less code.
没错,就是这么简单。只需将您拥有的数据点形成的梯形面积相加即可。你不能让它更复杂。寻找一个库来完成它是毫无意义的,您只需编写代码将数据转换为库所需的格式。自己计算会少一些代码。
回答by Hans Passant
Given points (x0, y0), (x1, y1) the area under the trapezoid is (x1 - x0) * (y0 + y1) / 2.
给定点 (x0, y0), (x1, y1) 梯形下的面积为 (x1 - x0) * (y0 + y1) / 2。
You can calculate the entire area by summing up these.
您可以通过总结这些来计算整个面积。
回答by duffymo
Your "random data" consists of a set of (x,y) pairs. Before you start the integration, you have to be sure that the pairs are sorted into a list where the values for x increase monotonically. Once you have that, trapezoid integration should be sufficient. (aka Simpson's rule).
您的“随机数据”由一组 (x,y) 对组成。在开始积分之前,您必须确保将这些对排序到一个列表中,其中 x 的值单调增加。一旦你有了它,梯形积分就足够了。(又名辛普森法则)。

