Newton–Cotes Formulas
6.2 Newton–Cotes Formulas
f (x)
P n -1 (x)
Figure 6.1. Polynomial approximation of f (x).
x 1 x 2 x 3 x 4 x n -1 x n x a b
Consider the definite integral
f (x) dx
We divide the range of integration (a, b) into n − 1 equal intervals of length h = (b − a)/(n − 1) each, as shown in Fig. 6.1, and denote the abscissas of the resulting
nodes by x 1 , x 2 ,..., x n . Next we approximate f (x) by a polynomial of degree n − 1 that intersects all the nodes. Lagrange’s form of this polynomial, Eq. (3.1a), is
where ℓ i (x) are the cardinal functions defined in Eq. (3.1b). Therefore, an approxima- tion to the integral in Eq. (6.1) is
I=
P n−1 (x)dx =
f (x i )
ℓ i (x)dx =
A i f (x i ) (6.2a)
ℓ i (x)dx,
i = 1, 2, . . . , n
(6.2b)
Equations (6.2) are the Newton–Cotes formulas. Classical examples of these formu- las are the trapezoidal rule (n = 2), Simpson’s rule (n = 3) and Simpson’s 3/8 rule (n = 4). The most important of these is the trapezoidal rule. It can be combined with
Richardson extrapolation into an efficient algorithm known as Romberg integration, which makes the other classical rules somewhat redundant.
Trapezoidal Rule
f (x) E
Area = I
Figure 6.2. Trapezoidal rule.
x =a 1 x =b 2 x
If n = 2 , we have ℓ 1 = (x − x 2 )/(x 1 −x 2 ) = −(x − b)/ h. Therefore,
Also ℓ 2 = (x − x 1 )/(x 2 −x 1 ) = (x − a)/ h, so that
h (x − a) dx = 2h (b − a) a = 2 Substitution in Eq. (6.2a) yields
I = [ f (a) + f (b)]
which is known as the trapezoidal rule. It represents the area of the trapezoid in Fig. 6.2. The error in the trapezoidal rule
E=
f (x)dx − I
is the area of the region between f (x) and the straight-line interpolant, as indicated in Fig. 6.2. It can be obtained by integrating the interpolation error in Eq. (4.3):
(x − a)(x − b)dx
Composite Trapezoidal Rule
f (x )
Figure 6.3. Composite trapezoidal rule.
x i +1
x n -1 x n x
In practice the trapezoidal rule is applied in a piecewise fashion. Figure 6.3 shows the region (a, b) divided into n − 1 panels, each of width h. The function f (x) to be
integrated is approximated by a straight line in each panel. From the trapezoidal rule we obtain for the approximate area of a typical (ith) panel
I i = [ f (x i ) + f (x i+1 )]
Hence total area, representing + b
I i = [ f (x 1 ) + 2 f (x 2 ) + 2 f (x 3 ) + · · · + 2 f (x n−1 ) + f (x n )] (6.5)
2 which is the composite trapezoidal rule.
i=1
The truncation error in the area of a panel is from Eq. (6.4),
where ξ i lies in (x i , x i+1 ). Hence the truncation error in Eq. (6.5) is
where ¯ f ′′ is the arithmetic mean of the second derivatives. If f ′′ (x) is continuous, there must be a point ξ in (a, b) at which f ′′ (ξ ) = ¯ f ′′ , enabling us to write
Therefore, Eq. (a) becomes
It would be incorrect to conclude from Eq. (6.6) that E = ch 2 (c being a constant), because f ′′ (ξ ) is not entirely independent of h. A deeper analysis of the error 10 shows that if f (x) and its derivatives are finite in (a, b), then
1 h 2 +c 2 h E=c 4 +c 3 h 6 +···
10 The analysis requires familiarity with the Euler–Maclaurin summation formula, which is covered in advanced texts.
Recursive Trapezoidal Rule
Let I k
be the integral evaluated with the composite trapezoidal rule using 2 k−1 panels. Note that if k is increased by one, the number of panels is doubled. Using the notation
H=b−a
we obtain from Eq. (6.5) the following results for k = 1, 2 and 3. k = 1 (1 panel):
I 1 = [ f (a) + f (b)]
k = 2 (2 panels):
"H
H $H
2 + f (b) 4 = 2 1 +f a+ 2 2 k = 3 (4 panels):
We can now see that for arbitrary k > 1 we have
, k = 2, 3, . . . (6.9a)
2 k−1
i=1
which is the recursive trapezoidal rule. Observe that the summation contains only the new nodes that were created when the number of panels was doubled. Therefore,
the computation of the sequence I 1 , I 2 , I 3 ,..., I k from Eqs. (6.8) and (6.9) involves the same amount of algebra as the calculation of I k directly from Eq. (6.5). The advantage of using the recursive trapezoidal rule is that it allows us to monitor convergence and terminate the process when the difference between I k−1 and I k becomes sufficiently small. A form of Eq. (6.9a) that is easier to remember is
I (h) =
2 I (2h) + h
f (x new )
(6.9b)
where h = H/(n − 1) is the width of each panel.
The function trapezoid computes I (h), given I (2h) from Eqs. (6.8) and (6.9). We can compute + b
a f (x) dx by calling trapezoid repeatedly with k = 1, 2, . . . until the desired precision is attained.
function Ih = trapezoid(func,a,b,I2h,k) % Recursive trapezoidal rule. % USAGE: Ih = trapezoid(func,a,b,I2h,k) % func = handle of function being integrated. % a,b
= limits of integration. % I2h
= integral with 2ˆ(k-1) panels. % Ih = integral with 2ˆk panels.
if k == 1 fa = feval(func,a); fb = feval(func,b); Ih = (fa + fb)*(b - a)/2.0; else n = 2ˆ(k -2 );
% Number of new points
h = (b - a)/n ;
% Spacing of new points
x = a + h/2.0; % Coord. of 1st new point sum = 0.0; for i = 1:n
fx = feval(func,x); sum = sum + fx; x = x + h;
end Ih = (I2h + h*sum)/2.0;
end
Simpson’s Rules
Parabola f ( x )
Figure 6.4. Simpson’s 1/3 rule.
x =a
x =b 1 x x 2 3
Simpson’s 1/3 rule can be obtained from Newton–Cotes formulas with n = 3; that is, by passing a parabolic interpolant through three adjacent nodes, as shown in
Fig. 6.4. The area under the parabola, which represents an approximation of + b
a f (x) dx, is (see derivation in Example 6.1)
#a+b $
"h
I=
f (a) + 4 f
2 + f (b) 3
(a) (a)
Figure 6.5. Composite Simpson’s 1/3 rule. x 1 x i
x i +1 x i +2
To obtain the composite Simpson’s 1/3 rule, the integration range (a, b) is divided into n − 1 panels (n odd) of width h = (b − a)/(n − 1) each, as indicated in Fig. 6.5.
Applying Eq. (a) to two adjacent panels, we have
, x i+2
f (x) dx ≈ [ f (x i ) + 4 f (x i+1 ) + f (x i+2 )] (b)
3 Substituting Eq. (b) into
n−2
!, x i+2
f (x)dx =
f (x) dx =
f (x)dx
a x 1 i=1,3,...
yields , b
f (x) dx ≈
I = [ f (x 1 ) + 4 f (x 2 ) + 2 f (x 3 ) + 4 f (x 4 )+··· (6.10)
h · · · + 2 f (x n−2 ) + 4 f (x n−1 ) + f (x n )] 3
The composite Simpson’s 1/3 rule in Eq. (6.10) is perhaps the best-known method of numerical integration. Its reputation is somewhat undeserved, since the trapezoidal rule is more robust, and Romberg integration is more efficient.
The error in the composite Simpson’s rule is
from which we conclude that Eq. (6.10) is exact if f (x) is a polynomial of degree three or less.
Simpson’s 1/3 rule requires the number of panels to be even. If this condition is not satisfied, we can integrate over the first (or last) three panels with Simpson’s 3/8 rule:
8 and use Simpson’s 1/3 rule for the remaining panels. The error in Eq. (6.12) is of the
same order as in Eq. (6.10). EXAMPLE 6.1
Derive Simpson’s 1/3 rule from Newton–Cotes formulas.
Solution Referring to Fig. 6.4, we see that Simpson’s 1/3 rule uses three nodes located at x 1 = a, x 2 = (a + b) /2 and x 3 = b. The spacing of the nodes is h = (b − a)/2. The cardinal functions of Lagrange’s three-point interpolation are (see Art. 3.2)
The integration of these functions is easier if we introduce the variable ξ with origin at x 2 . Then the coordinates of the nodes are ξ +
1 = −h, ξ 2 + = 0, ξ 3 = h and Eq. (6.2b) becomes A
i (x)dx = −h ℓ i (ξ )dξ . Therefore,
A (ξ − 0)(ξ − h)
(−h)(−2h)
2h 2
−h
A (ξ + h)(ξ − h)
(h)(−h)
A (ξ + h)(ξ − 0)
dξ = 2h 2 (ξ + hξ )dξ =
3 Equation (6.2a) then yields
−h
(2h)(h)
−h
#a+b $
"h
2 i=1 + f (b) 3 which is Simpson’s 1/3 rule. EXAMPLE 6.2
Evaluate the bounds on + π
0 sin(x) dx with the composite trapezoidal rule using (1) eight panels and (2) sixteen panels.
Solution of Part (1) With 8 panels there are 9 nodes spaced at h = π/8. The abscissas of the nodes are x i = (i − 1)π/8, i = 1, 2, . . . , 9. From Eq. (6.5) we get
8 iπ ' π
8 i=2 + sin π 16 = 1.97423 The error is given by Eq. (6.6): (b − a)h 2 2 f 3 ′′ (π − 0)(π/8) π
I= sin 0 + 2
sin
12 (− sin ξ) = 768 where 0 < ξ < π . Since we do not know the value of ξ , we cannot evaluate E , but we can determine its bounds:
E=− sin ξ
E min =
768 sin(0) = 0
E max =
sin = 0.040 37
Therefore, I + E min < + π 0 sin(x) dx < I + E max , or , π
sin(x) dx < 2.014 60
The exact integral is, of course, I = 2. Solution of Part (2) The new nodes created by the doubling of panels are located at
midpoints of the old panels. Their abscissas are
16 + ( j − 1) 8 = (2 j − 1) , 16 j = 1, 2, . . . , 8 Using the recursive trapezoidal rule in Eq. (6.9b), we get
sin (2 j − 1)π
16 = 1. 993 58 and the bounds on the error become (note that E is quartered when h is halved)
I=
2 + 16 j=1
E min = 0, E max = 0.040 37/4 = 0.010 09. Hence
sin(x) dx < 2.003 67
EXAMPLE 6.3 Estimate + 2.5
0 f (x) dx from the data
f (x) 1.5000 2.0000 2.0000 1.6364 1.2500 0.9565 Solution We will use Simpson’s rules, since they are more accurate than the trape-
zoidal rule. Because the number of panels is odd, we compute the integral over the first three panels by Simpson’s 3/8 rule, and use the 1/3 rule for the last two panels:
I = [ f (0) + 3 f (0.5) + 3 f (1.0) + f (1.5)] 8
0.5 + [ f (1.5) + 4 f (2.0) + f (2.5)] 3
EXAMPLE 6.4 Use the recursive trapezoidal rule to evaluate + π √
0 x cos x dx to six decimal places. How many function evaluations are required to achieve this result?
Solution The program listed below utilizes the function trapezoid . Apart from the value of the integral, it displays the number of function evaluations used in the computation.
% Example 6.4 (Recursive trapezoidal rule) format long
% Display extra precision
I2h = 0; for k = 1:20
Ih = trapezoid(@fex6 _ 4,0,pi,I2h,k); if (k > 1 & abs(Ih - I2h) < 1.0e-6)
Integral = Ih No _ of _ func _ evaluations = 2ˆ(k-1) + 1 return
end I2h = Ih;
end error(’Too many iterations’)
The M-file containing the function to be integrated is function y = fex6 _ 4(x)
% Function used in Example 6.4 y = sqrt(x)*cos(x);
Here is the output: >> Integral =
-0.89483166485329 No _ of _ func _ evaluations = 32769
Rounding to six decimal places, we have + π √
0 x cos x dx = −0.894 832 The number of function evaluations is unusually large in this problem. The slow
convergence is the result of the derivatives of f (x) being singular at x = 0. Conse- quently, the error does not behave as shown in Eq. (6.7): E = c 1 h 2 +c 2 h 4 + · · ·, but is unpredictable. Difficulties of this nature can often be remedied by a change in vari-
able. In this case, we introduce t = x, so that dt = dx/(2 x) = dx/(2t), or dx = 2t dt. Thus
x cos x dx =
2t 2 cos t 2 dt
Evaluation of the integral on the right-hand side would require 4097 function evaluations.