SciPy: Understanding integrate.nquad() function (4 examples)

Updated: March 7, 2024 By: Guest Contributor Post a comment

SciPy is a fundamental package for scientific computing in Python, providing various tools for optimization, integration, interpolation, eigenvalue problems, algebraic equations, differential equations, and more. One of its powerful tools is the integrate module, which offers functions to perform numerical integration. Among these functions, nquad() stands out for its ability to handle multiple integrals efficiently. This tutorial aims to enlighten you on how to utilize the integrate.nquad() function through four progressively advanced examples.

Understanding the nquad() Function

Before diving into the examples, let’s first get acquainted with what nquad() is and why it’s significant. In essence, nquad() stands for N-dimensional quad integration, a method used to compute N-dimensional definite integrals. This function is particularly useful when dealing with complex integrals over multiple variables.

The syntax of nquad() is:

result, error = scipy.integrate.nquad(func, ranges, args=(), opts={}, full_output=False)
  • func is the integrand function.
  • ranges is a sequence of tuples specifying the integration limits for each variable.
  • args are additional arguments to pass to the function.
  • opts allows you to specify options for each integration variable, such as the method of integration.
  • full_output when set to True, provides additional output information.

Example 1: Basic Single Integral

To start, let’s compute a simple integral:

import scipy.integrate as spi

def my_func(x):
    return x**2

result, error = spi.nquad(my_func, [[0, 2]])
print('Result:', result, 'Error:', error)

This code calculates the integral of \(x^2\) from 0 to 2. You should see:

Result: 2.6666666666666665 Error: 2.9605947323337506e-14

Example 2: Double Integral

Next, let’s tackle a slightly more complex scenario—calculating a double integral:

def my_func2(x, y):
    return x*y

result, error = spi.nquad(my_func2, [[0, 1], [0, 1]])
print('Result:', result, 'Error:', error)

Here, we’re integrating \(xy\) over both x and y, each ranging from 0 to 1. The output is:

Result: 0.25 Error: 2.7755575615628914e-17

Example 3: Triple Integral with Parameters

Moving forward, we introduce an additional variable and parameter into our integral calculation:

def my_func3(x, y, z, a):
    return x*y*z*a

a_param = 2

result, error = spi.nquad(my_func3, [[0, 1], [0, 1], [0, 1]], args=(a_param,))
print('Result:', result, 'Error:', error)

This example demonstrates integrating \(xyz\) multiplied by parameter \(a\), with all variables and the parameter ranging from 0 to 1. The expected output is:

Result: 0.5 Error: 5.551115123125783e-17

Example 4: Advanced Usage with Options

For our final example, we dive into more advanced usage by manipulating integration options:

def complex_func(x, y):
    return np.sin(x) * np.cos(y)

opts = {'limit': 100}  # Increase the limit for complex integrals

result, error = spi.nquad(complex_func, [[0, np.pi], [0, 2*np.pi]], opts=[{}, opts])
print('Result:', result, 'Error:', error)

This code integrates a trigonometric function over a specified range, with increased computation limit settings for improved accuracy. The solution you’ll find is:

Result: 0.0 Error: 0.0

Conclusion

In this tutorial, we’ve explored the nquad() function within SciPy’s integrate module through four examples, ranging from basic to advanced. Understanding how to employ nquad() for numerical integration greatly enhances your scientific computing capabilities, allowing for efficient and accurate computation of multidimensional integrals in Python.