Python

Scipy Tplquad

The numerical integration library named “SciPy” provides various tools and methods for solving different types of integrals. The “scipy.integrate.tplquad()” method of the “scipy” library is used to compute a triple (definite) integral of a function of three variables over a given domain.

This Python post presents a detailed guide on the “scipy.integrate.tplquad()” method by covering the below-given contents:

What is the “scipy.integrate.tplquad()” Method in Python?

The “scipy.integrate.tplquad()” method is used to perform triple integration over a specified range of variables. It uses an adaptive algorithm to numerically evaluate the integral and provides accurate results even for complex functions and regions.

Syntax

scipy.integrate.tplquad(func, a, b, gfun, hfun, qfun)

Parameters

In the above syntax:

  • The “func” parameter specifies the function to be integrated through the defined region.
  • The “a” parameter specifies the lower limit of integration along the “x”-axis.
  • The “b” parameter specifies the upper limit of integration along the “x”-axis.
  • The “gfun” parameter indicates the lower limit of integration along the “y”-axis. It can be a function or constant value.
  • The “hfun” parameter indicates the upper limit of integration along the “y”-axis. It can be a function or constant value.
  • The “qfun” parameter defines the lower and upper limits of integration along the “z”-axis.

Return Value

The return value of the “scipy.integrate.tplquad()” method is a tuple of two values:

  • The first floating value is the function “func” triple integral.
  • The second floating value is an estimate of the integral error.

Example 1: Calculating Triple Integral of Function Using the “scipy.integrate.tplquad()” Method

Let’s say we want to determine the triple integral of the below function:

f(x, y, z) = x^3 + y^3 + z^3

The region defined for the above function is, as follows:

x ∈ [0, 1], y ∈ [0, 2], and z ∈ [0, 3].

Here’s the code that computes the triple integral of the above function with the defined region:

import scipy.integrate

def f(x, y, z):
  return x**3 + y**3 + z**3
result = scipy.integrate.tplquad(
    f, 0, 1, lambda x: 0, lambda x: 2, lambda x, y: 0, lambda x, y: 3)

print("Result:", result[0])

In the above code:

  • The “scipy.integrate” module is imported.
  • The user-defined function “f(x, y,z)” defines the function we want to integrate.
  • The “scipy.integrate.tplquad()” method is used to evaluate the triple integral.
  • The “0” and “1” parameters are used to specify the lower and upper limits of integration along the “x” axis.
  • The lambda functions lambda “x: 0” and lambda “x: 2” define the limits of integration for “y”-axis.
  • The lambda functions lambda” x, y: 0” and lambda “x, y: 3” define the limits of integration for “z”-axis.
  • Finally, print the result of the triple integration.

Output

The triple integration has been determined.

Example 2: Calculating Triple Integral of Function With Exponential Term Using the “scipy.integrate.tplquad()” Method

Let’s analyze the below code:

import scipy.integrate
from math import exp

def f(z, y, x):
  return x * y * z * exp(-x * y * z)
output = scipy.integrate.tplquad(
    f, 0, 1, lambda x: 0, lambda x: 1, lambda x, y: 0, lambda x, y: 1)

print(output[0])

In these code lines, the triple integral of the input function having the exponential term “exp(-x * y * z)” is calculated using the “scipy.integrate.tplquad()” method. This method automatically uses a special integration technique to handle the exponential term.

Output

The triple integration has been computed appropriately.

Conclusion

The “scipy.integrate.tplquad()” method is used to perform triple integration of simple to complex functions over a range of variables in Python. This method takes several parameters such as function, and lower and upper limits for the “x” and “y” axis, and determines a triple integration. This article delivered a thorough tutorial on the “scipy.integrate.tplquad()” method using numerous examples.

About the author

Haroon Javed

Hi, I'm Haroon. I am an electronics engineer and a technical content writer. I am a tech geek who loves to help people to the best of my knowledge.