SciPy integrate.trapezoid() function (4 examples)

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

In this tutorial, we delve into the powerful trapezoid() function provided by the scipy.integrate module. The trapezoid rule is a numerical method for estimating the definite integral of a function. It works by approximating the region under the graph of the function as a trapezoid and calculating its area. The trapezoid() function in SciPy makes this process straightforward in Python. We’ll explore four practical examples, ranging from basic to advanced usage, to demonstrate the versatility of this function.

Getting Started

Before diving into examples, ensure you have SciPy installed in your Python environment. If not, you can install it using pip:

pip install scipy

Now, let’s start with our first example.

Example 1: Basic Integration

For our first example, let’s estimate the integral of f(x) = x^2 from 0 to 1.

import numpy as np
from scipy.integrate import trapezoid

# Define the function f(x) = x^2
f = lambda x: x**2

# Generate points along the x-axis
x = np.linspace(0, 1, 100)

# Function values at the points
y = f(x)

# Calculate the integral using the trapezoid rule
result = trapezoid(y, x)

print("Estimated integral: ", result)

Output:

Estimated integral:  0.33335033840084355

This simple example demonstrates the basic utility of the trapezoid() function. The estimated integral is close to the exact value of 1/3.

Example 2: Approximating Pi

In this example, we’ll approximate the value of

tag. By integrating the function f(x) = 4/(1 + x^2) from 0 to 1, we can estimate pi.

import numpy as np
from scipy.integrate import trapezoid

# The function to integrate
f = lambda x: 4 / (1 + x**2)

# Points along the x-axis
x = np.linspace(0, 1, 1000)

# Function values
y = f(x)

# Integrate using the trapezoid rule
result = trapezoid(y, x)

print("Pi approximation: ", result)

Output:

Pi approximation:  3.1415924865892926

The calculated result gives us an approximation of pi, showcasing the trapezoid method’s applicability in calculating mathematical constants.

Example 3: Analyzing Performance for Small Intervals

Now, let’s explore how the trapezoid() function performs with a significantly smaller number of intervals. We’ll repeat the approximation of pi, but this time with only 10 intervals.

import numpy as np
from scipy.integrate import trapezoid

# The same function as before
f = lambda x: 4 / (1 + x**2)

# Only 10 points this time
x = np.linspace(0, 1, 10)

# Function values
y = f(x)

# Calculate integral
result = trapezoid(y, x)

print("Pi approximation with 10 intervals: ", result)

Output:

Pi approximation with 10 intervals:  3.139535044154283

The result is less accurate than the previous example, highlighting the importance of interval selection in numerical integration.

Example 4: Advanced Applications

Lastly, let’s explore a more complex scenario where the trapezoid() function can be useful. Suppose we’re interested in the kinetic energy
integral of a particle with variable acceleration over time. The kinetic energy can be calculated by integrating the acceleration function over a time interval.

import numpy as np
from scipy.integrate import trapezoid

# Time intervals and acceleration function
acceleration = lambda t: 2*t

duration = np.linspace(0, 10, 500)
values = acceleration(duration)

# Integrate to find the kinetic energy (assuming mass = 1 for simplicity)
kinetic_energy = trapezoid(values, duration)

print("Estimated kinetic energy: ", kinetic_energy)

Output:

Estimated kinetic energy:  100.0

This example demonstrates the potential for using the trapezoid method in physics and engineering applications for solving real-world problems.

Conclusion

The trapezoid() function in SciPy is a versatile tool for numerical integration. Through these examples, from basic to advanced, we’ve seen its application in mathematics, science, and engineering. Whether you’re approximating constants, analyzing performance, or solving domain-specific problems, the trapezoid() rule provides a straightforward and efficient approach to numerical integration.