Date post: | 03-Apr-2018 |
Category: |
Documents |
Upload: | mahmoud-el-mahdy |
View: | 221 times |
Download: | 0 times |
of 15
7/29/2019 Lecture 008
1/15
Numerical Integration
1.1 Introduction
The definite integral
I+f/ a
bf+x/ x
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 off+x/; this is the Fundamental Theorem ofCalculus. 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 elementaryfunctions. Examples of such integrals are
0
1x2
x 0
x sin. x 2 x
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
7/29/2019 Lecture 008
2/15
1.2 Trapezoidal Method
The central idea behind most formulas for approximating
I+f/ a
bf+x/ x
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
p1+x/ +bx/ f+a/ +xa/ f+b/ba
which interpolates f+x/ at a and b (see Figure 0.0). The integral ofp1+x/ over#a, b' is the area of the shaded trapezoid shown in Figure 0.0; it is given by
yf#x'
p1+x/
a b
2 Lecture_008.nb
2011 G. Baumann
7/29/2019 Lecture 008
3/15
Figure 0.0. An illustration of the trapezoidal rule (0.0).
T1+f/ +ba/ f+a/ f+b/2
.
This approximates the integral I+f/ iff+x/ is almost linear on #a, b'.Example 0.0. Trapezoidal Method I
Approximate the integral
I 0
1 1
1xx
Solution 0.1. The true value of this integral is
I0 0
1 1
x 1x
log+2/
Defining the integrand as
f+x_/ :1
x 1
and applying the interpolation formula to the interval #0, 1' we get the firstapproximation
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
7/29/2019 Lecture 008
4/15
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 (0.0) when f+x/ is not a nearly linearfunction 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 benearly linear on each one. This idea is illustrated in Figure 0.0
yf+x/
p1+x/
a b
x0 x1 x2
Figure 0.0. An illustration of the trapezoidal rule T2+f/ (5.2.1).Example 0.0. Trapezoidal Method II
Approximate the integral
I 0
1 1
1xx
by using T1+f/ on two subintervals of equal length.Solution 0.2. The true value of this integral is
4 Lecture_008.nb
2011 G. Baumann
7/29/2019 Lecture 008
5/15
I0 0
1 1
x 1x
log
+2
/For two subintervals of equal length
I1 0
1
21
x 1x 1
2
1 1
x 1x
log3
2 log
4
3
Defining the integrand as
f+x_/ : 1x 1
and applying the interpolation formula to the intervals #0, 1 s 2' and #1 s 2, 1' weget the first approximation
T2 1
2
1
2 0 f+0/ f 1
2
1
21
1
2f+1/ f 1
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 forT1 compare Example 0.0.We will derive a general formula to simplify the calculations when using several
Lecture_008.nb 5
2011 G. Baumann
7/29/2019 Lecture 008
6/15
subintervals of equal length. Let the number of subintervals be denoted by n,
and let
h ba
n
be the length of each subinterval. The endpoints of the subintervals are given by
xj a j h j 0, 1, , n.
Then break the integral into n subintervals
I+f/ a
bf+x/ x
x0
xnf+x/ x
i0
n1
xi
xi1f+x/ x.
Approximate each subinterval by using a linear interpolating polynomial such as
(0.0) and noting that each subinterval #xi, xi1' has length h. Then
Tn+f/ i0
n1 h
2+f+xi/ f+xi1// h 1
2f+x0/ 1
2f+xn/
i1
n2
f+xi/ .
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 (0.0). The implementation is straight forward and does not
use special features of the evaluation.
6 Lecture_008.nb
2011 G. Baumann
7/29/2019 Lecture 008
7/15
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/ x2 x2
trapezoidalMethod-x2 x2 , x, 0, 1, 210.18932
Before giving some numerical examples ofTn+f/, we would like to discuss thechoice ofn. With a sequence of increasing values ofn, Tn+f/ will usually be anincreasingly accurate approximation ofI+f/. But which sequence of values ofnshould be used? Ifn 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 ofn will ensure that all previously computedinformation 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, considerT2+f/ and T4+f/.
Lecture_008.nb 7
2011 G. Baumann
7/29/2019 Lecture 008
8/15
T2+f/ h f+x0/2
f+x1/ f+x2/2
with h ba
2, x0 a, x1
ab
2, x2 b.
Also
T4+f/ h f+x0/2
f+x1/ f+x2/ f+x3/ f+x4/2
with h ba
4,
x0 a, x1 3 ab
4, x2
ab
2, x3
a3 b
4, x4 b.
Comparing the two approximations, we observe that f+x1/ and f+x3/ need to beevaluated, as the other function values are known from the lower
approximation. For this and other reasons, all of our examples ofTn+f/ arebased on doubling n.
Example 0.0. Higher Trapezoidal Method
Here we will calculate Tn+f/ using trapezoidal interpolation for three differentfunctions. The functions are:
f+x_/ : x2
for the interval #0, 1'.
g+x_/ : 1x2 1
for the interval #0, 4' and
h+x_/ : 1cos+x/ 2
for the interval #0, 2 '. The number of iterations aren 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
7/29/2019 Lecture 008
9/15
I1 0
1
f+x/x
1
2 erf+1/
I2 0
4
g+x/x
tan1+4/
and
I3 0
2
h+x/x
2
3
The approximation for the different functions and different iteration numbers
follow from the lines below
Tf +trapezoidalMethod+f+x/, x, 0, 1, 1/ &/ s2, 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, 2561.45882, 1.32941, 1.32525, 1.32567, 1.32578, 1.32581, 1.32582, 1.32582
Lecture_008.nb 9
2011 G. Baumann
7/29/2019 Lecture 008
10/15
Th +trapezoidalMethod+h+x/, x, 0, 2 , 1/ &/ s2, 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
1 I1 Tf
0.0154539, 0.00384004, 0.000958518, 0.000239536,0.0000598782, 0.0000149692, 3.74227 106, 9.35566 107
2 I2 Tg
0.133006, 0.0035941, 0.000564261, 0.000144082,0.000036038, 9.01059 106, 2.25272 106, 5.63183 107
3 I3 Th
0.561191, 0.0375927, 0.000192788, 5.12258 109,
8.88178 1016, 8.88178 1016, 0., 8.881781016
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
7/29/2019 Lecture 008
11/15
TableForm$Prepend$2, 4, 8, 16, 32, 64, 128, 256, 1, 2, 3,n, "1", "2", "3"((
n 1 2 32 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
64 0.0000149692 9.01059 106 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.
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
pn+x/ so that
a
bf+x/ x
a
bpn+x/ x
For the following calculation we assume an equidistant partition of the interval
#a, b' withxi a i h and i 0, 1, 2, , n
where h
+ba
/ sn and n 0 and n N0. If we assume that the interpolation
polynomial is generated by a Lagrange polynomial of ordern then we have
pn+x/ i0
n
fi Li+x/ with Li+x/ kik0
n xxk
xixk
and fi f+xi/ fori 0, 1, 2, , n. Introducing new variables with x ah t we
Lecture_008.nb 11
2011 G. Baumann
7/29/2019 Lecture 008
12/15
can rewrite the Lagrange polynomial in a simpler form
Li+x/ i+t/ kik0
n t k
i k.
This representation can be used in the integration of the polynomial pn+x/ as
a
bpn+x/ x
i0
n
fia
bLi+x/ x h
i0
n
fi0
ni+t/ t h
i0
n
fii.
The weights i are functions ofn only and do not depend on the function values
off.
Defining the Lagrange polynomials in their reduced representation is straightforward in Mathematica and allows us to use them in the calculation of the
weights. The reduced Lagrange polynomials are defined by
lagrangePolynomial+n_, i_/ : k0
n
If%i k, t ki 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 i. The
following calculation lists the weights forn 2 corresponding to the 1 s 3Simpson rule
n 2; Table%0
n
lagrangePolynomial+n, i/t, i, 0, n)
13
,4
3,
1
3!
12 Lecture_008.nb
2011 G. Baumann
7/29/2019 Lecture 008
13/15
The weights in general satisfy the property
i0
n
i n
We can check this relation with our function.
n 4; Fold%Plus, 0, Table%0
n
lagrangePolynomial+n, i/t, 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/t, i, 0, n))))
114175
4 h +5888 f+ah/ 928 f+a 2 h/ 10496 f+a 3 h/ 4540 f+a 4 h/ 10496f+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
7/29/2019 Lecture 008
14/15
integrationRulesLagrange#n_, h_' :.Fold%Plus, 0,
Table%h f#a i h' 0n
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'i16 ,TableHeadings None, n, Formula(
n Formula
11
2h +f+a h/ f+a//
21
3h +4 f+a h/ f+a 2 h/ f+a//
33
8h +3 f+a h/ 3 f+a 2 h/ f+a 3 h/ f+a//
42
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 5288
h +75 f+a h/ 50 f+a 2 h/ 50 f+a 3 h/ 75 f+a 4 h/ 19 f+a 6
1
140h +216 f+a h/ 27 f+a 2 h/ 272 f+a 3 h/ 27 f+a 4 h/ 216 f+
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 s 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
7/29/2019 Lecture 008
15/15
Recall the examples we discussed in the previous section, with
I 0
1 1
1xx ln+2/.
Solution 0.6. Here f+x/ 1 s +1x/, #a, b' #0, 1', and h +ba/ s n. The Tablefrom above will give us the related results and errors.
f+x_/ : 1x 1
TableForm%
Table%i, integrationRulesLagrange i,1
i,
log+2./ integrationRulesLagrange i, 1i
!, i, 1, 10) s.a 0.,
TableHeadings None, "Formula", "Value", "Error")
Formula Value Error
1 0.75 0.0568528
2 0.694444 0.001297263 0.69375 0.000602819
4 0.693175 0.0000274226
5 0.693163 0.0000158485
6 0.693148 8.81695 107
7 0.693148 5.52783 107
8 0.693147 3.39735 108
9 0.693147 2.22241 108
10 0.693147 1.45038 109
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