A1317 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

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 ∫0πsin⁡xdx=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 h2.
  • 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 sin⁡t over one full period: how accurately does the trapezoid rule give the mean square, 12π∫02πsin2⁡tdt=12?

Name it

The rules

With n intervals of width h=(b−a)/n and samples fk=f(a+kh):

RuleFormulaError
trapezoidh(f02+f1+⋯+fn−1+fn2)−(b−a)h212f′′(ξ)
midpointh∑f(a+(k+12)h)+(b−a)h224f′′(ξ)
Simpson (n even)h3(f0+4f1+2f2+4f3+⋯+4fn−1+fn)−(b−a)h4180f(4)(ξ)

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: Sn=4Tn−Tn/23.

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 sin⁡x from 0 to π with 4 intervals, by both rules.

Show the solution
  1. The samples. With h=π/4 they are 0, 0.7071, 1, 0.7071, 0.
  2. Trapezoid rule. T=π4(0.7071+1+0.7071)=1.8961 error 0.104.
  3. Simpson's rule. S=π12(0+4×0.7071+2×1+4×0.7071+0)=2.0046 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).

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
  1. Trapezoid rule. E=0.01(0+40+100+40+0)=1.8J
  2. Simpson's rule. E=0.013(0+160+200+160+0)=1.733J
  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.

Worked example

The trapezoid rule with 4 intervals gives 1.727222 for ∫01exdx. Correct it with the end-derivative term.

Show the solution
  1. The end correction. The Euler–Maclaurin formula says the trapezoid error is about h212(f′(b)−f′(a))
  2. Its size here. With h=14 and f′(1)−f′(0)=e−1: 0.062512×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×10−3 to 9×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×10−6 of the exact 1.718282.

Check

What does the trapezoid rule with 4 intervals give for ∫01x2dx?

Check

What does Simpson's rule with one panel (two intervals) give for ∫02x4dx?

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 |f′′|≤ 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 ∫01exdx with 8 intervals, from the end-derivative formulas.

(a)

What is the trapezoid rule's error, h212(f′(1)−f′(0))?

(b)

And Simpson's, h4180(f′′′(1)−f′′′(0))?

Check

Apply the trapezoid rule to x2 on [a,a+h].

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°.

Explain why

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

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.