# Workbook 14 — Adaptive quadrature by hand
> [!abstract] The problem
> Evaluate $\int_0^4 e^{-2x}\sin x\,dx$ to a tolerance of $10^{-3}$ by running **adaptive Simpson's** quadrature *by hand* from a table of precomputed Simpson panels $S(a,b)$. On each interval $[a,b]$ with midpoint $m$, accept the refined estimate $S(a,m)+S(m,b)$ when $\tfrac1{15}\big|[S(a,m)+S(m,b)]-S(a,b)\big|\le\texttt{tol}$; otherwise split and recurse on each half with $\texttt{tol}/2$. **(a)** carry out the accept/split tree; **(b)** sum the accepted panels and identify the unused (decoy) values.
The acceptance test is the Day 15 error estimate $|I-I_2|\approx\tfrac1{15}|I_2-I_1|$: $I_1=S(a,b)$ is the coarse panel, $I_2=S(a,m)+S(m,b)$ the refined one, and the $15=2^4-1$ is Simpson's fourth-order fingerprint.
---
## (a) The accept/split tree
Start at $[0,4]$ with $\texttt{tol}=10^{-3}$. At each interval compare coarse vs. refined; the error estimate is $\tfrac1{15}|I_2-I_1|$.
**$[0,4]$, tol $=10^{-3}$.** $I_1=S(0,4)=0.04424$; $I_2=S(0,2)+S(2,4)=0.15739+0.00593=0.16332$.
$\tfrac1{15}|0.16332-0.04424|=\tfrac{0.11908}{15}=7.9\times10^{-3}>10^{-3}\ \Rightarrow\ \textbf{split},\ \text{pass tol}=5\times10^{-4}.$
**$[0,2]$, tol $=5\times10^{-4}$.** $I_1=S(0,2)=0.15739$; $I_2=S(0,1)+S(1,2)=0.13656+0.05486=0.19142$.
$\tfrac1{15}|0.19142-0.15739|=\tfrac{0.03403}{15}=2.27\times10^{-3}>5\times10^{-4}\ \Rightarrow\ \textbf{split},\ \text{tol}=2.5\times10^{-4}.$
- **$[0,1]$, tol $=2.5\times10^{-4}$.** $I_1=S(0,1)=0.13656$; $I_2=S(0,\tfrac12)+S(\tfrac12,1)=0.06472+0.07488=0.13960$. Estimate $\tfrac1{15}|0.13960-0.13656|=2.03\times10^{-4}\le2.5\times10^{-4}$ → **accept $0.13960$**.
- **$[1,2]$, tol $=2.5\times10^{-4}$.** $I_1=S(1,2)=0.05486$; $I_2=S(1,\tfrac32)+S(\tfrac32,2)=0.03959+0.01543=0.05502$. Estimate $\tfrac1{15}|0.05502-0.05486|=1.1\times10^{-5}\le2.5\times10^{-4}$ → **accept $0.05502$**.
**$[2,4]$, tol $=5\times10^{-4}$.** $I_1=S(2,4)=0.00593$; $I_2=S(2,3)+S(3,4)=0.00552-0.00020=0.00532$.
$\tfrac1{15}|0.00532-0.00593|=\tfrac{0.00061}{15}=4.1\times10^{-5}\le5\times10^{-4}\ \Rightarrow\ \textbf{accept } 0.00532.$
So the tree accepts three panels: $[0,1]$, $[1,2]$, and $[2,4]$. The left side of the interval (near the peak of $e^{-2x}\sin x$) needed refining down to width $1$; the right side, where the integrand is already tiny and flat, was accepted whole.
## (b) The estimate, and the decoys
$\boxed{\int_0^4 e^{-2x}\sin x\,dx\approx 0.13960+0.05502+0.00532=0.19994.}$
The exact value is $\int_0^4 e^{-2x}\sin x\,dx=\Big[\tfrac{e^{-2x}}{5}(-2\sin x-\cos x)\Big]_0^4=0.200145$, so the error is $|0.19994-0.200145|=2.0\times10^{-4}<10^{-3}$ — comfortably inside tolerance.
**Never used:** $S(2,\tfrac52),\ S(\tfrac52,3),\ S(3,\tfrac72),\ S(\tfrac72,4)$. These are the *eighth*-width panels inside $[2,4]$. Because $[2,4]$ passed its test at the first level, the recursion never descended there, so its finer subdivisions were red herrings — exactly the point of adaptivity: **no effort is spent where the coarse estimate is already good enough.**
---
# Sanity checks and context
## Why this saves work
> [!note] Adaptivity vs. a uniform grid
> A uniform Simpson grid fine enough to resolve the peak near $x=0$ would place just as many points out in the dead, flat tail past $x=2$ — wasted evaluations. Adaptive quadrature spends points **where the integrand bends** and coasts where it is flat: here it used width-$1$ panels on $[0,2]$ but a single width-$2$ panel on $[2,4]$.
> - The $15$ in the test is $2^4-1$: halving $h$ cuts Simpson's error by $2^4=16$, so $I_2-I_1$ is about $15\times$ the error of the *finer* estimate $I_2$, and $\tfrac1{15}|I_2-I_1|$ estimates $|I-I_2|$ without knowing the true $I$.
> - Splitting the tolerance ($\texttt{tol}\to\texttt{tol}/2$ per level) keeps the accepted pieces summing to within the original $\texttt{tol}$ overall.
## Common pitfalls
> [!warning] Watch out for these
> - **Halve the tolerance on recursion.** Each child interval is tested against $\texttt{tol}/2$, not the parent's $\texttt{tol}$; forgetting this accepts too eagerly.
> - **Coarse vs. refined, not left vs. right.** The test compares $S(a,b)$ (one panel) against $S(a,m)+S(m,b)$ (two panels), not the two halves against each other.
> - **Accept the refined value.** When a test passes you keep $S(a,m)+S(m,b)$ (the better estimate), not the coarse $S(a,b)$.
> - **Don't chase the decoys.** You only need a panel's sub-panels if that panel *fails*; listing extra values is a deliberate trap.
## Checkpoint questions
1. If the tolerance were loosened to $10^{-2}$, how much of the tree would still split? (Test $[0,4]$: is $7.9\times10^{-3}\le10^{-2}$?)
2. Total function evaluations: each accepted Simpson panel uses $3$ points (with endpoints shared). Roughly how many distinct evaluations did the adaptive run cost, and how does that compare to a uniform grid resolving the same peak?
3. Why is the integrand $e^{-2x}\sin x$ a good advertisement for adaptivity specifically (think about where it is large and where it is essentially zero)?
4. Redo the $[2,4]$ test pretending it had *failed*: which four panels would you then have needed, and do they reproduce $S(2,3)$ and $S(3,4)$ when summed in pairs?