SciPy - integrate.cumulative_simpson() 方法



SciPy integrate.cumulative_simpson() 方法用於透過考慮單個點與兩個相鄰點之間的二次關係來計算每一對點處的座標。

語法

以下是 SciPy integrate.cumulative_simpson() 方法的語法:-

cumulative_simpson(var1, var2)
or,
cumulative_simpson(var1, var2, sample_point)

引數

此方法接受以下引數:-

  • var1:此引數用於表示各種內建函式,如 linspace()、sin() 等。
  • var2:此引數表示簡單的數學計算。
  • sample_point:此可選引數用於座標/取樣點。

返回值

此方法以兩種不同的形式返回結果,分別是值列表和 matplotlib 圖形。

示例 1

以下是演示 SciPy integrate.cumulative_simpson() 方法用法的一個基本示例。

from scipy import integrate
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-2, 2, num=10)
y = x**2
y_int = integrate.cumulative_simpson(y, x=x, initial=0)
fig, ax = plt.subplots()
ax.plot(x, y_int, 'go', x, x**3/3 - (x[0])**3/3, 'b-')
ax.grid()
plt.show()

輸出

上述程式碼生成以下結果:-

scipy_cumulative_simpson_one

示例 2

該程式使用 sin() 函式計算 0 到 2pi 區間上的取樣(座標)點,其中兩點之間的間隔設定為 0.1。使用 cumulative_simpson() 方法後,結果被儲存為列表。

import numpy as np
from scipy.integrate import cumulative_simpson

# declare y and dx values
y = np.sin(np.linspace(0, 2 * np.pi, 50))
dx = 0.1 # spacing between element of y

# operation on cumulative integral
cum_integral = cumulative_simpson(y, dx=dx)

print(cum_integral)

輸出

上述程式碼生成以下結果:-

[ 6.41135591e-03  2.55054410e-02  5.70028694e-02  1.00353446e-01
  1.54876692e-01  2.19648187e-01  2.93630869e-01  3.75586562e-01
  4.64189453e-01  5.57968599e-01  6.55396160e-01  7.54864639e-01
  8.54744106e-01  9.53395658e-01  1.04919389e+00  1.14057569e+00
  1.22602649e+00  1.30416123e+00  1.37367525e+00  1.43345212e+00
  1.48248242e+00  1.51999138e+00  1.54533088e+00  1.55811848e+00
  1.55810972e+00  1.54533951e+00  1.51998305e+00  1.48249033e+00
  1.43344475e+00  1.37368194e+00  1.30415533e+00  1.22603152e+00
  1.14057163e+00  1.04919692e+00  9.53393703e-01  8.54744950e-01
  7.54864920e-01  6.55394758e-01  5.57971098e-01  4.64185897e-01
  3.75591116e-01  2.93625392e-01  2.19654498e-01  1.54869651e-01
  1.00361101e-01  5.69947265e-02  2.55139387e-02  6.40264306e-03
 -8.71285215e-06]
scipy_reference.htm
廣告
© . All rights reserved.