One derivation replaces the integrand by the quadratic polynomial which takes the same values as at the end points ''a'' and ''b'' and the midpoint ''m'' = (''a''+''b'') / 2. One can use Lagrange polynomial interpolation to find an expression for this polynomial,
:
An easy (albeit tedious) calculation shows that
: [2]
Averaging the midpoint and the trapezium rules
Another derivation constructs Simpson's rule from two simpler approximations: the midpoint rule
:
and the trapezium rule
:
The errors in these approximations are
:
respectively. It follows that the leading error term vanishes if we take the weighted average
:
This weighted average is exactly Simpson's rule.
Using another approximation (for example, the trapezium rule with twice as many points), it is possible to take a suitable weighted average and eliminate another error term. This is Romberg's method.
Undetermined coefficients
The third derivation starts from the ''ansatz''
:
The coefficients α, β and γ can be fixed by requiring that this approximation be exact for all quadratic polynomials. This yields Simpson's rule.
Error
The error in approximating an integral by Simpson's rule is
:
where is some number between and .[3]
The error is (asymptotically) proportional to . However, the above derivations suggest an error proportional to . Simpson's rule gains an extra order because the points at which the integrand are evaluated, are distributed symmetrically in the interval [''a'', ''b''].
Composite Simpson's rule
If the interval of integration is in some sense "small", then Simpson's rule will provide an adequate approximation to the exact integral. By small, what we really mean is that the function being integrated is relatively smooth over the interval . For such a function, a smooth quadratic interpolant like the one used in Simpson's rule will give good results.
However, it is often the case that the function we are trying to integrate is not smooth over the interval. Typically, this means that either the function is highly oscillatory, or it lacks derivatives at certain points. In these cases, Simpson's rule may give very poor results. One common way of handling this problem is by breaking up the interval into a number of small subintervals. Simpson's rule is then applied to each subinterval, with the results being summed to produce an approximation for the integral over the entire interval. This sort of approach is termed the ''composite Simpson's rule''.
Suppose that the interval is split up in subintervals, with an even number. Then, the composite Simpson's rule is given by
:
where for with ; in particular, and . The above formula can also be written as
:
The error committed by the composite Simpson's rule is bounded (in absolute value) by
:
where is the "step length", given by [4]
This formulation splits the interval in subintervals of equal length. In practice, it is often advantageous to use subintervals of different lengths, and concentrate the efforts on the places where the integrand is less well-behaved. This leads to the adaptive Simpson's method.
Python implementation of Simpson's rule
Here is an implementation of Simpson's rule in Python.
def simpson_rule(f, a, b):
"Approximate the definite integral of f from a to b by Simpson's rule."
c = (a + b) / 2.0
h3 = abs(b - a) / 6.0
return h3 ★ (f(a) + 4.0 ★ f(c) + f(b))
# Calculates integral of sin(x) from 0 to 1
from math import sin
print simpson_rule(sin, 0, 1)
Integrating sin ''x'' from 0 to 1 with this code gives 0.4598622... whereas the true value is 1 − cos 1 = 0.45969769413... .
Matlab implementation of composite Simpson's rule
%Define the function to integrate (using anonymous functions)
f = @(x) x^2;
%Set the interval to integrate
a = -1;
b = 1;
%set the number of panels to compute and their length
n = 100;
h = (b-a)/n;
%split up the interval into subintervals
x = [a:h:b];
%note that matlab matrices are indexed starting at 1
sum = f(x(1));
for i=2:2:n
sum = sum + 4 ★ f(x(i));
end;
for i=3:2:n-1
sum = sum + 2 ★ f(x(i));
end;
%prints out the result
f_integrated = (h/3) ★ (sum + f(x(n+1)))
Notes
1. Süli and Mayers, §7.2 2. Atkinson, p. 256; Süli and Mayers, §7.2 3. Atkinson, equation (5.1.15); Süli and Mayers, Theorem 7.2 4. Atkinson, pp. 257+258; Süli and Mayers, §7.5
References
Simpson's rule is mentioned in many text books in numerical analysis:
★ An Introduction to Numerical Analysis, Atkinson, Kendall A., , , John Wiley & Sons, 1989, ISBN 0-471-50023-2
★ Numerical Analysis, Burden, Richard L. and Faires, J. Douglas, , , Brooks/Cole, 2000, ISBN 0-534-38216-9
★ An Introduction to Numerical Analysis, Süli, Endre and Mayers, David, , , Cambridge University Press, 2003, ISBN 0-521-81026-4 (hardback), ISBN 0-521-00794-1 (paperback)