---
title: Numerical integration
heading: Integrating what you can't integrate, and what you only measured
eyebrow: A5 · Lesson 46 · Numerical integration
xp: 50
---

::: hook
Most integrals have no formula. $\int_0^1 e^{-x^2}dx$, which describes noise and errors, can't be written with the functions you know. And a lot of what engineers integrate isn't a function at all: it's a list of measurements, power sampled every half second or current read by an ADC. The answer in both cases is to add up slices cleverly. How the slices are shaped decides how many you need.
:::

::: play
- The curve is $e^{-x^2}$ on $[0, 1]$. The shaded shapes are $n$ trapezoids, each joining two neighboring points with a straight line.
- Increase $n$. The error readout falls; watch by how much each time $n$ doubles.
- The trapezoids sit below the curve where it bends down and above where it bends up. Which happens here?
:::

```sim plot
view: graph
x: x = 0..1
y: y = 0..1.1
slider: n = 1..16 step 1 = 4
plot: exp(-x^2) | samples: 300 | label: e^(-x²)
plot: exp(-(floor(x*n)/n)^2) + (exp(-((floor(x*n) + 1)/n)^2) - exp(-(floor(x*n)/n)^2))*(x*n - floor(x*n)) | samples: 600 | color: ch2 | label: trapezoids
readout: trapezoid error, estimated = (1/n)^2/12*(-2*exp(-1)) | digits: 3
```

::: predict
## The trapezoid rule's error on a smooth curve is about 0.0038 with 4 slices. About what is it with 8?

- [ ] 0.0019
- [x] 0.00096
- [ ] 0.00024
- [ ] 0.0038, since it doesn't depend on the number of slices

::: reveal
About 0.00096, a quarter as much. The trapezoid rule's error goes as $h^2$, the square of the slice width, so halving $h$ cuts the error by 4. Simpson's rule, which fits parabolas instead of lines, does far better: its error goes as $h^4$, falling 16 times when $h$ halves.
:::
:::

::: nameit
## Three rules

Split $[a, b]$ into $n$ slices of width $h = (b - a)/n$, with points $x_k = a + kh$.

- **Trapezoid:** join neighboring points with lines.
$$T_n = h\left(\tfrac12f(x_0) + f(x_1) + \cdots + f(x_{n-1}) + \tfrac12f(x_n)\right)$$
- **Midpoint:** use each slice's center height, $M_n = h\sum f\left(x_k + \tfrac h2\right)$.
- **Simpson** ($n$ even): fit a parabola through each pair of slices.
$$S_n = \frac h3\left(f(x_0) + 4f(x_1) + 2f(x_2) + 4f(x_3) + \cdots + 4f(x_{n-1}) + f(x_n)\right)$$

## How their errors shrink

For a smooth function, the errors are about

$$E_T \approx -\frac{(b - a)h^2}{12}f'', \qquad E_M \approx \frac{(b - a)h^2}{24}f'', \qquad E_S \propto h^4$$

with $f''$ an average second derivative. So the trapezoid and midpoint errors have opposite signs, with the midpoint's half as big, and both fall by 4 when $h$ halves. Simpson's falls by 16. If $f$ bends down ($f'' < 0$), trapezoids sit below the curve and underestimate.

## Estimating the error from the answers

Compute $T_n$ and $T_{2n}$. Since the error falls by 4, the error in $T_{2n}$ is about $\frac{T_{2n} - T_n}{3}$, and adding it on gives $\frac{4T_{2n} - T_n}{3}$, which is exactly Simpson's rule. This is Richardson extrapolation: use how the answer changes to correct it.

## Sampled data

Measured data comes on a fixed grid, so you can't choose $h$. The trapezoid rule works on any spacing. Simpson needs an even number of equal slices. Either way, the samples have to be close enough to catch every bump in the signal: no rule can integrate what fell between the samples.
:::

::: example
## Estimate $\int_0^1 e^{-x^2}dx$ (exactly 0.746 824) with $n = 4$ by the trapezoid, midpoint and Simpson rules.

::: solution
1. **Apply the three rules with $h = 0.25$.**

| Rule | Estimate | Error |
|---|---|---|
| Trapezoid | 0.742 984 | $-0.003\,84$ |
| Midpoint | 0.748 747 | $+0.001\,92$ |
| Simpson | 0.746 855 | $+0.000\,031$ |

2. **Compare the trapezoid and midpoint rules.** The midpoint error is half the trapezoid error, with the opposite sign: the curve bends down here, so trapezoids sit low and midpoint rectangles sit high.
3. **Compare Simpson's rule.** At the same cost, it's a hundred times better.

**Answer:** the trapezoid rule gives 0.742 984, the midpoint rule 0.748 747 and Simpson's rule 0.746 855, within 0.000 031 of the exact value.
:::
:::

::: example
## With $n = 8$, the trapezoid rule gives 0.745 866. Use it and the $n = 4$ value, 0.742 984, to estimate the error and correct it.

::: solution
1. **Estimate the error in $T_8$.**
   $$\begin{aligned} \frac{T_8 - T_4}{3} &= \frac{0.002\,882}{3} \\ &= 0.000\,961 \end{aligned}$$
   and the true error is 0.000 959.
2. **Correct it.**
   $$T_8 + 0.000\,961 = 0.746\,826$$
   within 0.000 002 of the exact answer.
3. **Compare with Simpson's rule.** The corrected value is identical to Simpson's rule with $n = 8$.

**Answer:** the error in $T_8$ is about 0.000 961, and the corrected value is 0.746 826.
:::
:::

::: example
## A logger records a heater's power every 0.5 s: 0, 2.0, 3.5, 4.2, 4.0, 3.1, 1.8, 0.6, 0 W. How much energy did it use?

::: solution
1. **Trapezoid rule.**
   $$\begin{aligned} E &\approx 0.5\big(\tfrac12(0 + 0) + 2.0 + 3.5 \\ &\quad + 4.2 + 4.0 + 3.1 + 1.8 + 0.6\big) \\ &= 0.5 \times 19.2 \\ &= 9.6\ \text{J} \end{aligned}$$
2. **Simpson's rule, with 8 equal slices.**
   $$\begin{aligned} E &\approx \frac{0.5}{3}\big(0 + 4 \times 2.0 + 2 \times 3.5 \\ &\quad + 4 \times 4.2 + 2 \times 4.0 \\ &\quad + 4 \times 3.1 + 2 \times 1.8 \\ &\quad + 4 \times 0.6 + 0\big) \\ &= 9.7\ \text{J} \end{aligned}$$
3. **Compare.** The two agree to 1%. With measured data, that agreement is the best available check: if they differed a lot, the sampling would be too coarse.

**Answer:** the heater used about 9.6 J by the trapezoid rule, or 9.7 J by Simpson's rule.
:::
:::

::: check
given: e = 0.004 | 0.01 | 0.05
answer: e/4
tolerance: 0.5%

## The trapezoid rule gives an error of {e} on a smooth integral with $n$ slices. About what will its error be with $2n$?

::: right
Right. The error goes as $h^2$, so it falls by 4: about {answer}.
:::

::: misses
- [e/2] The error goes as $h^2$, so halving $h$ quarters it.
- [e/16] That's Simpson's rule, whose error goes as $h^4$.
- [any] Not yet. How does the trapezoid error depend on $h$?
:::
:::

::: check
given: e = 0.0001 | 0.002 | 0.03
answer: e/16
tolerance: 0.5%

## Simpson's rule has an error of {e} with $n$ slices. About what will it be with $2n$?

::: right
Right. Simpson's error goes as $h^4$, so it falls by 16: about {answer}.
:::

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

::: check
given: a = 0.5 | 1 | 2; b = 1 | 3
answer: 0.5*(a*0^2 + a*b^2)*b
tolerance: 0.5%

## Use the trapezoid rule with one slice for $\int_0^{\given{b}}\given{a}x^2\,dx$.

::: right
Right. $\frac{b}{2}(f(0) + f(b))$ = {answer}, an overestimate since the curve bends up.
:::

::: misses
- [a*b^3/3] That's the exact integral. The question asks for the one-slice trapezoid.
- [any] Not yet. One trapezoid: width times the average of the end heights.
:::
:::

::: check
given: T1 = 0.743 | 1.896 | 2.609; d = 0.025 | 0.0835 | 0.123
answer: T1 + d + d/3
tolerance: 0.01%

## The trapezoid rule gives {T1} with $n$ slices and $\given{T1} + \given{d}$ with $2n$. What does Richardson extrapolation give?

::: right
Right. $T_{2n} + \frac{T_{2n} - T_n}{3}$ = {answer}.
:::

::: misses
- [T1 + d + d/4] Divide the change by 3: $4 - 1$, from the error falling by 4.
- [T1 + d] That's $T_{2n}$ itself, uncorrected.
- [any] Not yet. The error in $T_{2n}$ is about a third of the change.
:::
:::

::: check
given: h = 0.1 | 0.25 | 0.5; p1 = 3 | 4; p2 = 5 | 7
answer: h/3*(0 + 4*p1 + p2)
unit: J
tolerance: 0.5%

## Power samples taken every {h} s read 0, {p1} and {p2} W. What energy does Simpson's rule give for those two slices?

::: right
Right. $\frac h3(f_0 + 4f_1 + f_2)$ = {answer}.
:::

::: misses
- [h*(0/2 + p1 + p2/2)] That's the trapezoid rule.
- [h/3*(0 + 2*p1 + p2)] The middle point gets weight 4.
- [any] Not yet. Weights 1, 4, 1, times $h/3$.
:::
:::

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

## Integrate $\int_0^2 x^3\,dx$ (exactly 4) with {n} slices.

::: part
answer: 4 + 4/n^2
tolerance: 0.1%

### What does the trapezoid rule give?

::: misses
- [4] That's the exact value; the trapezoid rule overestimates this curve.
- [any] $h(\tfrac12f_0 + f_1 + \cdots + \tfrac12f_n)$ with $h = 2/n$.
:::
:::

::: part
answer: 4
tolerance: 0.1%

### What does Simpson's rule give?

::: misses
- [any] Weights 1, 4, 2, 4, 1 times $h/3$. (It's exact for cubics.)
:::
:::
:::

::: check
kind: derivation
vars: a = -3..3, b = -3..3, c = -3..3, h = 0.1..2

## Show that Simpson's rule is exact for any parabola. Use $f(x) = ax^2 + bx + c$ on $[-h, h]$.

::: step
answer: 2*a*h^3/3 + 2*c*h

### What is the exact integral $\int_{-h}^{h}f\,dx$?
:::

::: step
answer: h/3*(a*h^2 - b*h + c + 4*c + a*h^2 + b*h + c)

### Write Simpson's formula, $\frac h3(f(-h) + 4f(0) + f(h))$, with the values put in.
:::

::: step
answer: 2*a*h^3/3 + 2*c*h
form: simplified

### Simplify it.
:::

::: right
Right. Both give $\frac{2ah^3}{3} + 2ch$, so Simpson's rule is exact for parabolas. By symmetry it's exact for cubics too, which is why its error goes as $h^4$.
:::
:::

::: explain
## Why do the trapezoid and midpoint rules err in opposite directions, and how does Simpson's rule use that?

::: model
On a curve that bends one way, say concave down, a straight chord between two points lies under the curve, so trapezoids underestimate. A midpoint rectangle has the curve's height at the center, and on a curve bending down that rectangle sticks out above the curve near its ends more than it falls short in the middle, so it overestimates. For a smooth curve the two errors are $-\frac{h^2}{12}$ and $+\frac{h^2}{24}$ times the same average curvature: opposite signs, with the midpoint's error half as big. A weighted average, $\frac{2M + T}{3}$, cancels that leading error, and that combination is exactly Simpson's rule. What's left is a much smaller error that goes as $h^4$.
:::
:::

::: practice

::: check
given: e = 0.00001..0.1
answer: e/4
tolerance: 0.5%

## Trapezoid error {e} with $n$ slices; with $2n$?

::: misses
- [any] It falls by 4.
:::
:::

::: check
given: T1 = 1..10; d = 0.001..0.1
answer: T1 + d + d/3
tolerance: 0.01%

## Richardson: $T_n$ = {T1}, $T_{2n}$ = $\given{T1} + \given{d}$?

::: misses
- [any] $T_{2n} + (T_{2n} - T_n)/3$.
:::
:::
:::
