SciPy - integrate.tplquad() 方法



SciPy 的 integrate.tplquad() 方法用于计算三重数值积分。这意味着该方法可以接受三个变量(例如 x、y、z)进行运算。这种类型的积分应用于物理学、工程学和环境科学等各个领域。

语法

以下是 SciPy integrate.tplquad() 方法的语法:

tplquad(func, a, b, gfun, hfun, qfun, rfun)

参数

此函数接受以下参数:

  • func:此参数用于处理被积函数。
  • a:此参数接受整数作为 x 的积分下限。
  • b:此参数接受整数作为 x 的积分上限。
  • gfun:此参数表示 y 的下界曲线。
  • hfun:此参数表示 y 的上界曲线。
  • qfun:此参数表示 z 的下界曲面,必须是一个函数,并按顺序接受两个参数 (x, y)。
  • rfun:此参数表示 z 的上界曲面。

返回值

此方法返回一个浮点数值作为结果。

示例 1

以下是使用 SciPy integrate.tplquad() 方法计算抛物面 z = 4-x2 - y2 和 xy 平面围成的体积的基本表示。

from scipy import integrate

# define the customize function
def integrand(z, y, x):
    return 1

# limits for x and y
a, b = -2, 2
gfun = lambda x: -2
hfun = lambda x: 2
qfun = lambda x, y: 0
rfun = lambda x, y: 4 - x**2 - y**2

# operation on integration
res, err = integrate.tplquad(integrand, a, b, gfun, hfun, qfun, rfun)

# print the result
print(f"Volume: {res}, Error: {err}")

输出

上述代码产生以下输出:

Volume: 21.333333333333336, Error: 2.7943958177832873e-13

示例 2

为了获得密度函数的质量,它使用三个单位表示密度,例如 [p(x,y,z) = x2 + y2 + z2],在单位立方体 [0,1]*[0,1]*[0,1] 上。因此,这将以浮点数值的形式返回结果。

from scipy.integrate import tplquad

# define the customize function
def density(z, y, x):
    return x**2 + y**2 + z**2

# limits for x, y, z
a, b = 0, 1
gfun = lambda x: 0
hfun = lambda x: 1
qfun = lambda x, y: 0
rfun = lambda x, y: 1

# operation on integration
res, err = tplquad(density, a, b, gfun, hfun, qfun, rfun)

# print the result
print(f"Mass: {res}, Error: {err}")

输出

上述代码产生以下输出:

Mass: 1.0, Error: 2.5808878251226036e-14

示例 3

程序计算 f(x,y,z) = x+y+z 在以原点为中心,半径为 1 的球形区域上的积分。

from scipy.integrate import tplquad
import numpy as np

# define the customize function
def integrand(z, y, x):
    return x + y + z

# limits for x, y, z
a, b = -1, 1
gfun = lambda x: -np.sqrt(1 - x**2)
hfun = lambda x: np.sqrt(1 - x**2)
qfun = lambda x, y: -np.sqrt(1 - x**2 - y**2)
rfun = lambda x, y: np.sqrt(1 - x**2 - y**2)

# operation on integration
res, err = tplquad(integrand, a, b, gfun, hfun, qfun, rfun)

# print the result
print(f"Integral: {res}, Error: {err}")

输出

上述代码产生以下输出:

Integral: 0.0, Error: 7.692504411238588e-10
scipy_reference.htm
广告
© . All rights reserved.