Sie sind auf Seite 1von 15

Numerical Integration

1.1 Introduction
The definite integral
I(f ) =
_
a
b
f (x) dx
is defined in calculus as a limit of what are called Riemann sums. It is then
proven that
I(f) = F(b) - F(a)
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 (0.0) because most
integrands f (x) do not have antiderivatives expressible in terms of elementary
functions. Examples of such integrals are
_
0
1
:
x
2
dx
_
0
r
x
r
sin_ x dx
So other methods are needed for evaluating such integrals.
In the first section of this chapter, we define two of the oldest and most popular
numerical methods for approximating integrals like (0.0): 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.
2011 G. Baumann
1.2 Trapezoidal Method
The central idea behind most formulas for approximating
I(f ) =
_
a
b
f (x) dx
is to replace f (x) by an approximating function whose integral can be evaluated.
I this section we will look at methods based on linear and quadratic
interpolation.
1.2.1 Trapezoidal Method
Approximate f (x) by a linear polynomial
p
1
(x) =
(bx) f (a) +(x a) f (b)
ba
which interpolates f (x) at a and b (see Figure 0.0). The integral of p
1
(x) over
[a, b] is the area of the shaded trapezoid shown in Figure 0.0; it is given by
y=f [x]
p1(x)
a b
2 Lecture_008.nb
2011 G. Baumann
Figure 0.0. An illustration of the trapezoidal rule (0.0).
T
1
(f ) = (ba)
f (a) +f (b)
2
.
This approximates the integral I(f ) if f (x) is almost linear on [a, b].
Example 0.0. Trapezoidal Method I
Approximate the integral
I =
_
0
1 1
1 +x
dx
Solution 0.1. The true value of this integral is
I0 =

0
1 1
x + 1
dx
log(2)
Defining the integrand as
f +x_/ :=
1
x + 1
and applying the interpolation formula to the interval [0, 1] we get the first
approximation
T1 =
1
2
+1 + 0/ + f +0/ + f +1//
3
4
The approximation deviates from the exact result by
Lecture_008.nb 3
2011 G. Baumann
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 T
1
(f ) in (0.0) when f (x) is not a nearly linear
function on [a, b], break the interval [a, b] into smaller subintervals and apply
(0.0) 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 0.0
y=f (x)
p1(x)
a b
x0 x1 x2
Figure 0.0. An illustration of the trapezoidal rule T
2
(f ) (5.2.1).
Example 0.0. Trapezoidal Method II
Approximate the integral
I =
_
0
1 1
1 +x
dx
by using T
1
(f ) on two subintervals of equal length.
Solution 0.2. The true value of this integral is
4 Lecture_008.nb
2011 G. Baumann
I0 =

0
1 1
x + 1
dx
log(2)
For two subintervals of equal length
I1 =

0
1
2
1
x + 1
dx +

1
2
1 1
x + 1
dx
log
3
2
+log
4
3
Defining the integrand as
f +x_/ :=
1
x + 1
and applying the interpolation formula to the intervals [0, 1f 2] and [1f 2, 1] we
get the first approximation
T2 =
1
2
1
2
+ 0 f +0/ + f
1
2
+
1
2
1 -
1
2
f +1/ + f
1
2
17
24
The approximation deviates from the exact result by
N#I1 - T2'
0.0151862
The error in T
2
is about 1f 4 of that given for T
1
compare Example 0.0.
We will derive a general formula to simplify the calculations when using several
Lecture_008.nb 5
2011 G. Baumann
subintervals of equal length. Let the number of subintervals be denoted by n,
and let
h =
b a
n
be the length of each subinterval. The endpoints of the subintervals are given by
x
j
= a+ j h j = 0, 1, , n.
Then break the integral into n subintervals
I(f ) =
_
a
b
f (x) dx =
_
x
0
x
n
f (x) dx = _
i=0
n1
_
x
i
x
i+1
f (x) dx.
Approximate each subinterval by using a linear interpolating polynomial such as
(0.0) and noting that each subinterval [x
i
, x
i+1
] has length h. Then
T
n
(f ) = _
i=0
n1
h
2
(f (x
i
) +f (x
i+1
)) = h
1
2
f (x
0
) +
1
2
f (x
n
) + _
i=1
n2
f (x
i
) .
This is called the trapezoidal numerical integration rule. The subscript n gives
the number of subintervals being used; and the points x
0
, x
1
, , x
n
are called
the numerical integration node points.
The following lines show an implementation of the trapezoidal method in a few
steps using formula (0.0). The implementation is straight forward and does not
use special features of the evaluation.
6 Lecture_008.nb
2011 G. Baumann
trapezoidalMethod#f_, x_, a_, b_, n_' :
Block%h, xval, fval,
+ determine the step length /
h
b a
n
;
+ generate the integration nodes /
xval Table#a i h, i, 0, n';
+ function values are generated /
fval Map#+f s. x ! / &, xval';
fval317
First#fval'
2
;
fval3Length#fval'7
Last#fval'
2
;
+ sum up the terms according to +5.12/ /
N#h Fold#Plus, 0, fval''
)
The following line is an application of the function trapezoidalMethod to the
function f (x) = x
2
:
x
2
trapezoidalMethod-x
2
z
-x
2
, x, 0, 1, 21
0.18932
Before giving some numerical examples of T
n
(f ), we would like to discuss the
choice of n. With a sequence of increasing values of n, T
n
(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 T
2 n
(f ) will include all of the earlier function values used in the preceding
T
n
(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 T
2
(f ) and T
4
(f ).
Lecture_008.nb 7
2011 G. Baumann
T
2
(f ) = h
f (x
0
)
2
+f (x
1
) +
f (x
2
)
2
with h =
b a
2
, x
0
= a, x
1
=
a+b
2
, x
2
= b.
Also
T
4
(f ) = h
f (x
0
)
2
+f (x
1
) +f (x
2
) +f (x
3
) +
f (x
4
)
2
with h =
b a
4
,
x
0
= a, x
1
=
3 a+b
4
, x
2
=
a +b
2
, x
3
=
a +3 b
4
, x
4
= b.
Comparing the two approximations, we observe that f (x
1
) and f (x
3
) 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 T
n
(f ) are
based on doubling n.
Example 0.0. Higher Trapezoidal Method
Here we will calculate T
n
(f ) using trapezoidal interpolation for three different
functions. The functions are:
f +x_/ := z
-x
2
for the interval [0, 1].
g+x_/ :=
1
x
2
+ 1
for the interval [0, 4] and
h+x_/ :=
1
cos+x/ + 2
for the interval [0, 2 r]. The number of iterations are
n = 2, 4, 8, 16, 32, 64, 128, 256.
Solution 0.3. The exact values for these functions can be determined by using
the antiderivatives of the functions
8 Lecture_008.nb
2011 G. Baumann
I1 =

0
1
f +x/ dx
1
2
r erf(1)
I2 =

0
4
g+x/ dx
tan
1
(4)
and
I3 =

0
2 n
h+x/ dx
2 r
3
The approximation for the different functions and different iteration numbers
follow from the lines below
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
Lecture_008.nb 9
2011 G. Baumann
Th = +trapezoidalMethod+h+x/, x, 0, 2 n, 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
e1 = I1 - Tf
|0.0154539, 0.00384004, 0.000958518, 0.000239536,
0.0000598782, 0.0000149692, 3.74227 10
6
, 9.35566 10
7
|
e2 = I2 - Tg
|0.133006, 0.0035941, 0.000564261, 0.000144082,
0.000036038, 9.01059 10
6
, 2.25272 10
6
, 5.63183 10
7
|
e3 = I3 - Th
|0.561191, 0.0375927, 0.000192788, 5.12258 10
9
,
8.88178 10
16
, 8.88178 10
16
, 0., 8.88178 10
16
|
These data are collected in the following table for each integral and each
number of used interpolation steps.
10 Lecture_008.nb
2011 G. Baumann
TableForm$Prepend$2, 4, 8, 16, 32, 64, 128, 256, e1, e2, e3

,
n, "e1", "e2", "e3"((
n e1 e2 e3
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 10
9
32 0.0000598782 0.000036038 8.88178 10
16
64 0.0000149692 9.01059 10
6
8.88178 10
16
128 3.74227 10
6
2.25272 10
6
0.
256 9.35566 10
7
5.63183 10
7
8.88178 10
16
From the table it is obvious that the error decreases with increasing n. The third
example converges very rapidly.
1.2.2 Generalized Integration Rules
The generalization of the Trapezoidal and Simpson's rule are known as Newton-
Cotes formulas. The integration (quadrature) formulas by Newton-Cotes can be
derived if the integrand f is replaced by an appropriate interpolation polynomial
p
n
(x) so that
_
a
b
f (x) dx
_
a
b
p
n
(x) dx
For the following calculation we assume an equidistant partition of the interval
[a, b] with
x
i
= a+i h and i = 0, 1, 2, , n
where h = (ba) f n and n > 0 and n e N
0
. If we assume that the interpolation
polynomial is generated by a Lagrange polynomial of order n then we have
p
n
(x) = _
i=0
n
f
i
L
i
(x) with L
i
(x) = [
k=i
k=0
n
x x
k
x
i
x
k
and f
i
= f (x
i
) for i = 0, 1, 2, , n. Introducing new variables with x = a +h t we
Lecture_008.nb 11
2011 G. Baumann
can rewrite the Lagrange polynomial in a simpler form
L
i
(x) =
i
(t) = [
k=i
k=0
n
t k
i k
.
This representation can be used in the integration of the polynomial p
n
(x) as
_
a
b
p
n
(x) dx = _
i=0
n
f
i _
a
b
L
i
(x) dx = h _
i=0
n
f
i _
0
n

i
(t) dt = h _
i=0
n
f
i
o
i
.
The weights o
i
are functions of n only and do not depend on the function values
of f .
Defining the Lagrange polynomials in their reduced representation is straight
forward in Mathematica and allows us to use them in the calculation of the
weights. The reduced Lagrange polynomials are defined by
lagrangePolynomial+n_, i_/ :=
k=0
n
If%i = k,
t - k
i - k
, 1)
This function delivers the corresponding function
i
(t) by
lagrangePolynomial+3, 1/
1
2
(2 t) (3 t) t
These functions can be used in the calculations of the coefficients o
i
. The
following calculation lists the weights for n = 2 corresponding to the 1 f 3
Simpson rule
n = 2; Table%

0
n
lagrangePolynomial+n, i/ dt, i, 0, n)
{
1
3
,
4
3
,
1
3

12 Lecture_008.nb
2011 G. Baumann
The weights in general satisfy the property
_
i=0
n
o
i
= n
We can check this relation with our function.
n = 4; Fold%Plus, 0, Table%

0
n
lagrangePolynomial+n, i/ dt, i, 0, n))
4
The related approximation of the integral follows next by summing over the
corresponding function values.
n = 8;
Factor%
Fold%Plus, 0,
Simplify%Table%h f +a + h i/

0
n
lagrangePolynomial+n, i/ dt, i, 0, n))))
1
14175
4 h (5888 f (a +h) 928 f (a +2 h) +10496 f (a +3 h) 4540 f (a +4 h) +10496
f (a +5 h) 928 f (a +6 h) +5888 f (a +7 h) +989 f (a +8 h) +989 f (a))
representing Simpson's basic rule of integration. To get the higher order
approximations we define a relation in Mathematica which delivers the
quadrature formulas.
Lecture_008.nb 13
2011 G. Baumann
integrationRulesLagrange#n_, h_' :
.Fold%Plus, 0,
Table%h f#a i h'

0
n
lagrangePolynomial#n, i' t,
i, 0, n) ss Simplify) ss Factor2
The function integrationRulesLagrange allows us to generate the integration
formulas in a straight forward way. The next line sets up a table of these
formulas up to order 6.
TableForm$i, integrationRulesLagrange#i, h'
i1
6
,
TableHeadings None, n, Formula(
n Formula
1
1
2
h + f +a + h/ + f +a//
2
1
3
h +4 f +a + h/ + f +a + 2 h/ + f +a//
3
3
8
h +3 f +a + h/ + 3 f +a + 2 h/ + f +a + 3 h/ + f +a//
4
2
45
h +32 f +a + h/ + 12 f +a + 2 h/ + 32 f +a + 3 h/ + 7 f +a + 4 h/ + 7 f +a//
5
5
288
h +75 f +a + h/ + 50 f +a + 2 h/ + 50 f +a + 3 h/ + 75 f +a + 4 h/ + 19 f +a + 5
6
1
140
h +216 f +a + h/ + 27 f +a + 2 h/ + 272 f +a + 3 h/ + 27 f +a + 4 h/ + 216 f +a
41 f +a + 6 h/ + 41 f +a//
The first formula is known as Trapezoidal rule, the second as Simpson's rule,
the third is called 3 f 8 Simpson rule the forth is the rule established by Milne,
the fifth has no specific name, and the sixth is Weddle's rule to approximate
integrals. For larger orders n > 6 the function integrationRulesLagrange does
not deliver useful formulas because of the insensitivity of the integral due to
changes in the function f . In principle we can generate these formulas but they
are not drastically improving the accuracy of the integral. This behavior is
discussed in more detail in the following example.
Example 0.0. Error Estimation
14 Lecture_008.nb
2011 G. Baumann
Recall the examples we discussed in the previous section, with
I =
_
0
1 1
1 +x
dx = ln(2).
Solution 0.6. Here f (x) = 1f (1 +x), [a, b] = [0, 1], and h = (b a) f n. The Table
from above will give us the related results and errors.
f +x_/ :=
1
x + 1
TableForm%
Table%i, integrationRulesLagrange i,
1
i
,
log+2./ - integrationRulesLagrange i,
1
i
!, i, 1, 10) s.a -0.,
TableHeadings -None, "Formula", "Value", "Error")
Formula Value Error
1 0.75 0.0568528
2 0.694444 0.00129726
3 0.69375 0.000602819
4 0.693175 0.0000274226
5 0.693163 0.0000158485
6 0.693148 8.81695 10
7
7 0.693148 5.52783 10
7
8 0.693147 3.39735 10
8
9 0.693147 2.22241 10
8
10 0.693147 1.45038 10
9
It is obvious from this table that the higher order interpolation polynomials
reduce the error of approximation. However, within the range of 6 t0 10 there is
only a marginal improvement of the error.
1.2.3 Error Estimations for Trapezoidal Rule
Lecture_008.nb 15
2011 G. Baumann

Das könnte Ihnen auch gefallen