A13 · Lesson 19 · Adaptive quadrature and Romberg integration
Spend the samples where the function is hard, and let the error estimate itself
A uniform grid spends as many samples on the boring stretches of a function as on the hard ones. Most integrands have trouble in a few places: a square-root edge, a sharp resonance, a kink where a diode turns on. Adaptive quadrature compares two estimates on each piece, keeps the pieces where they agree, and splits only the ones where they don't. The same comparison gives an error estimate for free. It's what scipy.integrate.quad does, and it lets you ask for an integral to a tolerance instead of choosing a step size and hoping.
- The trapezoid rule on with N intervals. The square root has an infinite slope at 0, where its error piles up.
- The readout gives the error, which behaves like : doubling N cuts it by only , not 4. The trouble at one end ruins the rate everywhere.
- Simpson's rule fares no better here, because the problem is the function, not the rule. The cure is to put the extra samples near 0, which is what an adaptive method does on its own.
Predict
Adaptive Simpson integrates from 0 to 1 to an error of . Roughly how many function evaluations does it need, compared with uniform Simpson?
About 300 evaluations (157 subdivisions), and its actual error is . Uniform Simpson needs about 20 000 intervals for , because its error falls only as here. The adaptive method halves the interval next to 0 dozens of times and leaves the rest coarse.
Name it
Adaptive Simpson
To integrate over [a, b] to tolerance ε:
- Compute Simpson's rule on the whole interval, , and on its two halves, .
- Since Simpson's error goes as , 's error is about .
- If , accept .
- Otherwise apply the same procedure to each half with tolerance ε/2, and add.
Function values are reused, so each split costs only two new evaluations.
Romberg integration
Start from trapezoid estimates with 1, 2, 4, 8, ... intervals, , and extrapolate repeatedly:
Column j has error : column 1 is Simpson's rule, column 2 is Boole's. For smooth integrands it converges very fast; for the square root it can't beat the wall, because the error isn't a series in .
Handling trouble
- Endpoint singularities: substitute to remove them. With , .
- Infinite intervals: map to a finite one, such as , or use a rule built for them (Gauss–Laguerre, tanh–sinh).
- Kinks and discontinuities: split the interval there by hand.
- Library routines (QUADPACK,
scipy.integrate.quad) use adaptive Gauss–Kronrod rules and return an error estimate. Read it.
Worked example
Do the first adaptive Simpson step on .
Show the solution
- Simpson on the whole interval.
- Simpson on the two halves.
- Estimate the error. The difference, 0.01846, says 's error is about 0.0012, so for a tolerance of the method splits.
- The corrected value. This is still 0.009 short of .
- Why. Near 0 the model is wrong, and the method keeps splitting there until it isn't.
Answer and ; their difference is far over the tolerance, so the method splits, and even the corrected 0.65776 is 0.009 short of .
Worked example
Romberg on : the trapezoid rule with 1 and 2 intervals gives 1.85914 and 1.75393. Extrapolate.
Show the solution
- Extrapolate the pair.
- Recognize it. That's Simpson's rule with 2 intervals, against the exact .
- One more level. With 4 intervals as well, the next column reaches 1.718282, correct to 6 digits from 5 function values.
Answer , Simpson's value; adding the 4-interval value takes the next column to 1.718282.
Worked example
Build a Romberg table for from the trapezoid values with 1, 2 and 4 intervals.
Show the solution
- The trapezoid values. With 1, 2 and 4 intervals they are 0, 1.570796 and 1.896119.
- First extrapolation. Combining each pair as gives 2.094395 and 2.004560, which are Simpson's rule.
- Second extrapolation.
- The gain. From five function values, the error went from 0.104 to 0.0014.
- Why it works. Each column removes one more even power of h from the error, which works because the trapezoid rule's error has an expansion in for smooth integrands.
Answer the table ends at 1.998571, an error of 0.0014 from five function values.
Check
Simpson's rule gives 1.25 on an interval and 1.2 from its two halves. What's the corrected estimate?
Check
An adaptive Simpson step has tolerance 0.0001. Below what difference does it accept the interval?
Check
Romberg's column j = 2 has error . What's p?
Check
Using the asymptotic error , about what's the trapezoid rule's error on with 100 intervals?
Check
After the substitution , what's ?
Check
A library's adaptive integrator returns a value and an error estimate of 0.3%, but you asked for . What's most likely?
Check
Trapezoid values with 1, 2 and 4 intervals are 1.859, 1.754 and 1.875.
(a)
What is the Romberg entry from the last two?
(b)
What is the next column's entry?
Check
On the function takes values a, b, c at 0, h, 2h.
Step 1
What does the trapezoid rule with two intervals 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
Extrapolate with the one-interval value : what is ?
Explain why
How does adaptive quadrature know where to put more samples without being told where the function is difficult?
On each piece it computes two estimates of the integral that have different errors, such as Simpson's rule on the whole piece and on its two halves. Where the function is smooth, the two agree closely, and their difference is a reliable estimate of the error, so the piece is accepted. Where the function changes abruptly, the estimates disagree, which signals a large error, so that piece is split and the test repeated on each half. The disagreement itself locates the difficulty, so the samples end up clustered where they're needed without any advance knowledge of the integrand.
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.