SciPy integrate.cumulative_trapezoid() function (4 examples)

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

In this tutorial, we’ll dive into the powerful SciPy library, specifically exploring the integrate.cumulative_trapezoid() function. This function is used for numerical integration, providing a way to estimate the cumulative integral of a given function or dataset. We will guide you through its usage with four progressively complex examples, enhancing your understanding of numerical integration and how it can be applied in various scenarios.

Introduction to integrate.cumulative_trapezoid()

The integrate.cumulative_trapezoid() function is part of the SciPy library’s integrate module. It approximates the integral of a function or a set of discrete points by breaking the domain into trapezoids, calculating the area of each, and then summing these areas. This method is known as the trapezoidal rule. cumulative_trapezoid(), as the name suggests, computes the cumulative integral, giving an array of values representing the integral up to each data point.

Before diving into the examples, ensure SciPy is installed in your environment:

pip install scipy

Example 1: Basic Usage

Let’s start with the most straightforward implementation, where we will integrate a simple function over a specified interval.

import numpy as np
from scipy import integrate

# Define the domain
x = np.linspace(0, 10, 100)

# Simple quadratic function
y = x**2

# Calculate the cumulative integral
integral = integrate.cumulative_trapezoid(y, x)

print('Cumulative integral:', integral[-1])

This will print the cumulative integral of x^2 from 0 to 10. The result should be close to the analytical solution, which is 1000/3.

Output:

Cumulative integral: 333.35033840084344

Example 2: Using with a Dataset

In our second example, we’ll apply integrate.cumulative_trapezoid() to a dataset. This is particularly useful when you have empirical data that cannot be easily modeled by a function.

import numpy as np
from scipy import integrate

# Time and velocity data for an object
time = np.array([0, 1, 2, 3, 4, 5])
velocity = np.array([0, 10, 20, 30, 40, 50])

# Calculating the displacement
displacement = integrate.cumulative_trapezoid(velocity, time, initial=0)

print('Displacement:', displacement)

Output:

Displacement: [  0.   5.  20.  45.  80. 125.]

This calculates the object’s displacement at each time step using the given velocity data, demonstrating how integrate.cumulative_trapezoid() can be applied to real-world data.

Example 3: Axis and dx Parameters

The integrate.cumulative_trapezoid() function offers flexibility through the axis and dx parameters, allowing integration over different dimensions and specifying the spacing between points if uniform data is not used.

import numpy as np
from scipy import integrate

# 2D array representing a surface temperature profile
# Each row represents a time step, and each column a location
temp_profile = np.random.rand(10, 10)

# Integrating over time (axis 0)
cum_integral = integrate.cumulative_trapezoid(temp_profile, dx=1, axis=0)

print('Cumulative integral over time:', cum_integral)

Output (vary):

Cumulative integral over time: [[0.84499534 0.54684432 0.55140105 0.14377012 0.1610495  0.53619976
  0.52025729 0.79891168 0.6485951  0.13693468]
 [1.74850651 1.14615465 1.08113175 0.23290704 0.57767074 1.08803183
  1.15708013 1.43612515 1.28407718 0.39043385]
 [2.54407447 1.6570672  1.51114139 0.40042859 1.01055046 1.61242749
  1.54366804 2.08481985 1.92271815 0.93498345]
 [2.94124994 2.03745787 1.92882612 0.57154627 1.08323111 2.25686139
  2.10353567 2.73020245 2.59219419 1.54691447]
 [3.2091633  2.45895317 2.18567467 0.8682107  1.29896983 2.60139619
  2.5759901  3.21737783 3.32519049 1.77817019]
 [3.86829974 3.10959284 2.3892084  1.32889343 1.64782012 3.10038442
  2.85163439 3.66870845 3.82399904 1.91018358]
 [4.34682567 3.57865736 2.69797412 1.63170094 2.20042229 3.76847773
  3.37254059 4.0184409  4.36070166 2.06799108]
 [4.9183768  4.1395095  2.92797872 1.92334904 2.62194687 4.26646691
  3.92108777 4.42393301 5.02003464 2.38225985]
 [5.57264248 4.96691844 3.54537678 2.61456067 2.71724906 4.48636331
  4.53249395 5.02465542 5.30291763 2.88637329]]

This example showcases integrating over a specified axis of a multidimensional array, useful in fields such as physics and engineering for analyzing temperatures, pressures, and more over different dimensions.

Example 4: Handling Uneven Spacing with x Parameter

The x parameter enables handling datasets with uneven spacing between data points, allowing for more accurate integration.

import numpy as np
from scipy import integrate

# Dataset with unevenly spaced points
x = np.array([0, 0.5, 2, 3.5, 5, 7.5])
y = np.cos(x)

# Computing the cumulative integral with uneven spacing
c_integral = integrate.cumulative_trapezoid(y, x)

print('Cumulative integral with uneven spacing:', c_integral)

Output:

Cumulative integral with uneven spacing: [ 0.46939564  0.81547243 -0.19898021 -0.68857608  0.09929579]

Here, the uneven spacing in the x values is taken into account, showing how integrate.cumulative_trapezoid() can be precisely used with non-uniform datasets.

Conclusion

The integrate.cumulative_trapezoid() function is a versatile tool for numerical integration, offering solutions for a wide range of applications, from simple mathematical functions to complex, real-world datasets. Whether you’re working with even or unevenly spaced data, this function can handle it, making it an invaluable resource in your numerical analysis toolkit.