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

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 ∫01xdx=23 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 −0.208/N1.5: doubling N cuts it by only 21.5=2.8, 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 x from 0 to 1 to an error of 10−8. Roughly how many function evaluations does it need, compared with uniform Simpson?

Name it

Adaptive Simpson

To integrate over [a, b] to tolerance ε:

  1. Compute Simpson's rule on the whole interval, S1, and on its two halves, S2=Sleft+Sright.
  2. Since Simpson's error goes as h4, S2's error is about S2−S115.
  3. If |S2−S1|≤15ε, accept S2+S2−S115.
  4. 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, Rk,0, and extrapolate repeatedly:

Rk,j=Rk,j−1+Rk,j−1−Rk−1,j−14j−1

Column j has error O(h2j+2): 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 h1.5 wall, because the error isn't a series in h2.

Handling trouble

  • Endpoint singularities: substitute to remove them. With x=t2, ∫01dxx=∫012dt.
  • Infinite intervals: map to a finite one, such as x=t1−t, 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 ∫01xdx.

Show the solution
  1. Simpson on the whole interval. S1=16(0+40.5+1)=0.63807
  2. Simpson on the two halves. S2=0.22559+0.43093=0.65653
  3. Estimate the error. The difference, 0.01846, says S2's error is about 0.0012, so for a tolerance of 10−6 the method splits.
  4. The corrected value. S2+S2−S115=0.65776 This is still 0.009 short of 23.
  5. Why. Near 0 the h4 model is wrong, and the method keeps splitting there until it isn't.

Answer S1=0.63807 and S2=0.65653; their difference is far over the tolerance, so the method splits, and even the corrected 0.65776 is 0.009 short of 23.

Worked example

Romberg on ∫01exdx: the trapezoid rule with 1 and 2 intervals gives 1.85914 and 1.75393. Extrapolate.

Show the solution
  1. Extrapolate the pair. R1,1=1.75393+1.75393−1.859143=1.71886
  2. Recognize it. That's Simpson's rule with 2 intervals, against the exact e−1=1.71828.
  3. One more level. With 4 intervals as well, the next column reaches 1.718282, correct to 6 digits from 5 function values.

Answer R1,1=1.71886, Simpson's value; adding the 4-interval value takes the next column to 1.718282.

Worked example

Build a Romberg table for ∫0πsin⁡xdx=2 from the trapezoid values with 1, 2 and 4 intervals.

Show the solution
  1. The trapezoid values. With 1, 2 and 4 intervals they are 0, 1.570796 and 1.896119.
  2. First extrapolation. Combining each pair as 4T2n−Tn3 gives 2.094395 and 2.004560, which are Simpson's rule.
  3. Second extrapolation. 16R2−R115=1.998571
  4. The gain. From five function values, the error went from 0.104 to 0.0014.
  5. 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 h2,h4,… 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 |S2−S1| does it accept the interval?

Check

Romberg's column j = 2 has error O(hp). What's p?

Check

Using the asymptotic error −0.2079N−1.5, about what's the trapezoid rule's error on ∫01xdx with 100 intervals?

Check

After the substitution x=t2, what's ∫04dxx?

Check

A library's adaptive integrator returns a value and an error estimate of 0.3%, but you asked for 10−8. 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 [0,2h] 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°.

Explain why

How does adaptive quadrature know where to put more samples without being told where the function is difficult?

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.