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 : 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 , the slice width squared. You halve . What happens to the error?
About a quarter: . That's why the trapezoid rule is called second order. Simpson's rule, which fits parabolas instead of straight lines, has error proportional to , so halving cuts its error about sixteen times.
Name it
Three rules, from crude to good
Split into slices of width , with points .
- Rectangles (left-hand sum): . Error proportional to : first order.
- Trapezoids, joining neighboring points with straight lines:
Error about : second order.
- Simpson's rule, with even, fits a parabola through each pair of slices:
Error proportional to : fourth order.
Checking the order. Compute with slices and with . If the method has order , the error should drop by about . 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 with the trapezoid rule and four slices.
Show the solution
- Sample the function. , and at is .
- Apply the trapezoid rule. The two end values get half weight:
- Compare with the exact value. The exact value is , 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 .
Worked example
Do the same with Simpson's rule.
Show the solution
- Weight the points. Simpson weights the points and multiplies by :
- 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 .
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, 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 with the trapezoid rule and two slices.
Check
The trapezoid rule's error on is at most . How many slices guarantee an error below 1 × 10⁻⁶ for ?
Explain why
Why is measuring how fast the error shrinks a good way to test numerical code?
Each method has a known order: the trapezoid rule's error should fall about four times when the slice width halves, Simpson's about sixteen times. If a program shows exactly that ratio, it's very likely implemented correctly, because most bugs, like a wrong weight at the ends, break the pattern even when the answer looks roughly right. It also tells you how many slices you need for a given accuracy.
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.