Processing math: 100%
Skip to main content
Library homepage
 

Text Color

Text Size

 

Margin Size

 

Font Type

Enable Dyslexic Font
Physics LibreTexts

9.3: Simpson's Rule

( \newcommand{\kernel}{\mathrm{null}\,}\)

Our analysis of the mid-point rule and the trapezium rule showed that both methods have O(1/N2) numerical error. In both cases, the error can be traced to the same source: the fact that the integral estimate over each segment differs from the Taylor series result in the second-order term—the term proportional to f(xn). This suggests a way to improve on the numerical integration result: we could take a weighted average of the mid-point rule and the trapezium rule, such that the second-order numerical errors from the two schemes cancel each other out! This is the numerical integration method known as Simpson's rule.

To be precise, let's again consider a pair of adjacent segments, which lie between the equally-spaced discretization points {xn1,xn,xn+1}. As derived above, the integral over these segments can be Taylor expanded as

In=2f(xn)Δx+f(xn)3Δx3+O(Δx5)+

By comparison, the mid-point rule and trapeium rule estimators for the integral are

Impn=2f(xn)Δx

Itrapzn=2f(xn)Δx+f(xn)2Δx3+O(Δx5).

Hence, we could take the following weighted average:

Isimpn=13Impn+23Itrapzn=2f(xn)Δx+f(xn)3Δx3+O(Δx5).

Such a weighted average would match the Taylor series result up to O(Δx4)! (You can check for yourself that the O(Δx5) terms differ.) In summary, Simpson's rule for this set of three points can be written as

Isimpn=13[2f(xn)Δx]+23Δx[f(xn1)2+f(xn)+f(xn+1)2]=Δx3[f(xn1)+4f(xn)+f(xn+1)].

The total numerical error, over a set of O(N) segments, is O(1/N4). That is an improvement of two powers of 1/N over the trapzezium and mid-point rules! What's even better is that it involves exactly the same number of arithmetic operations as the trapezium rule. This is as close to a free lunch as you can get in computational science.

9.3.1 Python Implementation of Simpson's Rule

In Scipy, Simpson's rule is implemented by the scipy.integrate.simps function, which is defined in the scipy.integrate submodule. Similar to the trapz function, this can be called as either simps(y,x) or simps(y,dx=s) to estimate the integral ydx, using the elements of x as the discretization points, with y specifying the set of values for the integrand.

Because Simpson's rule requires dividing the segments into pairs, if you specify an even number of discretization points in x (i.e. an odd number of segments), the function deals with this by doing a trapezium rule estimate on the first and last segments. Usually, the error is negligible, so don't worry about this detail

Here is an example of simps in action:

>>> from scipy import *
>>> from scipy.integrate import simps
>>> x = linspace(0,10,25)
>>> y = exp(-x)
>>> t = simps(y,x)
>>> print(t)
1.00011864276

For the same number of discretization points, the trapezium rule gives 1.01438; the exact result is 0.9999546 Clearly, Simpson's rule is more accurate.


This page titled 9.3: Simpson's Rule is shared under a CC BY-SA 4.0 license and was authored, remixed, and/or curated by Y. D. Chong via source content that was edited to the style and standards of the LibreTexts platform.

Support Center

How can we help?