Using integrate.fixed_quad() function in SciPy (4 examples)

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

Introduction

Scientific Computing with Python opens a diversity of doors to solve mathematical problems efficiently. One of the jewels in the crown of Python’s scientific stack is SciPy, a library that provides modules for optimization, integration, interpolation, eigenvalue problems, algebraic equations, and much more. This article focuses on one particular function, integrate.fixed_quad(), from the SciPy library, elucidating its practicality through four incremental example scenarios from elementary to more composite cases.

The fixed_quad() function is part of SciPy’s integration sub-package and implements fixed-order Gaussian quadrature for approximating definite integrals. It’s prized for its precision and efficiency in computing integrals of functions known to be smooth.

Example 1: Basic Integral Calculation

Let’s start simple by computing the integral of a basic function, say, f(x)=x^2 over the interval [0, 2].

from scipy import integrate
import numpy as np

# Define the function to be integrated
f = lambda x: x**2
result = integrate.fixed_quad(f, 0, 2, n=5)
print("Integral result:", result[0])

Output:

Integral result: 2.6666666666666665

This initial example showcases the ease and straightforwardness of using fixed_quad() to compute basic integrals. The parameter n specifies the order of quadrature, and in this case, we used 5, yielding a quick and precise result.

Example 2: Integrating Complex Functions

Moving onto a slightly more complex function, we’ll integrate e^(-x^2), a Gaussian function, over the same interval [0, 2].

from scipy import integrate
import numpy as np

f = lambda x: np.exp(-x**2)
result = integrate.fixed_quad(f, 0, 2, n=5)
print("Integral result:", result[0])

Output:

Integral result: 0.8820813907624215

This example demonstrates integrating a function that is more intricate than a simple polynomial. Again, the process remains concise and the result, reliable.

Example 3: Variable Order of Integration

The flexibility of fixed_quad() is also seen when varying the quadrature order based on integration needs. Let’s vary the order for the Gaussian function integral.

for n in range(1, 8):
    result = integrate.fixed_quad(f, 0, 2, n=n)
    print(f"Order {n}, Integral result: {result[0]}")

This snippet prints the result of our integral for different orders of quadrature from 1 to 7, illustrating how the order affects the accuracy of the integration. This adaptability provides a useful lever for calibrating computational effort against precision requirements.

Example 4: Integrating with Parameters

Finally, let’s explore integrating a function that takes additional parameters besides the integration variable. Consider the family of functions f(x, a, b) = a * x^2 + b, where a and b are parameters. We’re aiming to integrate this function over [0, 2] with varying parameters.

from scipy import integrate
import numpy as np

# Define a parametric function
f = lambda x, a, b: a * x**2 + b
def integrate_with_parameters(a, b):
    result = integrate.fixed_quad(f, 0, 2, n=5, args=(a,b))
    print(f"For parameters a={a}, b={b}, Integral result: {result[0]}")

integrate_with_parameters(1, 0)
integrate_with_parameters(2, 1)

This code block, by leveraging the args parameter of fixed_quad(), allows integration of a family of functions with different parameters smoothly and effectively.

Conclusion

In conclusion, SciPy’s integrate.fixed_quad() function offers a powerful, precise tool for numerical integration across an extensive array of functional forms. Through these examples, from basic polynomial integration to more sophisticated uses involving variable orders and additional parameters, we see the versatility and reliability of fixed-order Gaussian quadrature. Whether you’re a student, researcher, or professional, integrating this technique into your computational toolbox can significantly enhance your analytical capabilities.