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

A5 · Lesson 46 · Numerical integration

Integrating what you can't integrate, and what you only measured

Most integrals have no formula. ∫01e−x2dx, 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.

  • The curve is e−x2 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?

Predict

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

Name it

Three rules

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

  • Trapezoid: join neighboring points with lines.
Tn=h(12f(x0)+f(x1)+⋯+f(xn−1)+12f(xn))
  • Midpoint: use each slice's center height, Mn=h∑f(xk+h2).
  • Simpson (n even): fit a parabola through each pair of slices.
Sn=h3(f(x0)+4f(x1)+2f(x2)+4f(x3)+⋯+4f(xn−1)+f(xn))

How their errors shrink

For a smooth function, the errors are about

ET≈−(b−a)h212f′′,EM≈(b−a)h224f′′,ES∝h4

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 Tn and T2n. Since the error falls by 4, the error in T2n is about T2n−Tn3, and adding it on gives 4T2n−Tn3, 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.

Worked example

Estimate ∫01e−x2dx (exactly 0.746 824) with n=4 by the trapezoid, midpoint and Simpson rules.

Show the solution
  1. Apply the three rules with h=0.25.
RuleEstimateError
Trapezoid0.742 984−0.00384
Midpoint0.748 747+0.00192
Simpson0.746 855+0.000031
  1. 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.
  2. 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.

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

Show the solution
  1. Estimate the error in T8. T8−T43=0.0028823=0.000961 and the true error is 0.000 959.
  2. Correct it. T8+0.000961=0.746826 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 T8 is about 0.000 961, and the corrected value is 0.746 826.

Worked 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?

Show the solution
  1. Trapezoid rule. E≈0.5(12(0+0)+2.0+3.5+4.2+4.0+3.1+1.8+0.6)=0.5×19.2=9.6J
  2. Simpson's rule, with 8 equal slices. E≈0.53(0+4×2.0+2×3.5+4×4.2+2×4.0+4×3.1+2×1.8+4×0.6+0)=9.7J
  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

The trapezoid rule gives an error of 0.01 on a smooth integral with n slices. About what will its error be with 2n?

Check

Simpson's rule has an error of 0.002 with n slices. About what will it be with 2n?

Check

Use the trapezoid rule with one slice for ∫011x2dx.

Check

The trapezoid rule gives 1.896 with n slices and 1.896+0.025 with 2n. What does Richardson extrapolation give?

Check

Power samples taken every 0.25 s read 0, 3 and 7 W. What energy does Simpson's rule give for those two slices?

Write units the way you normally would: 1.5 J, 1500 mJ and 0.0015 kJ are the same answer.

Check

Integrate ∫02x3dx (exactly 4) with 4 slices.

(a)

What does the trapezoid rule give?

(b)

What does Simpson's rule give?

Check

Show that Simpson's rule is exact for any parabola. Use f(x)=ax2+bx+c on [−h,h].

Step 1

What is the exact integral ∫−hhfdx?

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 do the trapezoid and midpoint rules err in opposite directions, and how does Simpson's rule use that?

Practice

Keep practicing

8 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.