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

A6 · Lesson 11 · Numerical integration

When there's no formula, add up slices, and know how wrong you are

The energy in a recorded waveform, the charge from a measured current, the area under e−x2: none of these have a formula you can write down. You integrate them numerically, by adding up slices. The interesting part is the error. A good method doesn't just get closer as the slices get thinner; it gets closer at a predictable rate, which tells you how many slices you need.

Predict

The trapezoid rule's error shrinks in proportion to h2, the slice width squared. You halve h. What happens to the error?

Name it

Three rules, from crude to good

Split [a,b] into n slices of width h=(b−a)/n, with points x0=a,x1,…,xn=b.

  • Rectangles (left-hand sum): h∑k=0n−1f(xk). Error proportional to h: first order.
  • Trapezoids, joining neighboring points with straight lines:
∫abfdx≈h[12f(x0)+f(x1)+⋯+f(xn−1)+12f(xn)]

Error about −(b−a)h212f′′: second order.

  • Simpson's rule, with n even, fits a parabola through each pair of slices:
∫abfdx≈h3[f(x0)+4f(x1)+2f(x2)+4f(x3)+⋯+4f(xn−1)+f(xn)]

Error proportional to h4: fourth order.

Checking the order. Compute with n slices and with 2n. If the method has order p, the error should drop by about 2p. Measuring that ratio is how numerical code gets tested, and it's how this course's own circuit simulator was checked.

In NumPy, np.trapezoid(y, x) applies the trapezoid rule to sampled data, and SciPy's scipy.integrate.quad chooses its own slices adaptively to reach a requested accuracy.

Worked example

Estimate ∫01exdx with the trapezoid rule and four slices.

Show the solution
  1. Sample the function. h=0.25, and ex at 0,0.25,0.5,0.75,1 is 1,1.284025,1.648721,2.117000,2.718282.
  2. Apply the trapezoid rule. The two end values get half weight: T=0.25(12+1.284025+1.648721+2.117000+2.7182822)=1.727222
  3. Compare with the exact value. The exact value is e−1=1.718282, so the error is 0.0089. The curve bends upward, so the trapezoids overshoot.

Answer the trapezoid rule gives 1.727222, 0.0089 above the exact e−1=1.718282.

Worked example

Do the same with Simpson's rule.

Show the solution
  1. Weight the points. Simpson weights the points 1,4,2,4,1 and multiplies by h3: S=0.253(1+4×1.284025+2×1.648721+4×2.117000+2.718282)=1.718319
  2. Compare. The error is 0.000037, about 240 times smaller than the trapezoid rule's, from the same five function values.

Answer Simpson's rule gives 1.718319, off by only 0.000037.

Check

Write trapezoid(f, a, b, n), the trapezoid rule with n slices.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Write simpson(f, a, b, n) (with n even). Then measure its order: set ratio to the error with 10 slices divided by the error with 20 slices, for ∫0πsin⁡xdx=2.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

The arrays t (ms) and p (W) are samples of a decaying power, p=e−2t/τ with τ = 4 ms. Integrate them with the trapezoid rule over the samples to set E, the energy in mJ.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

A method's error drops by a factor of 4 each time you halve the slice width. What is its order?

Check

Estimate ∫03x2dx with the trapezoid rule and two slices.

Check

The trapezoid rule's error on [a,b] is at most (b−a)h212max⁡|f′′|. How many slices guarantee an error below 1 × 10⁻⁶ for ∫01exdx?

Explain why

Why is measuring how fast the error shrinks a good way to test numerical code?

Practice

Keep practicing

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