SciPy - integrate.dblquad() 方法



SciPy 的 integrate.dblquad() 用於計算雙重數值積分,這意味著它接受兩個變數(例如 (x, y))進行運算。線性代數數學中使用了兩個變數的計算。

語法

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

dblquad(func, a, b, lambda x: 0, lambda x: 1)

引數

此方法接受以下引數:

  • func:此引數用於處理積分。
  • a:將整數數值傳遞給此引數(x 的積分上限)。
  • b:將整數數值傳遞給此引數(x 的積分下限)。
  • lambda x: 0:此引數用於表示關於 x 的內層積分變數 y 的下限 (0)。
  • lambda x: 1:此引數用於表示關於 x 的內層積分變數 y 的上限 (1)。

返回值

此方法以浮點數形式返回結果。

示例 1

以下基本示例演示了 integrate.dblquad() 方法的使用。

from scipy import integrate

# define the function 
def fun(x, y):
    return x + y

# perform the operation using dblquad()
res, err = integrate.dblquad(fun, 0, 1, lambda x: 0, lambda x: 1)

# display the result
print("The result is ", res)
print("The estimated error is ", err)

輸出

以上程式碼產生以下輸出:

The result is  1.0
The estimated error is  1.662923778137264e-14

示例 2

在這裡,我們對函式 f(x,y) = x.y 進行雙重積分,積分割槽域覆蓋 x,並且 x 的範圍在 0 到 1 之間。

from scipy.integrate import dblquad

# define the function 
def fun(x, y):
    return x * y

# perform the operation using dblquad()
res, err = dblquad(fun, 0, 1, lambda x: x**2, lambda x: x)

# display the result
print("The result is ", res)
print("The estimated error is ", err)

輸出

以上程式碼產生以下輸出:

The result is  0.04166666666666666
The estimated error is  1.0313680578775149e-15

示例 3

下面是 dblquad() 的另一個演示,它使用三個變數(例如 (x, y, z))執行任務,並藉助 lambda 函式計算結果。

在數學上,它表示為 f(x,y) = c(x+y),在 x 的某個區域內,並且具有範圍區間。

from scipy.integrate import dblquad

# define the function 
def fun(x, y, c):
    return c * (x + y)

c = 2

# perform the operation using dblquad()
res, err = dblquad(fun, 0, 2, lambda x: 1, lambda x: 3, args=(c,))

# display the result
print("The result is ", res)
print("The estimated error is ", err)

輸出

以上程式碼產生以下輸出:

The result is  24.0
The estimated error is  2.6645352591003757e-13
scipy_reference.htm
廣告
© . All rights reserved.