Scipy Multiple Integration (nquad)




Multiple Integration in SciPy

Multiple integration in SciPy is used to calculating the integral of a function over more than one variable i.e., double, triple or higher-dimensional integrals. The scipy.integrate module which provides functions such as dblquad for double integration, tplquad for triple integration, quad for single integration and nquad for integration over multiple variables.

In multiple integration we can define the function to be integrated along with the integration limits for each variable and also any additional parameters. SciPy handles both finite and infinite limits and can integrate complex functions over specified ranges by providing an efficient solution for high-dimensional problems such as physics simulations and probability distributions.

Mathematical Formula for the n-dimensional Integration over a region of D −

Multiple Integration Mathathematical Formula

The limits of the multiple integration depends on the specific region D in n-dimensional space.

Syntax

Following is the syntax of the function scipy.integrate.nquad() which is used to calculate the multiple integration −

scipy.integrate.nquad(func, ranges, args=None, opts=None, full_output=False)

Parameters

Here are the parameters for the nquad() function of scipy.integrate module −

  • func: The function to be integrate. It should take the variables being integrated as individual arguments.
  • ranges: A list of limits for each variable's integration given as tuples (a, b) where a and b are the bounds. Use -inf or inf for infinite limits in improper integrals.
  • args(optional): Additional arguments to pass to func.
  • opts(optional): Integration options for each dimension such as tolerance settings or other parameters for the integrator.
  • full_output(optional): If this parameter is True then the extra information about the integration process is returned in a dictionary.

Example of Multiple Integration

This example shows how to use nquad() to perform double integration of a two-variable function over a specified rectangular region. We can modify the integrand and limits as needed for different applications −

import numpy as np
from scipy.integrate import nquad

# Define the function to integrate
def integrand(x, y):
    return np.sin(x) + np.cos(y)

# Define the limits for x and y
# x goes from 0 to pi
# y goes from 0 to pi/2
ranges = [[0, np.pi], [0, np.pi / 2]]

# Perform the double integration
result, error = nquad(integrand, ranges)

# Output the results
print(f"Double Integral Result: {result}")
print(f"Error Estimate: {error}")

Following is the output of the calculating the dobule integration with the help of nquad() function −

Double Integral Result: 6.283185307179586
Error Estimate: 6.975736996017264e-14

Example to Perform Double Integration

This example shows how to use nquad() to perform double integration of a two-variable function over a specified rectangular region. We can modify the integrand and limits as needed for different applications −

import numpy as np
from scipy.integrate import nquad

# Define the function to integrate
def integrand(x, y):
    return np.sin(x) + np.cos(y)

# Define the limits for x and y
# x goes from 0 to pi
# y goes from 0 to pi/2
ranges = [[0, np.pi], [0, np.pi / 2]]

# Perform the double integration
result, error = nquad(integrand, ranges)

# Output the results
print(f"Double Integral Result: {result}")
print(f"Error Estimate: {error}")

Following is the output of the calculating the double integration with the help of nquad() function −

Double Integral Result: 6.283185307179586
Error Estimate: 6.975736996017264e-14

Example to Perform Triple Integration

Here's an example of triple integration using the nquad() function from the scipy.integrate module in Python. This example will calculate the integral of the function f(x,y,z) = x2+y2+z2 over a specified range for each variable −

from scipy.integrate import nquad

# Define the function to integrate
def func(x, y, z):
    return x**2 + y**2 + z**2

# Specify the ranges for x, y, and z
ranges = [[0, 1], [0, 1], [0, 1]]

# Perform the triple integration
result, error = nquad(func, ranges)

# Output the result
print("Result of the triple integration:", result)
print("Estimated error:", error)

Here is the output of the calculating the multiple integration with the help of nquad() function −

Result of the triple integration: 1.0
Estimated error: 2.5808878251226036e-14

Key Features of the nquad() Function

Following are the key features of nquad() of scipy.integrate module −

  • The nquad() function handles multiple Integrations of any dimension.
  • This function supports variable limits for integration.
  • It can pass additional arguments to the integrand function.
  • This function perform customizable integration settings via the opts parameter.
Advertisements