SciPy - integrate.tplquad() Method



The SciPy integrate.tplquad() method is used to calculate the triple numerical integration. It means a method can accept three variable say(x, y, z) to work on. This type of integration uses in various field such as physics, engineering, and environmental science.

Syntax

Following is the syntax of the SciPy integrate.tplquad() method −

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

Parameters

This function accepts the following parameter −

  • func: This parameter is used to work with integrals.
  • a: The integer value is passed to this parameter(limit of integration in x).
  • b: The integer value is passed to this parameter(limit of integration in x).
  • gfun: This parameter represent the lower boundry curve of y.
  • hfun: This parameter represent the upper boundary curve in y.
  • qfun: The parameter represent the lower boundary surface in z which must be a function and accept two order in sequential manner(x, y).
  • rfun: This parameter represent the upper boundry surface in z.

Return value

This method returns the float value as result.

Example 1

Following is basic representation of volume of a paraboloid z = 4-x2 - y2 and the xy is a plane using Scipy integrate.tplquad() method.

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}")

Output

The above code produces the following output −

Volume: 21.333333333333336, Error: 2.7943958177832873e-13

Example 2

To obtain the mass of a density function, it uses three units for density say[p(x,y,z) = x2 + y2 + z2] over the unit cube of [0,1]*[0,1]*[0,1]. Thus, this returns the result in the forms of float values.

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}")

Output

The above code produces the following output −

Mass: 1.0, Error: 2.5808878251226036e-14

Example 3

The program calculate the integral of f(x,y,z) = x+y+z over the spherical region of radius 1 centered at the origin.

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}")

Output

The above code produces the following output −

Integral: 0.0, Error: 7.692504411238588e-10
scipy_reference.htm
Advertisements