You are on page 1of 13

All rights reserved.

May not be reproduced in any form without permission from the publisher, except fair uses permitted under U.S. or applicable copyright law.

5
Numerical Integration

5.1 Introduction
The definite integral
b
I+ f / Ã f +x/ Å x (5.1)
a

is defined in calculus as a limit of what are called Riemann sums. It is then proven that
I+ f / F+b/  F+a/ (5.2)

where F+x/ is any antiderivative of f +x/; this is the Fundamental Theorem of Calculus. Many integrals
can be evaluated by using this formula, and a significant portion of most calculus textbooks is devoted
to this approach. Nonetheless, most integrals cannot be evaluated by using (5.2) because most
integrands f +x/ do not have antiderivatives expressible in terms of elementary functions. Examples of
such integrals are
1 S
x 2
S
Ã Æ Åx à x sin, x 0 Å x (5.3)
0 0
Copyright @ 2010. De Gruyter Oldenbourg.

So, other methods are needed for evaluating such integrals.


170 Mathematics for Engineers
All rights reserved. May not be reproduced in any form without permission from the publisher, except fair uses permitted under U.S. or applicable copyright law.

In the first section of this chapter, we define two of the oldest and most popular numerical methods for
approximating integrals like (5.1): the trapezoidal rule and Simpson's rule. We also analyze the error in
using these methods and then obtain improvements on them. A third method is Gaussian quadrature
which will be discussed in addition to the two methods stated. This method is more complicated in its
origin than Simpson's and the trapezoidal method, but it is almost always much superior in accuracy for
similar amounts of computation.

5.2 Trapezoidal and Simpson Method


The central idea behind most formulas for approximating
b
I+ f / Ã f +x/ Å x (5.4)
a

is to replace f +x/ by an approximating function whose integral can be evaluated. In this section, we will
look at methods based on linear and quadratic interpolation.

5.2.1 Trapezoidal Method


Approximate f +x/ by a linear polynomial
+b  x/ f +a/  +x  a/ f +b/
p1 +x/ (5.5)
ba
which interpolates f +x/ at a and b (see Figure 5.1). The integral of p1 +x/ along #a , b' is the area of the
shaded trapezoid shown in Figure 5.1; it is given by

y f#x'

p1+x/
Copyright @ 2010. De Gruyter Oldenbourg.

a b

Figure 5.1. An illustration of the trapezoidal rule (5.1).


IV - Chapter 5: Numerical Integration 171
All rights reserved. May not be reproduced in any form without permission from the publisher, except fair uses permitted under U.S. or applicable copyright law.

f +a/  f +b/
T1 + f / +b  a/ . (5.6)
2
This approximates the integral I+ f / if f +x/ is almost linear on #a, b'.
Example 5.1. Trapezoidal Method I
Approximate the integral
1 1
I Ã Åx (5.7)
0 1x
Solution 5.1. The true value of this integral is
1 1
I0 Ã Åx
0 x1
log+2/

Defining the integrand as


1
f +x_/ :
x1
and applying the interpolation formula to the interval #0, 1' we get the first approximation
1
T1 +1  0/ + f +0/  f +1//
2
3
4

The approximation deviates from the exact result by


N#I0  T1'
 0.0568528

The approximation is exact in the first digit on the right hand side of the radix point.Ô
To improve the approximation T1 + f / in (5.4) when f +x/ is not a nearly linear function on #a, b', break
the interval #a, b' into smaller subintervals and apply (5.5) on each subinterval. If the subintervals are
small enough, then f +x/ will be nearly linear on each one. This idea is illustrated in Figure 5.2:
Copyright @ 2010. De Gruyter Oldenbourg.
172 Mathematics for Engineers
All rights reserved. May not be reproduced in any form without permission from the publisher, except fair uses permitted under U.S. or applicable copyright law.

y f+x/

p1+x/

x0 x1 x2

a b

Figure 5.2. An illustration of the trapezoidal rule T2 +f / (5.2.1).

Example 5.2. Trapezoidal Method II


Approximate the integral
1 1
I Ã Åx (5.8)
0 1x
by using T1 + f / on two subintervals of equal length.
Solution 5.2. The true value of this integral is
1 1
I0 Ã Åx
0 x1
log+2/

For two subintervals of equal length


1
2 1 1 1
I1 Ã Å x  Ã1 Åx
0 x1 2
x1

3 4
log  log
2 3
Copyright @ 2010. De Gruyter Oldenbourg.

Defining the integrand as


1
f +x_/ :
x1
and applying the interpolation formula to the intervals #0, 1 s 2' and #1 s 2, 1' we get the first
approximation
IV - Chapter 5: Numerical Integration 173
All rights reserved. May not be reproduced in any form without permission from the publisher, except fair uses permitted under U.S. or applicable copyright law.

1 1 1 1 1 1
T2 0 f +0/  f  1 f +1/  f
2 2 2 2 2 2
17
24

The approximation deviates from the exact result by


N#I1  T2'
 0.0151862

The error in T2 is about 1 s 4 of that given for T1 compare Example 5.1.Ô


We will derive a general formula to simplify the calculations when using several subintervals of equal
length. Let the number of subintervals be denoted by n, and let
ba
h (5.9)
n
be the length of each subinterval. The endpoints of the subintervals are given by
xj a jh j 0, 1, …, n. (5.10)

Then break the integral into n subintervals


b xn n1 xi1
I+ f / à f +x/ Å x à f +x/ Å x Åà f +x/ Å x. (5.11)
a x0 i 0 xi

Approximate each subinterval by using a linear interpolating polynomial such as (5.5) and noting that
each subinterval #xi , xi1 ' has length h. Then
n1 n2
h 1 1
Tn + f / Å + f +xi /  f +xi1 // h f +x0 /  f +xn /  Å f +xi / . (5.12)
i 0
2 2 2 i 1

This is called the trapezoidal numerical integration rule. The subscript n gives the number of
subintervals being used; and the points x0 , x1 , …, xn are called the numerical integration node points.
The following lines show an implementation of the trapezoidal method in a few steps using formula
(5.12). The implementation is straight forward and does not use special features of the evaluation.
Copyright @ 2010. De Gruyter Oldenbourg.
174 Mathematics for Engineers
All rights reserved. May not be reproduced in any form without permission from the publisher, except fair uses permitted under U.S. or applicable copyright law.

trapezoidalMethod$f_, x_, a_, b_ , n_( : Block%h, xval, fval,


+  determine the step length  /
ba
h ;
n
+  generate the integration nodes  /
xval Table#a  i h, i, 0, n';
+  function values are generated  /
fval Map#+f s. x ! Ó/ &, xval';
First#fval'
fval317 ;
2
Last#fval'
fval3Length#fval'7 ;
2
+  sum up the terms according to +5.12/  /
N#h Fold#Plus, 0, fval''
)

x2 Æx
2
The following line is an application of the function trapezoidalMethod to the function f +x/

trapezoidalMethod,x2 Æx , x, 0, 1, 20


2

0.18932

Before giving some numerical examples of Tn + f /, we would like to discuss the choice of n. With a
sequence of increasing values of n, Tn + f / will usually be an increasingly accurate approximation of
I+ f /. But which sequence of values of n should be used? If n is doubled repeatedly, then the function
values used in each T2 n + f / will include all of the earlier function values used in the preceding Tn + f /.
Thus, the doubling of n will ensure that all previously computed information is used in the new
calculation, making the trapezoidal rule less expensive than it would be otherwise. To illustrate how
function values are reduced when n is doubled, consider T2 + f / and T4 + f /.
f +x0 / f +x2 / ba ab
T2 + f / h  f +x1 /  with h , x0 a, x1 , x2 b. (5.13)
2 2 2 2
Also
f +x0 / f +x4 / ba
T4 + f / h  f +x1 /  f +x2 /  f +x3 /  with h ,
Copyright @ 2010. De Gruyter Oldenbourg.

2 2 4
(5.14)
3ab ab a3b
x0 a, x1 , x2 , x3 , x4 b.
4 2 4
Comparing the two approximations, we observe that f +x1 / and f +x3 / need to be evaluated, as the other
function values are known from the lower approximation. For this and other reasons, all of our
examples of Tn + f / are based on doubling n.
IV - Chapter 5: Numerical Integration 175
All rights reserved. May not be reproduced in any form without permission from the publisher, except fair uses permitted under U.S. or applicable copyright law.

Example 5.3. Higher Trapezoidal Method


Here we will calculate Tn + f / using trapezoidal interpolation for three different functions. The func-
tions are:
f +x_/ : Æx
2

for the interval #0, 1'.


1
g+x_/ :
x2  1
for the interval #0, 4' and
1
h+x_/ :
cos+x/  2

for the interval #0, 2 S'. The number of iterations are n 2, 4, 8, 16, 32, 64, 128, 256.
Solution 5.3. The exact values for these functions can be determined by using the antiderivatives
of the functions
1
I1 Ã f +x/ Å x
0

1
S erf+1/
2
4
I2 Ã g+x/ Å x
0

1
tan +4/

and
2S
I3 Ã h+x/ Å x
0

2S

The approximation for the different functions and different iteration numbers follow from the lines
below
Copyright @ 2010. De Gruyter Oldenbourg.

Tf +trapezoidalMethod+ f +x/, x, 0, 1, Ó1/ &/ s 2, 4, 8, 16, 32, 64, 128, 256

0.73137, 0.742984, 0.745866, 0.746585, 0.746764, 0.746809, 0.74682, 0.746823

Tg +trapezoidalMethod+g+x/, x, 0, 4, Ó1/ &/ s 2, 4, 8, 16, 32, 64, 128, 256

1.45882, 1.32941, 1.32525, 1.32567, 1.32578, 1.32581, 1.32582, 1.32582


176 Mathematics for Engineers
All rights reserved. May not be reproduced in any form without permission from the publisher, except fair uses permitted under U.S. or applicable copyright law.

Th +trapezoidalMethod+h+x/, x, 0, 2 S, Ó1/ &/ s 2, 4, 8, 16, 32, 64, 128, 256

4.18879, 3.66519, 3.62779, 3.6276, 3.6276, 3.6276, 3.6276, 3.6276

The errors with respect to the exact value of the integrals are
H1 I1  Tf
0.0154539, 0.00384004, 0.000958518, 0.000239536,
0.0000598782, 0.0000149692, 3.74227 — 106 , 9.35566 — 107

H2 I2  Tg

 0.133006, 0.0035941, 0.000564261, 0.000144082,


0.000036038, 9.01059 — 106 , 2.25272 — 106 , 5.63183 — 107

H3 I3  Th
 0.561191, 0.0375927, 0.000192788,
5.12258 — 109 , 8.88178 — 1016 , 8.88178 — 1016 , 0., 8.88178 — 1016

These data are collected in the following table for each integral and each number of used interpola-
tion steps.
TableForm$Prepend$2, 4, 8, 16, 32, 64, 128, 256, H1, H2, H3¬ , n, "H1", "H2", "H3"((

n H1 H2 H3
2 0.0154539 0.133006 0.561191
4 0.00384004 0.0035941 0.0375927
8 0.000958518 0.000564261 0.000192788
16 0.000239536 0.000144082 5.12258 — 109
32 0.0000598782 0.000036038 8.88178 — 1016
6
64 0.0000149692 9.01059 — 10 8.88178 — 1016
128 3.74227 — 106 2.25272 — 106 0.
256 9.35566 — 107 5.63183 — 107 8.88178 — 1016

From the table it is obvious that the error decreases with increasing n. The third example converges
very rapidly.Ô
Copyright @ 2010. De Gruyter Oldenbourg.

5.2.2 Simpson's Method


To improve trapezoidal integration in formula (5.6) we have to go to higher interpolations to
approximate f +x/ on #a, b'. Let p2 +x/ be the quadratic polynomial that interpolates f +x/ at a,
c +a  b/ s 2 and b. Using this idea in the approximation of I+ f /, we get
IV - Chapter 5: Numerical Integration 177
All rights reserved. May not be reproduced in any form without permission from the publisher, except fair uses permitted under U.S. or applicable copyright law.

b
I+ f / ž à p2 +x/ Å x
a
(5.15)
b +x  c/ +x  b/ +x  a/ +x  b/ +x  a/ +x  c/
à  f +a/  f +c/  f +b/! Šx.
a +a  c/ +a  b/ +c  a/ +c  b/ +b  a/ +b  c/
This integral can be evaluated directly, but it is easier to first introduce h +b  a/ s 2 and then change
the variable of integration. We will evaluate the first term to illustrate the general procedure. Let
u x  a. Then
+x  b/ +x  c/
t1 Simplify% s. b ‘ a  2 h, c ‘ a  h)
+a  b/ +a  c/
+a  h  x/ +a  2 h  x/
2 h2

t2 t1 s. x ‘ a  u
+h  u/ +2 h  u/
2 h2
b +x  c/ +x  b/ a2 h
à Åx à +x  c/ +x  b/ Å x
a +a  c/ +a  b/ a
(5.16)
1 2h 1 u3 3 h
à +u  h/ +u  2 h/ Šu   u h  2 h u!
2 2 2h
0 .
2 h2 0 2 h2 3 2 3
The complete evaluation of (5.15) yields
h ab
S2 + f / % f +a/  4 f  f +b/). (5.17)
3 2
The method is illustrated in Figure 5.3:
Copyright @ 2010. De Gruyter Oldenbourg.
178 Mathematics for Engineers
All rights reserved. May not be reproduced in any form without permission from the publisher, except fair uses permitted under U.S. or applicable copyright law.

Figure 5.3. An illustration of Simpson's rule S2 +f / (5.17).

The symbolic derivation of his formula takes the same steps as discussed above. First define the
polynomial
E+a/ ++x  b/ +x  c// E+b/ ++x  a/ +x  c// E+c/ ++x  a/ +x  b//
p2+x_/ :  
+a  b/ +a  c/ +b  a/ +b  c/ +c  a/ +c  b/

Then replace the points of interpolation with the specific values


ab
res1 Simplify%p2+x/ ss. c ‘ , b ‘ a  2 h!)
2
E+a/ ,a2  3 a h  2 a x  2 h2  3 h x  x2 0  +a  x/ +2 +a  2 h  x/ E+a  h/  +a  h  x/ E+a  2 h//
2 h2

and integrate the resulting polynomial with respect to x


a2 h ba
Simplify%à res1 Å x s. h ‘ )
a 2
1 ab
 +a  b/ 4 E  E+a/  E+b/
6 2

In this formula we used E+x/ f +x/.


Copyright @ 2010. De Gruyter Oldenbourg.

Example 5.4. Simpson's Rule I


Approximate the integral
1 1
I Ã Åx (5.18)
0 1x
by using Simpson's rule.
IV - Chapter 5: Numerical Integration 179
All rights reserved. May not be reproduced in any form without permission from the publisher, except fair uses permitted under U.S. or applicable copyright law.

Solution 5.4. First define the function


1
f +x_/ :
x1
and the step h
ba
h s. a ‘ 0, b ‘ 1
2
1
2

Then use the rule which delivers


1 01
S2 f +0/  f +1/  4 f h
3 2
25
36

The error now is


1 1
H0 Ã Å x  S2
0 x1
25
log+2/ 
36

N#H0'
 0.00129726

To compare this with the trapezoidal rule, use T2 , since the number of function evaluation is the
same for both S2 and T2 by a factor of about 11, a significant increase in accuracy
N#T2  S2'
0.0138889

T2  S2
N% )
H0
 10.7063
Copyright @ 2010. De Gruyter Oldenbourg.

The accuracy is much better in a Simpson integration than in a trapezoidal integration.Ô


The rule S2 + f / will be an accurate approximation to I+ f / if f +x/ is nearly quadratic on #a, b'. For other
cases, proceed in the same manner as for the trapezoidal rule. Let n be an even integer, h +b  a/ s n,
and define the evaluation points for f +x/ by
xj a jh j 0, 1, 2, …, n. (5.19)
180 Mathematics for Engineers
All rights reserved. May not be reproduced in any form without permission from the publisher, except fair uses permitted under U.S. or applicable copyright law.

Follow the ideas as stated for the trapezoidal method to introduce subintervals into the interval
#a, b' #x0 , xn '. The intervals should be not too small and containing three interpolation points. Thus,
b xn n1 xi1
I+ f / à f +x/ Å x à f +x/ Å x Åà f +x/ Å x (5.20)
a x0 i 0 xi

Approximate each subinterval by (5.17). This yields


h
I+ f / ž # f +x0 /  4 f +x1 /  f +x2 / 
3 (5.21)
 f +x2 /  4 f +x3 /  f +x4 /  …   f +xn2 /  4 f +xn1 /  f +xn /'.
If these terms are combined and simplified, we obtain the formula
h
Sn + f /  f +x0 /  4 f +x1 /  2 f +x2 /  …  2 f +xn2 /  4 f +xn1 /  f +xn /. (5.22)
3
This is called Simpson's rule, and it has been among the most popular numerical integration methods
for more than two centuries. The index n gives the number of subdivisions used in defining the
integration node points x0 , x1 , x2 , …, xn . The following Mathematica function shows a simple
implementation of Simpson's rule.
simpsonMethod$f_, x_, a_, b_ , n_( : Block%h, xval, fval, intval,
+  determine the step length  /
ba
h ;
n
+  generate the integration nodes  /
xval Table#a  i h, i, 0, n';
+  evaluate the function values  /
fval Map#+f s. x ! Ó/ &, xval';
intval First#fval', Last#fval';
intval Join#intval, 2 Take#fval, 2, Length#fval'  1'';
intval Flatten#Join#intval, 2 Take#fval, 2, Length#fval'  1, 2''';
+  sum up the terms  /
h
N% Fold#Plus, 0, intval')
3
Copyright @ 2010. De Gruyter Oldenbourg.

Æx for x ± #0, 2'.


2
The next line shows an application of the function to f +x/

simpsonMethod,Æx , x, 0, 2, 300


2

0.882081
IV - Chapter 5: Numerical Integration 181
All rights reserved. May not be reproduced in any form without permission from the publisher, except fair uses permitted under U.S. or applicable copyright law.

Example 5.5. Higher Simpson Method


Here we will calculate Sn + f / using Simpson's interpolation for three different functions. The func-
tions are:
f +x_/ : Æx
2

for the interval #0, 1'.


1
g+x_/ :
x2  1
for the interval #0, 4' and
1
e+x_/ :
cos+x/  2

for the interval #0, 2 S'. The number of iterations are n 2, 4, 8, 16, 32, 64, 128, 256.
Solution 5.5. The exact values for these functions can be determined by using the antiderivatives
of the functions
1
I1 Ã f +x/ Å x
0

1
S erf+1/
2
4
I2 Ã g+x/ Å x
0

1
tan +4/

and
2S
I3 Ã e+x/ Å x
0

2S

The approximation for the different functions and different iteration numbers follow from the lines
below
Sf +simpsonMethod+ f +x/, x, 0, 1, Ó1/ &/ s 2, 4, 8, 16, 32, 64, 128, 256
Copyright @ 2010. De Gruyter Oldenbourg.

0.74718, 0.746855, 0.746826, 0.746824, 0.746824, 0.746824, 0.746824, 0.746824

Sg +simpsonMethod+g+x/, x, 0, 4, Ó1/ &/ s 2, 4, 8, 16, 32, 64, 128, 256

1.23922, 1.28627, 1.32387, 1.32581, 1.32582, 1.32582, 1.32582, 1.32582

Se +simpsonMethod+e+x/, x, 0, 2 S, Ó1/ &/ s 2, 4, 8, 16, 32, 64, 128, 256

4.88692, 3.49066, 3.61532, 3.62753, 3.6276, 3.6276, 3.6276, 3.6276

You might also like