# Adaptive Simpson's method

Adaptive Simpson's method, also called adaptive Simpson's rule, is a method of numerical integration proposed by G.F. Kuncir in 1962. It is probably the first recursive adaptive algorithm for numerical integration to appear in print, although more modern adaptive methods based on Gauss–Kronrod quadrature and Clenshaw–Curtis quadrature are now generally preferred. Adaptive Simpson's method uses an estimate of the error we get from calculating a definite integral using Simpson's rule. If the error exceeds a user-specified tolerance, the algorithm calls for subdividing the interval of integration in two and applying adaptive Simpson's method to each subinterval in a recursive manner. The technique is usually much more efficient than composite Simpson's rule since it uses fewer function evaluations in places where the function is well-approximated by a cubic function.
Simpson's rule is an interpolatory quadrature rule which is exact when the integrand is a polynomial of degree three or lower. Using Richardson extrapolation, the more accurate Simpson estimate $S(a,m)+S(m,b)$ for six function values is combined with the less accurate estimate $S(a,b)$ for three function values by applying the correction $[S(a,m)+S(m,b)-S(a,b)]/15$. So, the obtained estimate is exact for polynomials of degree five or less.

## Mathematical procedure
### Defining terms

A criterion for determining when to stop subdividing an interval, suggested by J.N. Lyness, is $|S(a,m)+S(m,b)-S(a,b)|<15\varepsilon$
where $[a,b]$ is an interval with midpoint $m={\frac {a+b}{2}}$, while $S(a,b)\,\!$, $S(a,m)\,\!$, and $S(m,b)$ given by Simpson's rule are the estimates of ∫$_a$$^b$f(x)dx{\textstyle \int _{a}^{b}f(x)\, dx}, ∫$_a$$^m$f(x)dx{\textstyle \int _{a}^{m}f(x)\, dx}, and ∫$_m$$^b$f(x)dx{\textstyle \int _{m}^{b}f(x)\, dx} respectively, and $\varepsilon$ is the desired maximum error tolerance for the interval.
Note, $\varepsilon _{i+1}=\varepsilon _{i}/2$.

### Procedural steps

To perform adaptive Simpson's method, do the following: if $|S(a,m)+S(m,b)-S(a,b)|<15\varepsilon _{i}$, add $S(a,m)$ and $S(m,b)$ to the sum of Simpson's rules which are used to approximate the integral, otherwise, perform the same operation with |S(a, m−a/2 )+S( m−a/2, m)−S(a, m)|<15ε$_{i+1}${\textstyle \left|S{\left(a, {\frac {m-a}{2}}\right)}+S{\left({\frac {m-a}{2}}, m\right)}-S(a, m)\right|<15\varepsilon _{i+1}} and |S(m, b−m/2 )+S( b−m/2, b)−S(m, b)|<15ε$_{i+1}${\textstyle \left|S{\left(m, {\frac {b-m}{2}}\right)}+S{\left({\frac {b-m}{2}}, b\right)}-S(m, b)\right|<15\varepsilon _{i+1}} instead of $|S(a,m)+S(m,b)-S(a,b)|<15\varepsilon _{i}$.

## Numerical consideration

Some inputs will fail to converge in adaptive Simpson's method quickly, resulting in the tolerance underflowing and producing an infinite loop. Simple methods of guarding against this problem include adding a depth limitation (like in the C sample and in McKeeman), verifying that ε/2 ≠ ε in floating-point arithmetics, or both (like Kuncir). The interval size may also approach the local machine epsilon, giving a = b.
Lyness's 1969 paper includes a "Modification 4" that addresses this problem in a more concrete way:$^{:490–2}$
* Let the initial interval be [A, B]. Let the original tolerance be ε$_0$.
* For each subinterval [a, b], define D(a, b), the error estimate, as 12/a−b [S$^{(2)}$(a, b)−S(a, b)]=1/4 [f(a)−4f(lm)+6f(m)−4f(rm)+f(b)]{\textstyle {\frac {12}{a-b}}[S^{(2)}(a, b)-S(a, b)]={\frac {1}{4}}[f(a)-4f(lm)+6f(m)-4f(rm)+f(b)]}. Define E = 180 ε$_0$ / (B - A). The original termination criteria would then become D ≤ E.
* If the D(a, m) ≥ D(a, b), either the round-off level have been reached or a zero for f$^{(4)}$ is found in the interval. A change in the tolerance ε$_0$ to ε′$_0$ is necessary.
** The recursive routines now need to return a D level for the current interval. A routine-static variable E' = 180 ε'$_0$ / (B - A) is defined and initialized to E.
** (Modification 4 i, ii) If further recursion is used on an interval:
*** If round-off appears to have been reached, change the E' to D(a, m).
*** Otherwise, adjust E' to max(E, D(a, m)).
** Some control of the adjustments is necessary. Significant increases and minor decreases of the tolerances should be inhibited.
* To calculate the effective ε′$_0$ over the entire interval:
** Log each x$_i$ at which the E' is changed into an array of (x$_i$, ε$_i$' ) pairs. The first entry should be (a, ε′$_0$).
** The actual ε$_{eff}$ is the arithmetic mean of all ε′$_0$, weighted by the width of the intervals.
* If the current E' for an interval is higher than E, then the fifth-order acceleration/correction would not apply:
** The "15" factor in the termination criteria is disabled.
** The correction term should not be used.
The epsilon-raising maneuver allows the routine to be used in a "best effort" mode: given a zero initial tolerance, the routine will try to get the most precise answer and return an actual error level.$^{:492}$

## Sample code implementations

A common implementation technique shown below is passing down f(a), f(b), f(m) along with the interval [a, b]. These values, used for evaluating S(a, b) at the parent level, will again be used for the subintervals. Doing so cuts down the cost of each recursive call from 6 to 2 evaluations of the input function. The size of the stack space used stays linear to the layer of recursions.

### Python

Here is an implementation of adaptive Simpson's method in Python.

### C

Here is an implementation of the adaptive Simpson's method in C99 that avoids redundant evaluations of f and quadrature computations. It includes all three "simple" defenses against numerical problems.
This implementation has been incorporated into a C++ ray tracer intended for X-Ray Laser simulation at Oak Ridge National Laboratory, among other projects. The ORNL version has been enhanced with a call counter, templates for different datatypes, and wrappers for integrating over multiple dimensions.

### Racket

Here is an implementation of the adaptive Simpson method in Racket with a behavioral software contract. The exported function computes the indeterminate integral for some given function f.

## Related algorithms

* Henriksson (1961) is a non-recursive variant of Simpson's Rule. It "adapts" by integrating from left to right and adjusting the interval width as needed.
* Kuncir's Algorithm 103 (1962) is the original recursive, bisecting, adaptive integrator. Algorithm 103 consists of a larger routine with a nested subroutine (loop AA), made recursive by the use of the goto statement. It guards against the underflowing of interval widths (loop BB), and aborts as soon as the user-specified eps is exceeded. The termination criteria is $|S^{(2)}(a,b)-S(a,b)|<2^{-n}\epsilon \,$, where n is the current level of recursion and S$^{(2)}$ is the more accurate estimate.
* McKeeman's Algorithm 145 (1962) is a similarly recursive integrator that splits the interval into three instead of two parts. The recursion is written in a more familiar manner. The 1962 algorithm, found to be over-cautious, uses $|S^{(3)}(a,b)-S(a,b)|<3^{-n}\epsilon \,$ for termination, so a 1963 improvement uses ${\sqrt {3}}^{\,-n}\epsilon$ instead.$^{:485,487}$
* Lyness (1969) is almost the current integrator. Created as a set of four modifications of McKeeman 1962, it replaces trisection with bisection to lower computational costs (Modifications 1+2, coinciding with the Kuncir integrator) and improves McKeeman's 1962/63 error estimates to the fifth order (Modification 3), in a way related to Boole's rule and Romberg's method.$^{:489}$ Modification 4, not implemented here, contains provisions for roundoff error that allows for raising the ε to the minimum allowed by current precision and returning the new error.