SciPy integrate.simpson() function (4 examples)

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

The SciPy library is a central tool for scientific computing in Python, offering a wide array of efficient numerical routines, including optimization, linear algebra, integration, interpolation, and many others. In this tutorial, we’ll take a closer look at the integrate.simpson() function, an essential tool for numerical integration. We’ll start from the basics and progressively cover more advanced examples, demonstrating the versatility and power of this function.

Overview of SciPy’s integrate.simpson()

Numerical integration is a cornerstone in the field of scientific computing, enabling the approximation of definite integrals that are often unfeasible to compute analytically. The SciPy library’s integrate.simpson() function provides a method to perform such calculations using Simpson’s rule. This rule offers a balance between simplicity and accuracy, making it particularly useful for a wide array of applications.

Basic Example

First, let’s start with a basic example to illustrate how integrate.simpson() can be used. Suppose we want to integrate the function f(x) = x^2 over the interval [0, 3]. The Python code snippet below shows how to perform this:

import numpy as np
from scipy import integrate

# Define the function to be integrated
f = lambda x: x**2

# Create an array of points where the function will be evaluated
x = np.linspace(0, 3, 100)
y = f(x)

# Perform the integration
result = integrate.simpson(y, x)
print("Integration result:", result)

The output of this code snippet will be:

Integration result: 8.999999999999998

Integrating Experimental Data

Next, we address a common scenario in scientific computing: integrating experimental data. In situations where you have a set of data points rather than a functional form, integrate.simpson() can be particularly handy. Consider a dataset that represents the y-values observed at regular x-intervals:

import numpy as np
from scipy import integrate

# Dataset of observed values
x = np.linspace(0, 10, 11)
y = np.array([0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100])

# Perform the integration
result = integrate.simpson(y, x)
print("Experimental data integration result:", result)

The output showcases an effective use of integrate.simpson() on discrete data:

Experimental data integration result: 333.3333333333333

Weighted Integration

Moving forward to a more complex example, let’s explore a scenario where the data points have differing weights. This is particularly relevant in cases where some measurements are considered more reliable than others. The integrate.simpson() function accommodates for such scenarios through its dx and axis parameters:

import numpy as np
from scipy import integrate

# Weighted data points and their respective weights
x = np.linspace(0, 10, 10)
y = np.exp(-x/3.0)
w = np.linspace(1, 10, 10)  # Imaginary weights for illustration

# Integrate using the weights
result = integrate.simpson(y, x, w)
print("Weighted integration result:", result)

Output:

Weighted integration result: 2.8931540444793495

This demonstrates how integrate.simpson() can adapt to more complex numerical integration tasks, accommodating various forms of data and their specific needs.

Integrating Multivariable Functions

Finally, we delve into a more advanced topic: integrating multivariable functions. While the focus of integrate.simpson() is primarily on univariate functions, utilizing a strategic approach allows for handling multivariable integrations as well. Here’s how:

import numpy as np
from scipy import integrate

# Define a multivariable function
f = lambda x, y: x*y + np.sin(x)

# Generate a 2D grid of points
x = np.linspace(0, np.pi, 100)
y = np.linspace(0, np.pi, 100)
X, Y = np.meshgrid(x, y)
Z = f(X, Y)

# Integrate over each dimension separately
result_x = integrate.simpson(integrate.simpson(Z, x, axis=1), y)
print("Multivariable integration result:", result_x)

Output:

Multivariable integration result: 30.63545823375322

Note that the integration is performed over each dimension separately, showcasing the flexibility and potential for integrate.simpson() to tackle even the most complex numerical integration challenges.

Conclusion

The integrate.simpson() function is a versatile tool for numerical integration, offering simplicity, efficiency, and broad applicability across a wide range of scientific computing needs. From basic function integration to more complex applications like experimental data integration, weighted assimilation, and multivariable functions, it facilitates robust and accurate computational solutions.