## Numerical Integration: Simpson’s Rules

### Simpson’s ⅓ Rule

Let . By dividing the interval into many subintervals, the Simpson’s 1/3 rule approximates the area under the curve in every subinterval by interpolating between the values of the function at the midpoint and ends of the subinterval, and thus, on each subinterval, the curve to be integrated is a parabola. For simplicity, the width of each subinterval is chosen to be constant and is equal to . Let be the number of intervals with and constant spacing . On each interval with end points and , Lagrange polynomials can be used to define the interpolating parabola as follows:

where is the midpoint in the interval . Integrating the above formula yields:

The Simpson’s 1/3 rule can be implemented as follows:

The following tool illustrates the implementation of the Simpson’s 1/3 rule for integrating the function defined as:

Use the slider to see the effect of increasing the number of intervals on the approximation.

The following Mathematica code can be used to numerically integrate any function on the interval using the Simpson’s 1/3 rule.

View Mathematica CodeIS1[f_, a_, b_, n_] := (h = (b - a)/2/n; h/3*Sum[(f[a + (2i - 2)*h] + 4*f[a + (2i - 1)*h]+f[a + (2i)*h]), {i, 1, n}]) f[x_] := 2 + 2 x + x^2 + Sin[2 Pi*x] + Cos[2 Pi*x/0.5]; IS1[f, 0, 1.5, 1.0] IS1[f, 0, 1.5, 18.0]

import numpy as np def IS1(f, a, b, n): h = (b - a)/2/n return h/3*sum([(f(a + (2*i)*h) + 4*f(a + (2*i + 1)*h) + f(a + (2*i + 2)*h)) for i in range(int(n))]) def f(x): return 2 + 2*x + x**2 + np.sin(2*np.pi*x) + np.cos(2*np.pi*x/0.5) print(IS1(f, 0, 1.5, 1.0)) print(IS1(f, 0, 1.5, 18.0))

The following link provides the MATLAB codes for implementing the Simpson’s 1/3 rule.

#### Error Analysis

One estimate for the upper bound of the error can be derived similar to the derivation of the upper bound of the error in the trapezoidal rule as follows. Given a function and its interpolating polynomial of degree (), the error term between the interpolating polynomial and the function is given by (See the Mathematical Background section for a proof):

Where is in the domain of the function . The error in the calculation of the integral of the parabola number connecting the points , , and will be estimated based on the above formula assuming . Therefore:

Therefore, the upper bound for the error can be given by:

If is the number of subdivisions, where each subdivision has width of , i.e., , then:

However, it can actually be shown that there is a better estimate for the upper bound of the error. This can be shown using Newton interpolating polynomials through the points . The reason we add an extra point is going to become apparent when the integration is carried out. The error term between the interpolating polynomial and the function is given by:

where and is dependent on . The first three terms on the right-hand side are exactly the interpolating parabola passing through the points , , and . Therefore, an estimate for the error can be evaluated as:

The first term on the right-hand side of the inequality is equal to zero. This is because the point is the average of and , so, integrating that cubic polynomial term yields zero. This was the reason to consider a third-order polynomial instead of a second-order polynomial which allows the error term to be in terms of . If is the number of subdivisions, where each subdivision has width of , i.e., , then:

#### Example

Using the Simpson’s 1/3 rule with , calculate and compare with the exact integral of the function on the interval . Find the value of so that the error is less than 0.001.

##### Solution

It is important to note that defines the number of intervals on which a parabola is defined. Each interval has a width of . Since the spacing can be calculated as:

Therefore, , , , , and . The values of the function at these points are given by:

According to the Simpson’s 1/3 rule we have:

Which is already a very good approximation to the exact value of even though only 2 intervals were used.

##### Error Bounds

The total error obtained when is indeed less than the error bounds obtained by the formula listed above. For , the error in the estimation is bounded by:

The error evaluated by is indeed less than that upper bound. The same formula can be used to find the value of so that the error is bounded by 0.001:

Therefore, to guarantee an error less than 0.001 using , the interval will have to be divided into: intervals where a parabola is defined on each! In this case, the value of and .

### Simpson’s ⅜ Rule

Let . By dividing the interval into many subintervals, the Simpson’s 3/8 rule approximates the area under the curve in every subinterval by interpolating between the values of the function at the ends of the subinterval and at two intermediate points, and thus, on each subinterval, the curve to be integrated is a cube. For simplicity, the width of each subinterval is chosen to be constant and is equal to . Let be the number of intervals with and constant spacing with the intermediate points for each interval as and . On each interval with end points and , Lagrange polynomials can be used to define the interpolating cubic polynomial as follows:

Integrating the above formula yields:

The Simpson’s 3/8 rule can be implemented as follows:

The following tool illustrates the implementation of the Simpson’s 3/8 rule for integrating the function defined as:

Use the slider to see the effect of increasing the number of intervals on the approximation.

The following Mathematica code can be used to numerically integrate any function on the interval using the Simpson’s 3/8 rule.

View Mathematica CodeIS2[f_, a_, b_, n_] := (h = (b - a)/3/n;3 h/8*Sum[(f[a + (3 i - 3)*h] + 3*f[a + (3 i - 2)*h] + 3 f[a + (3 i - 1)*h] + f[a + (3 i)*h]), {i, 1, n}]) f[x_] := 2 + 2 x + x^2 + Sin[2 Pi*x] + Cos[2 Pi*x/0.5]; IS2[f, 0, 1.5, 5]

import numpy as np def IS2(f, a, b, n): h = (b - a)/3/n return 3*h/8*sum([(f(a + (3*i)*h) + 3*f(a + (3*i + 1)*h) + 3*f(a + (3*i + 2)*h) + f(a + (3*i + 3)*h)) for i in range(int(n))]) def f(x): return 2 + 2*x + x**2 + np.sin(2*np.pi*x) + np.cos(2*np.pi*x/0.5) print(IS2(f, 0, 1.5, 5))

The following link provides the MATLAB codes for implementing the Simpson’s 3/8 rule.

#### Error Analysis

The estimate for the upper bound of the error can be derived similar to the derivation of the upper bound of the error in the trapezoidal rule as follows. Given a function and its interpolating polynomial of degree (), the error term between the interpolating polynomial and the function is given by (See the Mathematical Background section for a proof):

Where is in the domain of the function . The error in the calculation of the integral of the cubic function number connecting the points , , , and will be estimated based on the above formula assuming . Therefore:

Therefore, the upper bound for the error can be given by:

If is the number of subdivisions, where each subdivision has width of , i.e., , then:

#### Example

Using the Simpson’s 3/8 rule with , calculate and compare with the exact integral of the function on the interval . Find the value of so that the error is less than 0.001.

##### Solution

It is important to note that defines the number of intervals on which a cubic polynomial is defined. Each interval has a width of . Since the spacing can be calculated as:

Therefore, , , , and . The values of the function at these points are given by:

According to the Simpson’s 3/8 rule we have:

Which is already a very good approximation to the exact value of even though only 1 interval was used.

##### Error Bounds

The total error obtained when is indeed less than the error bounds obtained by the formula listed above. For , the error in the estimation is bounded by:

The error is indeed less than that upper bound. The same formula can be used to find the value of so that the error is bounded by 0.001:

Therefore, to guarantee an error less than 0.001 using , the interval will have to be divided into: intervals where a cubic polynomial is defined on each! In this case, the value of and .