SciPy integrate.quad() function (4 examples)

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

Welcome to this comprehensive tutorial focused on the integrate.quad() function from the SciPy library. The SciPy integrate module provides several functions for numerical integration, of which quad is most commonly used for single-variable and finite-interval integration. In this guide, we will explore how to use integrate.quad() through four increasingly complex examples.

What is integrate.quad() Used for?

The quad function is part of the SCIPY library’s integrate module. It is primarily designed for integrating a function of one variable over a finite or infinite interval. The syntax is scipy.integrate.quad(func, a, b, args=()), where func is the name of the function to integrate, a and b are the lower and upper limits of integration, respectively, and args is an optional tuple containing extra arguments to pass to the function.

Example 1: Basic Integration

import scipy.integrate as spi
import numpy as np

def func(x):
    return x**2

result, error = spi.quad(func, 0, 1)
print("Integration Result:", result)
print("Error Estimate:", error)

In this basic example, we integrate the simple quadratic function x^2 from 0 to 1. The output will be:

Integration Result: 0.3333333333333333
Error Estimate: 3.700743415417188e-15

Example 2: Integrating with Parameters

def with_params(x, a, b):
    return a * x**2 + b

result, error = spi.quad(with_params, 0, 1, args=(1, 2))
print("Integration Result:", result)
print("Error Estimate:", error)

This example demonstrates how to integrate a function with additional parameters a and b, showcasing the use of the args parameter to pass extra arguments. The output will be akin to:

Integration Result: 2.333333333333333
Error Estimate: 2.591879886150042e-14

Examples 3: Dealing with Singularities

Scipy’s quad function can handle singularities within the integration interval. Let’s integrate a function with a known singularity:

import matplotlib.pyplot as plt

def singular_func(x):
    return 1/np.sqrt(x)

result, error = spi.quad(singular_func, 0, 1)
print("Integration Result:", result)
print("Error Estimate:", error)

This example integrates a function that tends towards infinity at the lower bound of the integration interval. The quad function manages this gracefully, producing an accurate result.

Example 4: Infinite Limits

def exp_decay(x):
    return np.exp(-x)

result, error = spi.quad(exp_decay, 0, np.inf)
print("Integration Result:", result)
print("Error Estimate:", error)

The final example demonstrates integration over an infinite interval. The function selected decays exponentially, a common scenario for utilizing an infinite upper limit. Here, the quad function again proves its versatility and accuracy.

Conclusion

This guide has explored the Scipy integrate.quad() function through a variety of scenarios, from basic integrations to handling singularities and infinite limits. Through these examples, we have seen the power and flexibility of quad in solving integration challenges, making it an invaluable tool for anyone involved in numerical computing or data analysis.