简体   繁体   English

如何确定scipy.integrate.simps还是numpy.trapz?

[英]How to decide between scipy.integrate.simps or numpy.trapz?

I have a set of points, of which when I plot I get the graph below. 我有一组要点,当我作图时,得到以下图表。 I would like to find the area under the graph, however I am not sure whether scipy.integrate.simps or numpy.trapz is more suitable. 我想在图表下找到面积,但是不确定scipy.integrate.simps还是numpy.trapz是否更合适。

Could someone advice me on the mathematical background between the two functions and thus the conclusion on which function is more accurate? 有人可以为我提供这两个函数之间的数学背景的建议,从而得出关于哪个函数更准确的结论吗?

在此处输入图片说明

The trapezoidal rule is the simplest of numerical integration methods. 梯形法则是最简单的数值积分方法。 In effect, it estimates the area under a curve by approximating the curve with straight line segments, which only requires two points for each segment. 实际上,它通过用直线段近似曲线来估计曲线下的面积,每个直线段仅需要两个点。 Simpson's rule uses quadratic curves to approximate the function segments instead, each of which requires three points, sampled from your function, to approximate a given segment. 辛普森法则使用二次曲线来近似函数段,每个函数段都需要从函数中采样的三个点来近似给定段。

So what is the error associated with using these numerical methods as approximations to an analytical integral? 那么,将这些数值方法用作解析积分的近似值有什么误差呢?

The error associated with the trapezoidal rule, to leading order, is proportional to h^2[f'(a) - f'(b)]. 与梯形法则相关的误差,按领先顺序,与h ^ 2 [f'(a)-f'(b)]成正比。 h is the spacing between sampled points in your function; h是函数中采样点之间的间隔; f'(a) and f'(b) are the first derivative of your function at the beginning and end of the sampling domain. f'(a)和f'(b)是函数在采样域的开头和结尾的一阶导数。

The error through Simpson's rule, on the other hand, is proportional to h^4[f'''(a) - f'''(b)]. 另一方面,通过辛普森法则的误差与h ^ 4 [f'''(a)-f'''(b)]成正比。 f''' is the third-order derivative in your function. f'''是函数中的三阶导数。

h is typically small, so h^4 is typically much smaller than h^2! h通常很小,因此h ^ 4通常比h ^ 2小得多!

TLDR: Simpson's rule typically gives far superior results for numerical integration, compared to the trapezoidal rule, with basically no additional computational cost. TLDR:与梯形法则相比,辛普森法则通常为数值积分提供更好的结果,基本上没有额外的计算成本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM