A13 · Lesson 17 · The trapezoid rule and Simpson's rule
Integrating from samples, and how fast the error falls
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.
- The shaded polygon is the trapezoid rule's picture of 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 .
- 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.
Predict
Eight equally spaced samples of over one full period: how accurately does the trapezoid rule give the mean square, ?
Exactly: . 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.
Name it
The rules
With n intervals of width and samples :
| Rule | Formula | Error |
|---|---|---|
| trapezoid | ||
| midpoint | ||
| Simpson (n even) |
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: .
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.
Worked example
Integrate from 0 to π with 4 intervals, by both rules.
Show the solution
- The samples. With they are 0, 0.7071, 1, 0.7071, 0.
- Trapezoid rule. error 0.104.
- Simpson's rule. error 0.0046.
- 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).
Worked example
A load's power is sampled every 10 ms as 0, 40, 100, 40, 0 W. How much energy did it use?
Show the solution
- Trapezoid rule.
- Simpson's rule.
- 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.
Worked example
The trapezoid rule with 4 intervals gives 1.727222 for . Correct it with the end-derivative term.
Show the solution
- The end correction. The Euler–Maclaurin formula says the trapezoid error is about
- Its size here. With and :
- Correct the value. against the exact 1.718282.
- The gain. The error fell from to for the cost of two derivative values.
- 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 of the exact 1.718282.
Check
What does the trapezoid rule with 4 intervals give for ?
Check
What does Simpson's rule with one panel (two intervals) give for ?
Check
Power samples 10 ms apart are 0, 30, 100, 30, 0 W. What energy does the trapezoid rule give?
Write units the way you normally would: 1.5 J, 1500 mJ and 0.0015 kJ are the same answer.
Check
On an interval of length 1 with 10 trapezoids, and 5, what's the worst-case error?
Check
By what factor does Simpson's error shrink when you use 3 times as many intervals?
Check
You must integrate a smooth periodic signal over exactly one period from 16 equally spaced samples. Which rule should you use?
Check
Estimate the errors of with 8 intervals, from the end-derivative formulas.
(a)
What is the trapezoid rule's error, ?
(b)
And Simpson's, ?
Check
Apply the trapezoid rule to on .
Step 1
What does it give?
Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.
Step 2
By how much does it exceed the exact integral?
Explain why
Why is Simpson's rule so much more accurate than the trapezoid rule for the same samples?
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 . 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 . With the same samples, the only change is how they're weighted: 1, 4, 2, 4, ..., 1 instead of ½, 1, 1, ..., ½.
Practice
Keep practicing
9 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.