如何在 scipy.integrate.simps 或 numpy.trapz 之间做出决定? [英] How to decide between scipy.integrate.simps or numpy.trapz?

查看:46
本文介绍了如何在 scipy.integrate.simps 或 numpy.trapz 之间做出决定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组点,当我绘制这些点时,我会得到下面的图表.我想在图表下找到区域,但是不确定scipy.integrate.simps还是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.

有人可以就这两个函数之间的数学背景提出建议,从而得出哪个函数更准确的结论吗?

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?

与梯形规则相关的误差,与前导顺序相关,与 h^2[f'(a) - f'(b)] 成正比.h 是函数中采样点之间的间距;f'(a) 和 f'(b) 是您的函数在采样域的开头和结尾处的一阶导数.

The error associated with the trapezoidal rule, to leading order, is proportional to h^2[f'(a) - f'(b)]. h is the spacing between sampled points in your function; f'(a) and f'(b) are the first derivative of your function at the beginning and end of the sampling domain.

另一方面,通过辛普森规则的误差与 h^4[f'''(a) - f'''(b)] 成正比.f'''是函数中的三阶导数.

The error through Simpson's rule, on the other hand, is proportional to h^4[f'''(a) - f'''(b)]. f''' is the third-order derivative in your function.

h 通常很小,所以 h^4 通常比 h^2 小得多!

h is typically small, so h^4 is typically much smaller than h^2!

TLDR:与梯形规则相比,辛普森规则在数值积分方面通常具有更好的结果,

TLDR: Simpson's rule typically gives far superior results for numerical integration, compared to the trapezoidal rule, with basically no additional computational cost.

这篇关于如何在 scipy.integrate.simps 或 numpy.trapz 之间做出决定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆