Reading history

Numerical Integration: Trapezoidal and Simpson’s Rules

4 min read 1 views

Approximate integrals with the trapezoidal rule and Simpson's rule, compare their accuracy, integrate measured data and code both rules in Python.

Many integrals have no formula. ∫ e^(−x²) dx, the bell curve of statistics, has no elementary antiderivative at all. And in real life you often have only a table of measurements, not a function. In both cases you can still find the integral to any accuracy you like using numerical integration. This lesson covers the two classic methods, the trapezoidal rule and Simpson’s rule, shows why Simpson’s rule is so much more accurate, and includes a short Python program that does the arithmetic for you.

In this lesson you will learn

  • The trapezoidal rule and its formula
  • Simpson’s rule, which fits parabolas instead of straight lines
  • How errors shrink as you use more strips
  • How to integrate a table of measured data
  • A short Python implementation of both rules

You should know Riemann sums and definite integrals. For root-finding methods, see bisection and Newton-Raphson.

The Trapezoidal Rule

Split [a, b] into n equal strips of width h = (b − a)/n, with points x₀, x₁, …, xₙ and heights yᵢ = f(xᵢ). Instead of rectangles, join neighbouring points with straight lines. Each strip becomes a trapezium of area h(yᵢ + yᵢ₊₁)/2. Adding them:

T = (h/2) [y₀ + 2y₁ + 2y₂ + ... + 2yₙ₋₁ + yₙ]
End heights count once; every inner height counts twice.
xy00.250.50.751y = e^(−x²)4 trapezia: T ≈ 0.7430True value 0.7468: the straight tops sit just under this mostly concave-down curve
∫₀¹ e^(−x²) dx with n = 4, h = 0.25
y: 1, 0.939413, 0.778801, 0.569783, 0.367879
T = 0.125 [1 + 2(0.939413 + 0.778801 + 0.569783) + 0.367879]
  = 0.125 × 5.943873 ≈ 0.742984

True value: 0.746824...   Error ≈ 0.0038

The trapezoidal rule is the average of the left and right Riemann sums. Its error shrinks like 1/n²: doubling the number of strips cuts the error by about 4.

Simpson’s Rule

Straight lines miss the curvature. Simpson’s rule fits a parabola through each group of three points, which matches curved graphs much better. n must be even.

S = (h/3) [y₀ + 4y₁ + 2y₂ + 4y₃ + 2y₄ + ... + 4yₙ₋₁ + yₙ]
Pattern of weights: 1, 4, 2, 4, 2, ..., 4, 1
Same integral, n = 4, h = 0.25
S = (0.25/3) [1 + 4(0.939413) + 2(0.778801) + 4(0.569783) + 0.367879]
  = 0.083333 × 8.962 ≈ 0.746855

Error ≈ 0.00003   (over 100 times smaller than the trapezoidal rule)

Simpson’s error shrinks like 1/n⁴: doubling the strips cuts the error by about 16. It is even exact for every cubic polynomial.

Comparing the Methods

nTrapezoidalSimpson’sTrue value
20.7313700.7471800.746824
40.7429840.746855
80.7458660.746826

Integrating Measured Data

A car’s speed is recorded every 10 seconds. How far does it travel in 60 seconds?

t (s):   0   10   20   30   40   50   60
v (m/s): 0    8   14   18   20   21   21

Simpson (n = 6, h = 10):
(10/3)[0 + 4(8) + 2(14) + 4(18) + 2(20) + 4(21) + 21]
= (10/3)(277) ≈ 923 m

No formula for v(t) was needed, just the readings. Engineers use exactly this to estimate river flow, land area from surveys and energy use from meter readings.

In Python

import math

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

def simpson(f, a, b, n):          # n must be even
    h = (b - a) / n
    s = f(a) + f(b)
    s += 4 * sum(f(a + i*h) for i in range(1, n, 2))
    s += 2 * sum(f(a + i*h) for i in range(2, n, 2))
    return h * s / 3

f = lambda x: math.exp(-x*x)
print(round(trapezoid(f, 0, 1, 4), 6))   # 0.742984
print(round(simpson(f, 0, 1, 4), 6))     # 0.746855

Common Mistakes

  1. Using an odd n with Simpson’s rule. It needs an even number of strips.
  2. Wrong weights: trapezoidal is 1, 2, 2, …, 2, 1; Simpson’s is 1, 4, 2, 4, …, 4, 1.
  3. Using h/2 for Simpson or h/3 for the trapezoidal rule.
  4. Counting points as strips. 7 data points make 6 strips.

Practice Exercises

Exercise 1. Use the trapezoidal rule with n = 2 to estimate ∫₀² x² dx.

Show answer

h = 1: ½[0 + 2(1) + 4] = 3 (true value 8/3).

Exercise 2. Use Simpson’s rule with n = 2 for the same integral.

Show answer

(1/3)[0 + 4(1) + 4] = 8/3, exact, because Simpson’s rule is exact for quadratics.

Exercise 3. Use Simpson’s rule with n = 4 to estimate ∫₁² (1/x) dx.

Show answer

h = 0.25: (0.25/3)[1 + 4(0.8) + 2(0.666667) + 4(0.571429) + 0.5] ≈ 0.693254 (true ln 2 ≈ 0.693147).

Frequently Asked Questions

What is the difference between the trapezoidal rule and Simpson’s rule?

The trapezoidal rule joins neighbouring points with straight lines, while Simpson’s rule fits a parabola through each set of three points. Simpson’s rule needs an even number of strips but is far more accurate for smooth functions, with an error that falls like 1 over n to the fourth power.

When do we need numerical integration?

When a function has no simple antiderivative, such as e to the minus x squared, or when you only have measured data in a table instead of a formula. Numerical methods give an approximate value as accurately as you need by using more strips.

Summary

  • Trapezoidal: (h/2)[y₀ + 2(inner) + yₙ]; error ∝ 1/n².
  • Simpson’s: (h/3)[y₀ + 4, 2, 4, … + yₙ]; n even; error ∝ 1/n⁴.
  • Simpson’s rule is exact for polynomials up to degree 3.
  • Both work on tables of measured data.
  • A few lines of code compute either rule.

Next: Area Between Two Curves.

Lessons are also marked complete when you click Next.

Leave a Reply

Your email address will not be published. Required fields are marked *