- SciPy 教程
- SciPy - 主頁
- SciPy - 簡介
- SciPy - 環境設定
- SciPy - 基本功能
- SciPy - 叢集
- SciPy - 常量
- SciPy - FFTpack
- SciPy - 整合
- SciPy - 插值
- SciPy - 輸入和輸出
- SciPy - 線性代數
- SciPy - N 維影像
- SciPy - 最佳化
- SciPy - 統計
- SciPy - CS 圖
- SciPy - 空間
- SciPy - ODR
- SciPy - 特殊包
- SciPy 實用資源
- SciPy - 參考
- SciPy - 快速指南
- SciPy - 實用資源
- SciPy - 討論
SciPy - integrate.newton_cotes() 方法
SciPy integrate.newton-cotes() 方法用於返回牛頓 - 科茨積分的權重和誤差係數。讓我們詳細瞭解權重和誤差係數的術語 -
- 權重:此係數適用於函式以表示指定點。因此,此係數操作積分。
- 誤差係數:使用以下公式計算權重,該公式準確地對一定程度的多項式進行積分。
語法
以下是 SciPy integrate.newton-cotes() 方法的語法 -
newton_cotes(int_val)
引數
此方法僅接受一個引數,以整數形式確定階值。
返回值
此方法以兩種不同的形式返回結果 - 浮點數和列表。
示例 1
以下是一個基本示例,展示了 SciPy integrate.newton-cotes() 方法的用法。
import numpy as np
from scipy import integrate
def exp_fun(x):
return np.exp(-x)
res = integrate.romberg(exp_fun, 0, 1)
print("The result of integrating exp(-x) from 0 to 1:", res)
# Order of Newton-Cotes rule
rn = 4
# Compute weights and error coefficient
weights, error_coeff = integrate.newton_cotes(rn)
print("The weights is ", weights)
print("The error coefficient is ", error_coeff)
輸出
以上程式碼產生以下輸出 -
The result of integrating exp(-x) from 0 to 1: 0.63212055882857 The weights is [0.31111111 1.42222222 0.53333333 1.42222222 0.31111111] The error coefficient is -0.008465608465608466
示例 2
下面的程式執行使用權重的數值積分任務。
import numpy as np
from scipy.integrate import newton_cotes
# define the function to integrate
def fun(x):
return np.sin(x)
# define the interval [a, b]
a = 0
b = np.pi
# Order of Newton-Cotes rule
rn = 3
# calculate weights and error coefficient
weights, _ = newton_cotes(rn)
# calculate the points at which the function is evaluated
x = np.linspace(a, b, rn+1)
# calculate the integral approximation
integral_approx = (b - a) * np.dot(weights, fun(x)) / rn
print("The result of approximate integral:", integral_approx)
輸出
以上程式碼產生以下輸出 -
The result of approximate integral: 2.040524284763495
scipy_reference.htm
廣告