scipy.integrate.

simpson#

scipy.integrate.simpson(y, x=None, *, dx=1.0, axis=-1)[source]#

使用沿給定軸的樣本和複合辛普森法則積分 y(x)。如果 x 為 None,則假設間距為 dx。

參數:
yarray_like

要積分的陣列。

xarray_like,選填

如果給定,則為 y 的取樣點。

dxfloat,選填

沿 x 軸的積分點間距。僅在 x 為 None 時使用。預設值為 1。

axisint,選填

要積分的軸。預設值為最後一個軸。

回傳:
float

使用複合辛普森法則計算的估計積分。

另請參閱

quad

使用 QUADPACK 的自適應正交

fixed_quad

固定階高斯正交

dblquad

雙重積分

tplquad

三重積分

romb

取樣資料的積分器

cumulative_trapezoid

取樣資料的累積積分

cumulative_simpson

使用辛普森 1/3 法則的累積積分

註解

對於奇數個等間距樣本,如果函數是 3 階或更低階的多項式,則結果是精確的。如果樣本不是等間距的,則只有當函數是 2 階或更低階的多項式時,結果才是精確的。

參考文獻

[1]

Cartwright, Kenneth V. Simpson’s Rule Cumulative Integration with MS Excel and Irregularly-spaced Data. Journal of Mathematical Sciences and Mathematics Education. 12 (2): 1-9

範例

>>> from scipy import integrate
>>> import numpy as np
>>> x = np.arange(0, 10)
>>> y = np.arange(0, 10)
>>> integrate.simpson(y, x=x)
40.5
>>> y = np.power(x, 3)
>>> integrate.simpson(y, x=x)
1640.5
>>> integrate.quad(lambda x: x**3, 0, 9)[0]
1640.25