---
title: Adaptive quadrature and Romberg integration
heading: Spend the samples where the function is hard, and let the error estimate itself
eyebrow: A13 · Lesson 19 · Adaptive quadrature and Romberg integration
xp: 40
---

::: hook
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.
:::

::: play
- The trapezoid rule on $\int_0^1\sqrt x\,dx = \frac23$ 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/N^{1.5}$: doubling N cuts it by only $2^{1.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.
:::
:::

```sim plot
view: graph
x: x = 0..1
y: y = 0..1.05
slider: N = 2..64 step 1 = 4
plot: sqrt(x) | label: √x
plot: sqrt(floor(x*N)/N) + (x - floor(x*N)/N)*(sqrt((floor(x*N) + 1)/N) - sqrt(floor(x*N)/N))*N | samples: 1200 | fill: 0..1 | color: ch2 | label: trapezoids
readout: trapezoid error, about = -0.2078862*N^(-1.5)
readout: error ratio when N doubles = 2^1.5
```

::: predict
## Adaptive Simpson integrates $\sqrt x$ from 0 to 1 to an error of $10^{-8}$. Roughly how many function evaluations does it need, compared with uniform Simpson?

- [ ] About the same
- [ ] About half
- [x] About 300, against tens of thousands
- [ ] More, because of its bookkeeping

::: reveal
About 300 evaluations (157 subdivisions), and its actual error is $2.6 \times 10^{-11}$. Uniform Simpson needs about 20 000 intervals for $10^{-8}$, because its error falls only as $h^{1.5}$ here. The adaptive method halves the interval next to 0 dozens of times and leaves the rest coarse.
:::
:::

::: nameit
## Adaptive Simpson

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

1. Compute Simpson's rule on the whole interval, $S_1$, and on its two halves, $S_2 = S_{left} + S_{right}$.
2. Since Simpson's error goes as $h^4$, $S_2$'s error is about $\frac{S_2 - S_1}{15}$.
3. If $\lvert S_2 - S_1\rvert \le 15\varepsilon$, accept $S_2 + \frac{S_2 - S_1}{15}$.
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, $R_{k,0}$, and extrapolate repeatedly:

$$R_{k,j} = R_{k,j-1} + \frac{R_{k,j-1} - R_{k-1,j-1}}{4^j - 1}$$

Column j has error $O(h^{2j+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 $h^{1.5}$ wall, because the error isn't a series in $h^2$.

## Handling trouble

- **Endpoint singularities:** substitute to remove them. With $x = t^2$, $\int_0^1\frac{dx}{\sqrt x} = \int_0^12\,dt$.
- **Infinite intervals:** map to a finite one, such as $x = \frac{t}{1 - 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.
:::

::: example
## Do the first adaptive Simpson step on $\int_0^1\sqrt x\,dx$.

::: solution
1. **Simpson on the whole interval.**
   $$\begin{aligned} S_1 &= \frac16(0 + 4\sqrt{0.5} + 1) \\ &= 0.63807 \end{aligned}$$
2. **Simpson on the two halves.**
   $$\begin{aligned} S_2 &= 0.22559 + 0.43093 \\ &= 0.65653 \end{aligned}$$
3. **Estimate the error.** The difference, 0.01846, says $S_2$'s error is about 0.0012, so for a tolerance of $10^{-6}$ the method splits.
4. **The corrected value.**
   $$S_2 + \frac{S_2 - S_1}{15} = 0.65776$$
   This is still 0.009 short of $\frac23$.
5. **Why.** Near 0 the $h^4$ model is wrong, and the method keeps splitting there until it isn't.

**Answer:** $S_1 = 0.63807$ and $S_2 = 0.65653$; their difference is far over the tolerance, so the method splits, and even the corrected 0.65776 is 0.009 short of $\frac23$.
:::
:::

::: example
## Romberg on $\int_0^1e^x\,dx$: the trapezoid rule with 1 and 2 intervals gives 1.85914 and 1.75393. Extrapolate.

::: solution
1. **Extrapolate the pair.**
   $$\begin{aligned} R_{1,1} &= 1.75393 \\ &\quad + \frac{1.75393 - 1.85914}{3} \\ &= 1.71886 \end{aligned}$$
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:** $R_{1,1} = 1.71886$, Simpson's value; adding the 4-interval value takes the next column to 1.718282.
:::
:::

::: example
## Build a Romberg table for $\int_0^\pi\sin x\,dx = 2$ from the trapezoid values with 1, 2 and 4 intervals.

::: 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
   $$\frac{4T_{2n} - T_n}{3}$$
   gives 2.094395 and 2.004560, which are Simpson's rule.
3. **Second extrapolation.**
   $$\frac{16R_{2} - R_1}{15} = 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 $h^2, h^4, \ldots$ for smooth integrands.

**Answer:** the table ends at 1.998571, an error of 0.0014 from five function values.
:::
:::

::: check
given: S1 = 1.25 | 2.5; S2 = 1.2 | 2.47
answer: S2 + (S2 - S1)/15
tolerance: 0.01%

## Simpson's rule gives {S1} on an interval and {S2} from its two halves. What's the corrected estimate?

::: right
Right. $S_2 + \frac{S_2 - S_1}{15}$ = {answer}.
:::

::: misses
- [S2 + (S2 - S1)/3] That's for an $h^2$ error, like the trapezoid rule's; Simpson's goes as $h^4$, so divide by 15.
- [any] Not yet. $S_2 + \frac{S_2 - S_1}{2^4 - 1}$.
:::
:::

::: check
given: e = 0.001 | 0.0001 | 0.00001 | 0.000001 | 0.0000001
answer: 15*e
tolerance: 0.5%

## An adaptive Simpson step has tolerance {e}. Below what difference $\lvert S_2 - S_1\rvert$ does it accept the interval?

::: right
Right. $15\varepsilon$ = {answer}.
:::

::: misses
- [e] The test compares 15ε with the difference, because the error is about a fifteenth of it.
- [any] Not yet. Accept when $\lvert S_2 - S_1\rvert/15 \le \varepsilon$.
:::
:::

::: check
given: j = 1 | 2 | 3 | 4
answer: 2*j + 2
tolerance: 0

## Romberg's column j = {j} has error $O(h^p)$. What's p?

::: right
Right. $p = 2j + 2$ = {answer}.
:::

::: misses
- [2*j] Column 0 (the trapezoid rule) already has $h^2$; each column adds 2.
- [any] Not yet. Each extrapolation removes one more power of $h^2$.
:::
:::

::: check
given: N = 10 | 100 | 1000
answer: -0.2078862*N^(-1.5)
tolerance: 3%

## Using the asymptotic error $-0.2079N^{-1.5}$, about what's the trapezoid rule's error on $\int_0^1\sqrt x\,dx$ with {N} intervals?

::: right
Right. {answer}.
:::

::: misses
- [-0.2078862*N^(-2)] The square root's singular slope makes the error fall as $N^{-1.5}$, not $N^{-2}$.
- [any] Not yet. $-0.2079N^{-1.5}$.
:::
:::

::: check
given: b = 1 | 4 | 9
answer: 2*sqrt(b)
tolerance: 0.1%

## After the substitution $x = t^2$, what's $\int_0^{\given{b}}\frac{dx}{\sqrt x}$?

::: right
Right. $\int_0^{\sqrt b}2\,dt = 2\sqrt b$ = {answer}, with no singularity left.
:::

::: misses
- [sqrt(b)] $dx = 2t\,dt$, so the integrand becomes 2.
- [any] Not yet. $\frac{dx}{\sqrt x} = \frac{2t\,dt}{t}$.
:::
:::

::: check
kind: choice

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

- [ ] The library is broken
- [x] The integrand has a singularity, discontinuity or rapid oscillation it couldn't resolve within its subdivision limit
- [ ] The answer is exact and the estimate is wrong
- [ ] The tolerance was too loose

::: misses
- [1] It's doing its job: it's telling you it failed.
- [3] A large error estimate is a warning to take seriously.
- [4] $10^{-8}$ is tight, not loose.
:::
:::

::: check
kind: parts
given: t1 = 1.859 | 2.000; t2 = 1.754 | 1.900; t4 = 1.727 | 1.875

## Trapezoid values with 1, 2 and 4 intervals are {t1}, {t2} and {t4}.

::: part
answer: (4*t4 - t2)/3
tolerance: 0.01%

### What is the Romberg entry from the last two?

::: misses
- [(t4 + t2)/2] Weight them $\frac{4T_{2n} - T_n}{3}$.
- [any] $\frac{4T_4 - T_2}{3}$.
:::
:::

::: part
answer: (16*(4*t4 - t2)/3 - (4*t2 - t1)/3)/15
tolerance: 0.01%

### What is the next column's entry?

::: misses
- [any] $\frac{16R_{2,1} - R_{1,1}}{15}$.
:::
:::
:::

::: check
kind: derivation
vars: a = -2..2, b = -2..2, c = -2..2, h = 0.1..1

## On $[0, 2h]$ the function takes values a, b, c at 0, h, 2h.

::: step
answer: h*(a + 2*b + c)/2

### What does the trapezoid rule with two intervals give?
:::

::: step
answer: h*(a + 4*b + c)/3

### Extrapolate with the one-interval value $h(a + c)$: what is $\frac{4T_2 - T_1}{3}$?
:::

::: right
Right. Richardson extrapolation of the trapezoid rule is Simpson's rule.
:::
:::

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

::: model
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

::: check
given: S1 = 0.75 | 3.2; S2 = 0.71 | 3.15
answer: S2 + (S2 - S1)/15
tolerance: 0.01%

## Corrected Simpson estimate from {S1} and {S2}?

::: misses
- [any] $S_2 + (S_2 - S_1)/15$.
:::
:::

::: check
given: j = 1..8 int
answer: 2*j + 2
tolerance: 0

## Error order of Romberg column {j}?

::: misses
- [any] $2j + 2$.
:::
:::

::: check
given: N = 10 | 50 | 100 | 500 | 1000 | 5000
answer: -0.2078862*N^(-1.5)
tolerance: 3%

## Trapezoid error for $\int_0^1\sqrt x$ with {N} intervals?

::: misses
- [any] $-0.2079N^{-1.5}$.
:::
:::
:::
