SciPy - integrate.fixed_quad() 方法



SciPy integrate.fixed_quad() 方法對正態高斯積分的固定階進行操作以進行數值積分。正態高斯積分由區間內的一組點定義。

語法

以下是 SciPy integrate.fixed_quad() 方法的語法 −

fixed_quad(custom_func, int_val1, int_val2)

引數

此函式接受以下引數 −

  • custom_func:此引數用於設定在該區間上進行特定積分的計算。
  • int_val1:此引數定義積分的下限。
  • int_val2:此引數定義積分的上限。

返回值

方法將混合的不同資料值()返回為元組形式。

示例 1

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

import scipy.integrate as integrate

def simple_polynomial(x):
    return x**2

res = integrate.fixed_quad(simple_polynomial, 0, 1)
print(f"Integral of x^2 from 0 to 1: {res}")

輸出

上述程式碼生成以下輸出 −

Integral of x^2 from 0 to 1: (0.33333333333333326, None)

示例 2

此程式演示了指數函式積分,該積分可在區間 [0, 2] 上使用。

import scipy.integrate as integrate
import numpy as np

def exp_fun(x):
    return np.exp(x)

res = integrate.fixed_quad(exp_fun, 0, 2)
print(f"Integral of e^x from 0 to 2: {res}")

輸出

上述程式碼生成以下輸出 −

Integral of e^x from 0 to 2: (6.389056096688674, None)

示例 3

以下程式顯示了正弦函式積分,其範圍區間為 [0, pi]。

import scipy.integrate as integrate
import numpy as np

def sine_function(x):
    return np.sin(x)

res = integrate.fixed_quad(sine_function, 0, np.pi)
print(f"Integral of sin(x) from 0 to pi: {res}")

輸出

上述程式碼生成以下輸出 −

Integral of sin(x) from 0 to pi: (2.0000001102844727, None)
scipy_reference.htm
廣告
© . All rights reserved.