5 Best Ways to Integrate Using the Composite Trapezoidal Rule in Python

Rate this post

πŸ’‘ Problem Formulation: In numerical analysis, integration of functions along a given axis is often required. Python, being a robust language for scientific computing, allows integration using the composite trapezoidal rule. This article presents five methods to perform such integration over a set of discrete data points or a continuous function. For example, given a function f(x) = x^2, we aim to find the integral from a to b using discretely spaced points along the x-axis.

Method 1: Using NumPy’s trapz Function

The NumPy library in Python provides a convenient trapz function which applies the trapezoidal rule to integrate, given a function’s values at points along an axis. It is suited for quickly integrating data that is evenly or unevenly spaced.

Here’s an example:

import numpy as np

# Define the sample points and their corresponding function values.
x = np.linspace(0, 10, 100)
y = x**2

# Perform integration using the trapezoidal rule.
area = np.trapz(y, x)


Output: 333.3503384008434

This code snippet demonstrates the integration of the function f(x) = x^2 between 0 and 10 using 100 sample points. The result approximates the integral of f(x) over the range [0, 10] using the trapezoidal rule.

Method 2: Implementing the Trapezoidal Rule Manually

A manual implementation allows more control over the integration process and is a great way to understand the underlying mathematics. It is especially useful for educational purposes or when a custom integration routine is needed.

Here’s an example:

def trapezoidal_rule(f, a, b, n):
    h = (b - a) / n
    s = 0.5 * (f(a) + f(b))
    for i in range(1, n):
        s += f(a + i * h)
    return h * s

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

# Compute the area under the curve
area = trapezoidal_rule(f, 0, 10, 100)


Output: 333.83350000000004

In this code snippet, a custom function trapezoidal_rule integrates the function f(x) = x^2 using the trapezoidal rule. The function takes the integration limits a, b, and the number of intervals n as its arguments.

Method 3: Using SciPy’s integrate Module

SciPy’s integrate module provides higher-order integration techniques, like the trapz method, which is deeper and more versatile than NumPy’s corresponding function. This method is suitable for high precision tasks in scientific computing.

Here’s an example:

from scipy.integrate import trapz
import numpy as np

x = np.linspace(0, 10, 100)
y = x**2

area = trapz(y, x)


Output: 333.3503384008434

This code snippet illustrates the usage of SciPy’s trapz method which is functionally similar to NumPy’s trapz but resides in a module with more comprehensive integration functions.

Method 4: Utilizing SymPy for Symbolic Integration

SymPy is a Python library for symbolic mathematics. It enables the symbolic formulation of the trapezoidal rule, which can be beneficial when working with symbolic expressions or when an exact result is required.

Here’s an example:

from sympy import symbols, integrate, lambdify

x = symbols('x')
f = x**2

# Define the trapezoidal rule
a, b, n = 0, 10, 100
h = (b - a) / n
s = 0.5 * (f.subs(x, a) + f.subs(x, b))
for i in range(1, n):
    s += f.subs(x, a + i * h)
area = h * s

# Convert symbolic expression to numerical value
area_num = lambdify(x, area)()


Output: 333.500000000000

Here, SymPy is used to integrate f(x) = x^2 symbolically using the trapezoidal rule. The final integration result is converted from a symbolic expression to a numerical value.

Bonus One-Liner Method 5: Using List Comprehension

Python’s list comprehension can compactly express the trapezoidal rule. This method is concise, but might be less readable for those not familiar with list comprehensions.

Here’s an example:

area = ((b - a) / (2 * n)) * (f(a) + 2*sum(f(a + i*h) for i in range(1, n)) + f(b))

Output: As with the manual implementation, the result would be approximately 333.83.

The one-liner code demonstrates how Python’s powerful list comprehension feature can be harnessed to implement the trapezoidal rule in a compact form. It calculates the area under the curve for the function f(x) = x^2.


  • Method 1: NumPy’s trapz. Straightforward. Efficient for quick calculations with arrays. Not for symbolic integration.
  • Method 2: Manual Implementation. Customizable. Educational. Requires more coding than library functions.
  • Method 3: SciPy’s integrate module. Precision. Part of a comprehensive suite of integration tools. Overkill for simple tasks.
  • Method 4: SymPy for Symbolic Integration. Symbolically exact. Useful for algebraic manipulations. Slower for numerical integrations.
  • Method 5: One-Liner List Comprehension. Concise. Clever use of Python syntax. May sacrifice readability.