Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 24

Trapezoidal Rule for Numerical Integration in Python

Recommended resources

Hello! Welcome to your final lesson in our module on Calculus for Optimization.

In our last session, we explored the Fundamental Theorem of Calculus (FTC), which gives us a powerful method to find the exact area under a curve by finding an antiderivative. However, this analytical method has a big limitation: what if we can't find a clean antiderivative for a function? Or what if we don't even have a function, but just a series of measured data points?

This is where numerical methods come in. Today, our goal is to implement numerical integration in Python using the trapezoidal rule. We will learn how to approximate definite integrals by dividing the area under a curve into trapezoids and summing their areas. This skill is incredibly useful in science and engineering when exact solutions are out of reach.

1. From Rectangles to Trapezoids: A Better Approximation

Recall from our last lesson that the concept of an integral is built on the idea of summing the areas of infinitely many, infinitesimally thin rectangles under a curve (a Riemann sum). Numerical integration takes this idea but uses a finite number of shapes to approximate the area.

While rectangles are simple, they can lead to significant errors, either over- or under-shooting the curve. We can get a much better approximation by using trapezoids instead.

Numerical Integration: Discrete Riemann Integrals and Trapezoid Rule

To understand why trapezoids are a better choice and how they improve accuracy, watch this segment from a video by Steve Brunton. It visually compares the rectangle method to the trapezoid rule and explains where the error comes from.

Watch the video from 09:55 to 14:00. Focus on: The visual comparison between the rectangle rule and the trapezoid rule. The idea that the trapezoid rule is essentially the average of the left- and right-sided rectangle rules. The explanation that the error in the trapezoid rule is related to the curvature (second derivative) of the function, whereas the error in the rectangle rule is related to the slope (first derivative).

As the video explains, by fitting the top of our shape to the curve at both ends of the interval, we hug the function more closely, leading to a more accurate estimate of the area.


2. The Formula for the Trapezoidal Rule

Let's formalize this idea. To approximate the integral , we divide the interval into smaller subintervals, each of width . Each of these subintervals forms the base of a trapezoid.

The area of a single trapezoid over a subinterval is given by the average of the heights (the function values at the endpoints) multiplied by the width:

To find the total approximate area, we sum the areas of all trapezoids.

Trapezoid Rule - Mathematical Python

This article from the 'Mathematical Python' series provides a clear definition and mathematical formula for the trapezoid rule.

Read the first two sections, 'Trapezoids' and 'Definition'. Pay close attention to the final formula for T_N(f) which sums up all the individual trapezoid areas.

After summing all the trapezoid areas and collecting terms, we arrive at the general formula for the trapezoid rule:

Notice that the first and last function values are used once, while all the interior values are used twice (as they are part of two adjacent trapezoids).


3. Implementing the Trapezoidal Rule in Python

Now let's translate this formula into code. Your background in Python and NumPy from the first module will be very helpful here. We'll write a function that takes a function f, the interval [a, b], and the number of trapezoids N as arguments.

Trapezoid Rule - Mathematical Python

The 'Mathematical Python' article provides an excellent, concise implementation of the trapezoid rule using NumPy. We will use this as our guide.

Focus on the 'Implementation' section. Read through the Python code for the trapz function and its explanation. Note how it uses np.linspace to create the points and array slicing (y[1:], y[:-1]) to efficiently sum the left and right endpoints of the trapezoids.

Let's break down how to build this function step-by-step.

import numpy as np

def trapezoid_rule(f, a, b, N):
    """
    Approximates the definite integral of f(x) from a to b
    using the trapezoidal rule with N subintervals.
    """
    # 1. Calculate the width of the trapezoids
    dx = (b - a) / N
    
    # 2. Create an array of N+1 points from a to b
    x = np.linspace(a, b, N + 1)
    
    # 3. Evaluate the function at these points
    y = f(x)
    
    # 4. Apply the trapezoidal rule formula
    # T = (dx/2) * (y_0 + 2y_1 + ... + 2y_{N-1} + y_N)
    # This can be implemented efficiently by summing all y values,
    # and then adding the interior points once more.
    
    integral_approx = (dx / 2) * (y[0] + y[-1] + 2 * np.sum(y[1:-1]))
    
    return integral_approx

Let's test it on an integral we can solve analytically. From our last lesson, we know .

# Define the function to integrate
def my_func(x):
    return 3 * x**2

# Approximate the integral
approx_val = trapezoid_rule(my_func, 0, 1, 100)
print(f"Approximation: {approx_val}")
print(f"Exact value: 1")

Running this code, you'll see the approximation is very close to 1!

Test your understanding!

Use the trapezoid_rule function we just wrote to approximate the integral . We know the exact answer is 1.

Try running it with N=10 and then N=1000. What do you observe about the accuracy?

Show answer
# First, define the function using numpy's sin
def sin_func(x):
    return np.sin(x)

# With N=10
approx_10 = trapezoid_rule(sin_func, 0, np.pi/2, 10)
print(f"Approximation with N=10: {approx_10}")

# With N=1000
approx_1000 = trapezoid_rule(sin_func, 0, np.pi/2, 1000)
print(f"Approximation with N=1000: {approx_1000}")

You should observe that the approximation with N=1000 is significantly closer to the true value of 1 than the approximation with N=10. This demonstrates that as we increase the number of trapezoids, our approximation becomes more accurate.


4. Error Analysis and Using Library Functions

A key part of numerical methods is understanding the error. As we saw, increasing N reduces the error. The following video provides a complete walkthrough of implementing the trapezoid rule and, importantly, visualizing how the error decreases as N increases.

Numerical Integration Using Trapezoidal Rule in Python | PythonCodeMan

This video from Python CodeMan does a great job of putting everything together: the concept, the Python implementation, and an analysis of the error.

Watch the entire video (about 23 minutes). It will reinforce what we've learned and show you: A slightly different (but equivalent) implementation of the trapezoid rule. How to calculate the exact integral value to compute the relative error. How to create a loop to run the approximation for different values of N. How to plot the error versus N to see the relationship visually (including a log-log plot).

While implementing algorithms from scratch is fantastic for learning, in practice, you'll often use highly optimized functions from libraries like NumPy or SciPy. Both provide a trapz function.

Here's how you would use NumPy's trapz function to do the same calculation. It's even simpler because the function deduces the interval width from the x values you provide.

import numpy as np

# Define the function, limits, and N
f = lambda x: 3 * x**2
a, b, N = 0, 1, 100

# Create the x and y data points
x_vals = np.linspace(a, b, N + 1)
y_vals = f(x_vals)

# Use numpy's built-in function
# Note: The order is y, then x.
numpy_approx = np.trapz(y_vals, x_vals)

print(f"NumPy's trapz approximation: {numpy_approx}")

As you can see, libraries make the process very straightforward, but now you understand exactly what's happening under the hood. The resource "Implementing The Trapezium Rule With Python" (ID: LINK) even includes a performance benchmark showing how much faster these library functions are compared to a pure Python implementation, especially for large N.


Conclusion

Congratulations on completing this lesson and the module on Calculus! You now have a solid foundation in both the analytical and numerical sides of integration.

Key Takeaways:

  • Numerical integration is used to approximate definite integrals when an analytical solution is difficult or impossible, or when you only have data points.
  • The trapezoidal rule improves on simple rectangle-based approximations by fitting the curve more closely, reducing error.
  • The formula for the trapezoidal rule is a weighted sum of the function's values at evenly spaced points:
  • Increasing the number of trapezoids (N) increases the accuracy of the approximation.
  • We can implement the rule from scratch in Python, but for practical applications, optimized functions like numpy.trapz are preferred for their simplicity and performance.

Preview of the Next Module:

We have now covered the essentials of Python programming, Linear Algebra, and Calculus—three major pillars of data science and AI. You're well-equipped to move on to the next one: Probability and Statistics. In our next lesson, we will start with the fundamentals of counting by learning to apply fundamental counting principles, including permutations and combinations. These concepts form the bedrock for calculating probabilities.