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 formula_1 for six function values is combined with the less accurate estimate formula_2 for three function values by applying the correction formula_3. 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
formula_4
where formula_5 is an interval with midpoint formula_6, while formula_7, formula_8, and formula_9 given by Simpson's rule are the estimates of formula_10, formula_11, and formula_12 respectively, and formula_13 is the desired maximum error tolerance for the interval.
Note, formula_14.
Procedural steps.
To perform adaptive Simpson's method, do the following: if formula_15, add formula_16 and formula_9 to the sum of Simpson's rules which are used to approximate the integral, otherwise, perform the same operation with formula_18 and formula_19 instead of formula_15.
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:
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.
from __future__ import division # python 2 compat
def _quad_simpsons_mem(f, a, fa, b, fb):
 """Evaluates the Simpson's Rule, also returning m and f(m) to reuse"""
 m = (a + b) / 2
 fm = f(m)
 return (m, fm, abs(b - a) / 6 * (fa + 4 * fm + fb))
def _quad_asr(f, a, fa, b, fb, eps, whole, m, fm):
 Efficient recursive implementation of adaptive Simpson's rule.
 Function values at the start, middle, end of the intervals are retained.
 lm, flm, left = _quad_simpsons_mem(f, a, fa, m, fm)
 rm, frm, right = _quad_simpsons_mem(f, m, fm, b, fb)
 delta = left + right - whole
 if abs(delta) ;= 15 * eps:
 return left + right + delta / 15
 return _quad_asr(f, a, fa, m, fm, eps/2, left , lm, flm) +\
 _quad_asr(f, m, fm, b, fb, eps/2, right, rm, frm)
def quad_asr(f, a, b, eps):
 """Integrate f from a to b using Adaptive Simpson's Rule with max error of eps."""
 fa, fb = f(a), f(b)
 m, fm, whole = _quad_simpsons_mem(f, a, fa, b, fb)
 return _quad_asr(f, a, fa, b, fb, eps, whole, m, fm)
from math import sin
print(quad_asr(sin, 0, 1, 1e-09))
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.
/** Adaptive Simpson's Rule, Recursive Core */
float adaptiveSimpsonsAux(float (*f)(float), float a, float b, float eps,
 float whole, float fa, float fb, float fm, int rec) {
 float m = (a + b)/2, h = (b - a)/2;
 float lm = (a + m)/2, rm = (m + b)/2;
 // serious numerical trouble: it won't converge
 float flm = f(lm), frm = f(rm);
 float left = (h/6) * (fa + 4*flm + fm);
 float right = (h/6) * (fm + 4*frm + fb);
 float delta = left + right - whole;
 if (rec ;= 0 &amp;&amp; errno != EDOM) errno = ERANGE; // depth limit too shallow
 // Lyness 1969 + Richardson extrapolation; see article
 if (rec ;= 0 || fabs(delta) ;= 15*eps)
 return left + right + (delta)/15;
 return adaptiveSimpsonsAux(f, a, m, eps/2, left, fa, fm, flm, rec-1) +
 adaptiveSimpsonsAux(f, m, b, eps/2, right, fm, fb, frm, rec-1);
/** Adaptive Simpson's Rule Wrapper
 * (fills in cached function evaluations) */
float adaptiveSimpsons(float (*f)(float), // function ptr to integrate
 float a, float b, // interval [a,b]
 float epsilon, // error tolerance
 int maxRecDepth) { // recursion cap
 errno = 0;
 float h = b - a;
 if (h == 0) return 0;
 float fa = f(a), fb = f(b), fm = f((a + b)/2);
 float S = (h/6)*(fa + 4*fm + fb);
 return adaptiveSimpsonsAux(f, a, b, epsilon, S, fa, fb, fm, maxRecDepth);
/** usage example */
static int callcnt = 0;
static float sinfc(float x) { callcnt++; return sinf(x); } 
static float frand48c(float x) { callcnt++; return drand48(); } 
int main() {
 // Let I be the integral of sin(x) from 0 to 2
 float I = adaptiveSimpsons(sinfc, 0, 2, 1e-5, 3);
 printf("integrate(sinf, 0, 2) = %lf\n", I); // print the result
 perror("adaptiveSimpsons"); // Was it successful? (depth=1 is too shallow)
 printf("(%d evaluations)\n", callcnt);
 callcnt = 0; srand48(0);
 I = adaptiveSimpsons(frand48c, 0, 0.25, 1e-5, 25); // a random function
 printf("integrate(frand48, 0, 0.25) = %lf\n", I);
 perror("adaptiveSimpsons"); // won't converge
 printf("(%d evaluations)\n", callcnt);
 return 0;
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".
(provide/contract
 [adaptive-simpson 
 (- ;i ((f (- ; real? real?)) (L real?) (R (L) (and/c real? ( ;/c L))))
 (#:epsilon (ε real?))
 (r real?))])
 (define [Email] (f L))
 (define [Email] (f R))
 (define-values (M [Email] whole) (simpson-1call-to-f f L [Email] R [Email]
 (asr f L [Email] R [Email] ε whole M [Email]
 (define-values (leftM [Email] left*) (simpson-1call-to-f f L [Email] M [Email]
 (define-values (rightM [Email] right*) (simpson-1call-to-f f M [Email] R [Email]
 (define delta* (- (+ left* right*) whole))
 (cond
 [( ;= (abs delta*) (* 15 ε)) (+ left* right* (/ delta* 15))]
 [else (define epsilon1 (/ ε 2))
 (+ (asr f L [Email] M [Email] epsilon1 left* leftM [Email] 
 (asr f M [Email] R [Email] epsilon1 right* rightM [Email]
 (define M (mid L R))
 (define [Email] (f M))
 (values M [Email] (* (/ (abs (- R L)) 6) (+ [Email] (* 4 [Email] [Email]
Bibliography.
 ;templatestyles src="Reflist/styles.css" / ;