---
title: The trapezoid rule and Simpson's rule
heading: Integrating from samples, and how fast the error falls
eyebrow: A13 · Lesson 17 · The trapezoid rule and Simpson's rule
xp: 40
---

::: hook
A power meter samples voltage and current thousands of times a cycle and must report energy, an integral. A battery gauge integrates current to get charge. An oscilloscope's RMS readout integrates the square of the samples. All of them integrate from samples, and the simplest way is to join the samples with straight lines and add up the areas: the **trapezoid rule**. Fitting parabolas through pairs of intervals instead, **Simpson's rule**, is barely more work and far more accurate for smooth signals. And over a whole period of a periodic signal, the plain trapezoid rule turns out to be astonishingly exact.
:::

::: play
- The shaded polygon is the trapezoid rule's picture of $\int_0^\pi\sin x\,dx = 2$ with N intervals: straight lines between the samples.
- With N = 4 it misses by 0.10; double N and the error drops to a quarter, since it goes as $h^2$.
- The second readout is Simpson's rule with the same samples. It's already within 0.005 at N = 4 and drops 16 times with each doubling.
:::
:::

```sim plot
view: graph
x: x = 0..3.1416
y: y = 0..1.1
slider: N = 2..20 step 2 = 4
plot: sin(x) | label: sin x
plot: sin(floor(x/(pi/N))*(pi/N)) + (x - floor(x/(pi/N))*(pi/N))*(sin((floor(x/(pi/N)) + 1)*(pi/N)) - sin(floor(x/(pi/N))*(pi/N)))/(pi/N) | samples: 900 | fill: 0..pi | color: ch2 | label: trapezoids
readout: trapezoid error = 2 - (pi/N)/tan(pi/(2*N))
readout: Simpson error = 2 - (4*(pi/N)/tan(pi/(2*N)) - (2*pi/N)/tan(pi/N))/3
```

::: predict
## Eight equally spaced samples of $\sin t$ over one full period: how accurately does the trapezoid rule give the mean square, $\frac{1}{2\pi}\int_0^{2\pi}\sin^2t\,dt = \frac12$?

- [ ] To about 1%, since the error goes as $h^2$
- [ ] To about 0.01%
- [x] Exactly, apart from rounding
- [ ] Not at all: 8 samples are too few

::: reveal
Exactly: $\frac18\sum_{k=0}^7\sin^2\frac{2\pi k}{8} = 0.5$. Over a full period of a smooth periodic function, the trapezoid rule's errors cancel, and it's exact for every trigonometric polynomial of low enough degree (here, below 8). That's why a scope's RMS of a whole number of cycles is so accurate, and why the DFT works.
:::
:::

::: nameit
## The rules

With n intervals of width $h = (b - a)/n$ and samples $f_k = f(a + kh)$:

| Rule | Formula | Error |
|---|---|---|
| trapezoid | $h\left(\frac{f_0}{2} + f_1 + \cdots + f_{n-1} + \frac{f_n}{2}\right)$ | $-\frac{(b - a)h^2}{12}f''(\xi)$ |
| midpoint | $h\sum f(a + (k + \frac12)h)$ | $+\frac{(b - a)h^2}{24}f''(\xi)$ |
| Simpson (n even) | $\frac h3\left(f_0 + 4f_1 + 2f_2 + 4f_3 + \cdots + 4f_{n-1} + f_n\right)$ | $-\frac{(b - a)h^4}{180}f^{(4)}(\xi)$ |

Halving h cuts the trapezoid error by 4 and Simpson's by 16. Simpson's rule is exact for cubics, and it's Richardson extrapolation of the trapezoid rule: $S_n = \frac{4T_n - T_{n/2}}{3}$.

## Periodic functions

For a smooth periodic function integrated over a whole period, the trapezoid rule's error falls faster than any power of h: exponentially, for an analytic function. With n samples it's exact for every harmonic below the n-th. (The Bessel functions in A12's sims are computed this way.)

## In practice

- Sampled data: trapezoid or Simpson on the samples; you can't choose the points.
- A function you can evaluate anywhere: Gaussian quadrature (lesson 18) or adaptive methods (lesson 19).
- Noise in the samples averages out: integration is the well-conditioned opposite of differentiation.
:::

::: example
## Integrate $\sin x$ from 0 to π with 4 intervals, by both rules.

::: solution
1. **The samples.** With $h = \pi/4$ they are 0, 0.7071, 1, 0.7071, 0.
2. **Trapezoid rule.**
   $$\begin{aligned} T &= \frac\pi4(0.7071 + 1 + 0.7071) \\ &= 1.8961 \end{aligned}$$
   error 0.104.
3. **Simpson's rule.**
   $$\begin{aligned} S &= \frac{\pi}{12}\big(0 + 4 \times 0.7071 + 2 \times 1 \\ &\quad + 4 \times 0.7071 + 0\big) \\ &= 2.0046 \end{aligned}$$
   error 0.0046.
4. **Compare.** Simpson's error is 23 times smaller, with the same samples.

**Answer:** the trapezoid rule gives 1.8961 (error 0.104) and Simpson's rule 2.0046 (error 0.0046).
:::
:::

::: example
## A load's power is sampled every 10 ms as 0, 40, 100, 40, 0 W. How much energy did it use?

::: solution
1. **Trapezoid rule.**
   $$\begin{aligned} E &= 0.01(0 + 40 + 100 + 40 + 0) \\ &= 1.8\ \text{J} \end{aligned}$$
2. **Simpson's rule.**
   $$\begin{aligned} E &= \frac{0.01}{3}(0 + 160 + 200 \\ &\quad + 160 + 0) \\ &= 1.733\ \text{J} \end{aligned}$$
3. **Compare.** They disagree by 4%, so the sampling is coarse for this pulse.

**Answer:** about 1.8 J by the trapezoid rule and 1.733 J by Simpson's rule; the 4% gap says the sampling is coarse.
:::
:::

::: example
## The trapezoid rule with 4 intervals gives 1.727222 for $\int_0^1e^x\,dx$. Correct it with the end-derivative term.

::: solution
1. **The end correction.** The Euler–Maclaurin formula says the trapezoid error is about
   $$\frac{h^2}{12}\left(f'(b) - f'(a)\right)$$
2. **Its size here.** With $h = \frac14$ and $f'(1) - f'(0) = e - 1$:
   $$\frac{0.0625}{12} \times 1.71828 = 0.008949$$
3. **Correct the value.**
   $$1.727222 - 0.008949 = 1.718273$$
   against the exact 1.718282.
4. **The gain.** The error fell from $8.9 \times 10^{-3}$ to $9 \times 10^{-6}$ for the cost of two derivative values.
5. **Periodic functions.** For a periodic function over a period the end terms cancel, which is why the plain rule is so accurate there.

**Answer:** the corrected value is 1.718273, within $9 \times 10^{-6}$ of the exact 1.718282.
:::
:::

::: check
given: n = 2 | 4 | 10
answer: 1/3 + 1/(6*n^2)
tolerance: 0.01%

## What does the trapezoid rule with {n} intervals give for $\int_0^1x^2\,dx$?

::: right
Right. $\frac13 + \frac{1}{6n^2}$ = {answer}: its error is exactly $\frac{h^2}{12}f'' \times (b - a)$, since f″ is constant.
:::

::: misses
- [1/3] That's the exact integral; the trapezoid rule overestimates a convex function.
- [any] Not yet. Error $= \frac{(b - a)h^2}{12}f'' = \frac{2}{12n^2}$.
:::
:::

::: check
given: b = 1 | 2 | 3
answer: 5*b^5/24
tolerance: 0.01%

## What does Simpson's rule with one panel (two intervals) give for $\int_0^{\given{b}}x^4\,dx$?

::: right
Right. $\frac b6\left(0 + 4(b/2)^4 + b^4\right) = \frac{5b^5}{24}$ = {answer}, against the exact $b^5/5$.
:::

::: misses
- [b^5/5] That's the exact value; Simpson's rule is exact only up to cubics.
- [any] Not yet. $\frac{h}{3}(f_0 + 4f_1 + f_2)$ with $h = b/2$.
:::
:::

::: check
given: dt = 10 | 5; a = 30 | 40; c = 90 | 100
answer: dt/1000*(2*a + c)
unit: J
tolerance: 0.5%

## Power samples {dt} ms apart are 0, {a}, {c}, {a}, 0 W. What energy does the trapezoid rule give?

::: right
Right. {answer} J.
:::

::: misses
- [dt/1000*(8*a + 2*c)/3] That's Simpson's rule; the question asks for the trapezoid rule.
- [any] Not yet. $h\left(\frac{f_0}{2} + f_1 + f_2 + f_3 + \frac{f_4}{2}\right)$.
:::
:::

::: check
given: L = 1 | 2; n = 10 | 100; M = 1 | 5
answer: L^3*M/(12*n^2)
tolerance: 0.5%

## On an interval of length {L} with {n} trapezoids, and $\lvert f''\rvert \le$ {M}, what's the worst-case error?

::: right
Right. $\frac{(b - a)h^2}{12}M = \frac{L^3M}{12n^2}$ = {answer}.
:::

::: misses
- [L^3*M/(24*n^2)] That's the midpoint rule's bound.
- [any] Not yet. $\frac{(b - a)h^2}{12}\max\lvert f''\rvert$ with $h = L/n$.
:::
:::

::: check
given: k = 2 | 3 | 10
answer: k^4
tolerance: 0.5%

## By what factor does Simpson's error shrink when you use {k} times as many intervals?

::: right
Right. {answer}.
:::

::: misses
- [k^2] That's the trapezoid rule's rate.
- [any] Not yet. Simpson's error goes as $h^4$.
:::
:::

::: check
kind: choice

## You must integrate a smooth periodic signal over exactly one period from 16 equally spaced samples. Which rule should you use?

- [x] The trapezoid rule: over a full period it's far more accurate than its $h^2$ error suggests
- [ ] Simpson's rule, because it's always more accurate
- [ ] The midpoint rule
- [ ] Any of them; they're the same here

::: misses
- [2] Simpson's end weights spoil the periodic cancellation; here the plain trapezoid rule wins.
- [3] The samples are at the ends of intervals, not the middles.
- [4] They aren't: only the trapezoid rule treats every sample alike over a period.
:::
:::

::: check
kind: parts
given: n = 4 | 8 | 10

## Estimate the errors of $\int_0^1e^x\,dx$ with {n} intervals, from the end-derivative formulas.

::: part
answer: (1/n)^2*(exp(1) - 1)/12
tolerance: 1%

### What is the trapezoid rule's error, $\frac{h^2}{12}\left(f'(1) - f'(0)\right)$?

::: misses
- [(1/n)^2*(exp(1) - 1)/6] The coefficient is $\frac{1}{12}$.
- [any] $\frac{h^2}{12}(e - 1)$.
:::
:::

::: part
answer: (1/n)^4*(exp(1) - 1)/180
tolerance: 1%

### And Simpson's, $\frac{h^4}{180}\left(f'''(1) - f'''(0)\right)$?

::: misses
- [any] $\frac{h^4}{180}(e - 1)$.
:::
:::
:::

::: check
kind: derivation
vars: a = -2..2, h = 0.1..1

## Apply the trapezoid rule to $x^2$ on $[a, a + h]$.

::: step
answer: h*(a^2 + (a + h)^2)/2

### What does it give?
:::

::: step
answer: h^3/6

### By how much does it exceed the exact integral?
:::

::: right
Right. The error is $\frac{h^3}{12}f''$ with $f'' = 2$, the same on every interval.
:::
:::

::: explain
## Why is Simpson's rule so much more accurate than the trapezoid rule for the same samples?

::: model
The trapezoid rule fits a straight line through each pair of neighboring samples, so it gets the area exactly only when the function is linear; its error comes from the curvature, f″, and shrinks as $h^2$. Simpson's rule fits a parabola through each three consecutive samples. By symmetry, the parabola's error from a cubic term cancels across each pair of intervals, so the rule is exact for cubics, and its error comes from the fourth derivative, shrinking as $h^4$. With the same samples, the only change is how they're weighted: 1, 4, 2, 4, ..., 1 instead of ½, 1, 1, ..., ½.
:::
:::

::: practice

::: check
given: n = 2 | 4 | 5 | 10 | 20 | 50
answer: 1/3 + 1/(6*n^2)
tolerance: 0.01%

## Trapezoid with {n} intervals for $\int_0^1x^2$?

::: misses
- [any] $\frac13 + \frac{1}{6n^2}$.
:::
:::

::: check
given: b = 0.5 | 1 | 2 | 3 | 4 | 5
answer: 5*b^5/24
tolerance: 0.01%

## One-panel Simpson for $\int_0^{\given{b}}x^4$?

::: misses
- [any] $5b^5/24$.
:::
:::

::: check
given: L = 3 | 0.5; n = 20 | 50; M = 2 | 8
answer: L^3*M/(12*n^2)
tolerance: 0.5%

## Trapezoid error bound, length {L}, {n} intervals, $\lvert f''\rvert \le$ {M}?

::: misses
- [any] $L^3M/(12n^2)$.
:::
:::
:::
