SciPy - integrate.trapezoid() 方法



SciPy integrate.trapezoid() 方法用於查詢使用梯形規則的積分函式的近似值。梯形規則有兩個 -

  • 等間距點處的函式值。
  • 間隔的寬度。

語法

以下是可以使用 SciPy integrate.trapezoid() 方法的語法 -

trapezoid(y, x)

引數

此方法接受兩個引數 -

  • y:此引數用於設定要積分的函式的值。
  • x:這也定義相同的值。

返回值

此方法返回浮點值。

示例 1

以下是一個基本示例,顯示了 SciPy integrate.trapezoid() 方法的用法。

import numpy as np
from scipy import integrate

x = np.linspace(0, 10, 100)
y = x**2
integral = integrate.trapezoid(y, x)
print("The resultant value is ", integral)

輸出

上面的程式碼生成以下結果 -

The resultant value is  333.35033840084344

示例 2

此程式為各個變數定義兩個陣列,並將這些變數傳遞給 trapezoid() 以計算積分結果。

import numpy as np
from scipy.integrate import trapezoid
x = np.array([0, 1, 2, 5, 6])
y = np.array([0, 1, 4, 25, 36])
integral = trapezoid(y, x)
print("The resultant value is ", integral)

輸出

上面的程式碼生成以下結果 -

The resultant value is  77.0

示例 3

在下方,程式計算了沿一個軸的多重積分。

import numpy as np
from scipy.integrate import trapezoid
y = np.array([[0, 11, 64, 93, 16], [0, 17, 87, 27, 64]])
x = np.array([0, 1, 24, 34, 4])
integral = trapezoid(y, x, axis=1)
print("The resultant value is ", integral)

輸出

上面的程式碼生成以下結果 -

The resultant value is  [ 18.  409.5]
scipy_reference.htm
廣告
© . All rights reserved.