Lect 4 PDF
Lect 4 PDF
October 9, 2012
1 Introduction
The magnetic field induced by a current flowing in a circular loop of wire has intensity
Z /2 x 2 1/2
4Ir 2
H(x) = 2 1 sin d
r x2 0 r
where I is the current, r is the radius of the loop, and x is the distance from the center
of the loop. The integral is not expressible in terms of familiar functions, but it can be
evaluated numerically.
In this chapter, we discuss the subject of numerical integration, also called quadrature.
The numerical methods that are covered here are based, one way or another, on adding up
the value of the integrand at a sequence of points along the x-axis. The idea is to obtain
the integral as accurately as possible with the smallest number of function evaluations.
The Richardson extrapolation formalism (discussed in the previous chapter) is used to
construct a general method known as the Romberg algorithm. Multidimensional integra-
tion is touched only briefly here. We return to the subject later in connection with Monte
Carlo methods.
with partition points xi (0 i n) that divide the interval [a, b] into n subintervals [xi , xi+1 ].
1
Denote by mi the greatest lower bound (infimum) of f (x) on [xi , xi+1 ]:
Likewise, denote by Mi the least upper bound (supremum) of f (x) on [xi , xi+1 ]:
The lower sums and upper sums of f corresponding to a given partition P are defined to
be
n1
L( f ; P) = mi(xi+1 xi)
i=0
n1
U( f ; P) = Mi(xi+1 xi)
i=0
If f is positive, then these two quantities can be interpreted as estimates of the area under
the curve for f .
a=x0 x1 x2 x3 x4 x5 x6 x7 x8=b
a=x0 x1 x2 x3 x4 x5 x6 x7 x8=b
2
1.2 Riemann-integrable functions
Consider the least upper bound obtained when P is allowed to range over all partitions of
the interval [a, b]. This is abbreviated supP L( f ; P). Similarly, the greatest lower bound,
when P ranges over all partitions P on [a, b], is abbreviated infP L( f ; P).
sup L( f ; P) = inf L( f ; P)
P P
Theorem. Every continuous function defined on a closed interval of the real line is Rie-
mann integrable.
The Riemann integral of a continuous function on [a, b] can be obtained by two limits:
Z b
lim sup L( f ; Pn ) = f (x)dx = lim inf L( f ; Pn )
n P a n P
Direct translation of this definition into a numerical algorithm is possible, but the resulting
algorithm converges very slowly. The situation can be improved dramatically by using
more sophisticated methods such as the Rombergs algorithm.
3
2 Trapezoid rule
The trapezoid method is based on an estimation of the area under a curve using trape-
zoids. First the interval [a, b] is divided into subintervals according to the partition P =
{a = x0 < x1 < . . . < xn1 < xn = b}.
A typical trapezoid has the subinterval [xi , xi+1 ] as its base, and the two vertical sides are
f (xi ) and f (xi+1 ). The area is equal to the base times the average height. Thus we obtain
the basic trapezoid rule:
Z xi+1
1
f (x)dx Ai = (xi+1 xi )[ f (xi ) + f (xi+1 )]
xi 2
The total area under all of the trapezoids is given by the composite trapezoid rule:
n1
1 n1
Z b
f (x)dx T ( f ; P) = Ai = (xi+1 xi)[ f (xi) + f (xi+1)]
a i=0 2 i=0
f(xi)
f(xi+1)
xi xi+1
4
Implementation
The following procedure Trapezoid uses the trapezoid method with uniform spacing to
estimate the definite integral of a function f on a closed interval [a, b].
h = (b-a)/(double)n;
result = 0.5*(f(a)+f(b));
return(h*result);
}
Output
2
For f = ex on [0,1], we obtain the result 0.7468071 with n = 60 and 0.7468238 with
n = 500. The correct answer is 0.7468241 with seven decimals.
1 f(x) = 1.0/exp(x*x)
0.9
0.8
0.7
f(x)
0.6
0.5
0.4
0.3
0 0.2 0.4 0.6 0.8 1
x
2
Figure 4: Plot of the function f = ex .
5
2.2 Error analysis
Theorem on precision of trapezoid rule.
If f 00 exists and is continuous on the interval [a, b], and if the composite trapezoid rule T
with uniform spacing h is used to estimate the integral I = ab f (x)dx, then for some in
R
(a, b),
1
I T = (b a)h2 f 00 () = O (h2 )
12
To prove this use the error formula for polynomial interpolation with n = 1,R x0 = 0, and
1
x1 = 1: f (x) p(x) = (n+1)! f (n+1) () ni=0 (xxi ) plus mean-value theorem ab f (x)g(x)dx =
Rb
f () a g(x)dx. Example. If the trapezoid rule is used to compute
Z 1
2
I= ex dx
0
1
|I T | h2
6
To have an error of at most 12 104 , we require that h = (b a)/n 0.01732 or n 58.
Using the procedure Trapezoid with n = 58, we obtain 0.7468059 which is incorrect in
the fifth digit.
6
2.3 Recursive trapezoid formula
We now introduce a formula for the composite trapezoid rule when the interval [a, b] is
subdivided into 2n equal parts. We have
n1
h
T ( f ; P) = h f (xi ) + [ f (x0 ) + f (xn )]
i=1 2
n1
h
= h f (a + ih) + [ f (a) + f (b)]
i=1 2
Here we have introduced the notation R(n, 0) which is used in the next section in connec-
tion with the Romberg algorithm. The notation R(n, 0) denotes the result of applying the
composite trapezoid rule with 2n equal subintervals.
SUBINTERVALS ARRAY
20 R(0,0)
21 R(1,0)
22 R(2,0)
23 R(3,0)
a b
For the Romberg algorithm, we need a method for computing R(n, 0) from R(n 1, 0)
without unnecessary evaluations of f . We use the identity
1 1
R(n, 0) = R(n 1, 0) + [R(n, 0) R(n 1, 0)]
2 2
where
h
C = [ f (a) + f (b)]
2
Notice that the size of the subintervals for R(n 1, 0) are twice the size of those for
R(n, 0).
7
By subtraction we get
2 1 n 2 1 n1
1
R(n, 0) R(n 1, 0) = h f (a + ih) h f (a + 2 jh)
2 i=1 j=1
2n1
=h f [a + (2k 1)h]
k=1
Here we have taken into account that each term in the first sum that corresponds to an even
value of i is canceled by a term in the second sum. This leaves only terms that correspond
to odd values of i.
2 n1
1
R(n, 0) = R(n 1, 0) + h f [a + (2k 1)h] (n 1)
2 k=1
Implementation
The following procedure RecTrapezoid uses the recursive trapezoid formula to calculate
a sequence of approximations to the definite integral of function f on the interval [a, b].
h = b-a;
/* Value of R(0,0) */
R[0] = (h/2.0)*(f(a)+f(b));
8
Example.
The procedure RecTrapezoid was used to calculate the value of by evaluating the integral
Z 1
4
dx
0 1 + x2
R(0,0) = 3.000000000000
R(1,0) = 3.100000000000
R(2,0) = 3.131176470588
R(3,0) = 3.138988494491
R(4,0) = 3.140941612041
R(5,0) = 3.141429893175
R(6,0) = 3.141551963486
R(7,0) = 3.141582481064
R(8,0) = 3.141590110458
R(9,0) = 3.141592017807
The approximation is correct in the sixth decimal. The correct value is 3.141592654 with
nine decimals.
Next, we introduce the Romberg algorithm which uses the numbers R(n, 0) and improves
the estimates in a fashion similar to that of Richardson extrapolation.
9
3 Romberg algorithm
3.1 Description
The Romberg algorithm produces a triangular
Rb
array of numbers, all of which are numeri-
cal estimates of the definite integral a f (x)dx. The array is denoted by
R(0, 0)
R(1, 0) R(1, 1)
R(2, 0) R(2, 1) R(2, 2)
R(3, 0) R(3, 1) R(3, 2) R(3, 3)
.. .. .. ..
. . . .
R(n, 0) R(n, 1) R(n, 2) R(n, 3) . . . R(n, n)
The first column contains estimates R(k, 0) obtained by applying the trapezoid rule with
2k equal subintervals. The first one is obtained using the formula
1
R(0, 0) = (b a)[ f (a) + f (b)]
2
R(n, 0) is obtained easily from R(n 1, 0) using the formula developed in the previous
section:
2n1
1
R(n, 0) = R(n 1, 0) + h f [a + (2k 1)h]
2 k=1
where h = (b a)/2n and n 1.
The second and successive columns are generated by the following extrapolation formula:
1
R(n, m) = R(n, m 1) + [R(n, m 1) R(n 1, m 1)]
4m 1
with n 1 and m 1. This formula results from the application of the Richardson ex-
trapolation theorem.
10
3.2 Derivation of the Romberg algorithm
To briefly sketch where the iterative equation behind Romberg algorithm comes from we
start from the following formula where the error is expressed in the trapezoid rule with
2n1 equal subintervals
Z b
f (x)dx = R(n 1, 0) + a2 h2 + a4 h4 + a6 h6 + . . .
a
This is one form of Euler-Maclaurin formula (see the next page). (A proof can be found
for instance in Gregory, R.T, and Kearney, D., A Collection of Matrices for Testing Com-
putational Algorithms (Wiley, 1969).) Here h = (b a)/2n and the coefficients ai depend
on f but not on h. R(n 1, 0) is one of the trapezoidal elements in the Romberg array.
For our purposes, it is not necessary to know the definite expressions for the coefficients.
For the theory to work smoothly, we assume that f possesses derivatives of all orders on
the interval [a, b].
Now recall the theory of Richardson extrapolation. We can use the same procedure here
due to the form of the equation above. Replacing n with n + 1 and h with h/2, we have
Z b
1 1 1
f (x)dx = R(n, 0) + a2 h2 + a4 h4 + a6 h6 + . . .
a 4 16 64
Subtracting the first equation from the second equation multiplied by 4 gives
Z b
1 5
f (x)dx = R(n, 1) a4 h4 a6 h6 . . .
a 4 16
where
1
R(n, 1) = R(n, 0) + [R(n, 0) R(n 1, 0)] (n 1)
3
Note that this is the first case (m = 1) of the extrapolation formula that is used to generate
the Romberg array.
The term R(n, 1) should be considerably more accurate than R(n, 0) or R(n 1, 0) since
the error series is now O (h4 ). This process can be repeated to further eliminate higher
terms in the error series.
The only assumption made is that the first equation with the error series is valid for the
function f . In practice, we use only a modest number of rows in the Romberg algorithm,
which means that the assumption is likely to be valid. The situation is governed by a
theorem called the Euler-Maclaurin formula.
11
3.3 Euler-Maclaurin formula and error term
Theorem.
If f (2m) exists and is continuous on the interval [a, b], then
h n1
Z b
f (x)dx = [ f (xi) + f (xi+1)] + E
a 2 i=0
In this theorem, the Ak s are constants and they can be defined by the equation (see
e.g. D.M. Young and R.T. Gregory, A survey of Numerical Mathematics, Vol.1, p. 374,
(Dover) for details):
x
= Ak x k
ex 1 k=0
The points to notice are that the right-hand side of the Euler-Maclaurin formula contains
the trapezoid rule and an error term E. Furthermore, the error term can be expressed as a
finite sum in ascending powers of h2 .
12
3.4 Implementation
The following procedure Romberg Rb
uses the extrapolation formula to calculate numerical
estimates of the definite integral a f (x)dx. The input is the function f , the endpoints of
the interval [a, b], the number of iterations n and the Romberg array (R)0:n0:n .
h = b-a;
R[0][0] = (h/2.0)*(f(a)+f(b));
/* Successive R(i,j) */
for(j=1; j<=i; j++)
R[i][j] = R[i][j-1]
+(R[i][j-1]-R[i-1][j-1])/(pow(4.0,(double)j)-1.0);
}
}
Here the elements of the array R(i, j) are computed row by row up to the specified number
of rows, n (this number need not to be very large because the method converges very
quickly, e.g., n = 4 . . . 5 is often suitable).
13
Output
Using same example as before, we calculate the value of by evaluating the integral
Z 1
4
dx
0 1 + x2
(Correct value is 3.141592654.)
3.0000000000
3.1000000000 3.1333333333
3.1311764706 3.1415686275 3.1421176471
3.1389884945 3.1415925025 3.1415940941 3.1415857838
3.1409416120 3.1415926512 3.1415926611 3.1415926384 3.1415926653
3.1414298932 3.1415926536 3.1415926537 3.1415926536 3.1415926536 3.1415926536
3.0000000000
3.0999999046 3.1333332062
3.1311764717 3.1415686607 3.1421177387
3.1389884949 3.1415925026 3.1415941715 3.1415858269
3.1409416199 3.1415927410 3.1415927410 3.1415927410 3.1415927410
3.1414299011 3.1415927410 3.1415927410 3.1415927410 3.1415927410 3.1415927410
We notice that the algorithm converges very quickly (with double precision, n = 5 is
enough to obtain nine decimals of precision). With single precision, the accuracy of the
calculation is limited to six decimals.
14
4 Adaptive Simpsons scheme
We now proceed to discussing a method known as the Simpsons rule and Rb
develop an
adaptive scheme for obtaining a numerical approximation for the integral a f (x)dx. In
the adaptive algorithm, the partitioning of the interval [a, b] is not selected beforehand but
automatically determined.
Proof. Approximating the function with a polynomial of degree 2 we can write ab f (x)dx
R
A f (a) + B f ( a+b
2 ) + C f (b), where f (x) is assumed continuous on the interval [a, b]. The
coefficients A, B and C are chosen such that the approximation will give correct R1
values
for the integral when f is aquadratic polynomial. Let a = 1 and b = 1: 1 f (x)dx
A f (1) + B f (0) +C f (1). The following equations must hold:
R1
f (x) = 1 : 1 dx = 2 = A + B +C
R1
f (x) = x : 1 xdx = 0 = A +C
R1 2
f (x) = x2 : 1 x dx = 2/3 = A +C
R1
A = 1/3, B = 4/3, and C = 1/3. 1 f (x)dx 13 [ f (1) + 4 f (0) + f (1)]. Using
the linear mapping y = (b a)/2 + (a + b)/2 from [1, 1] to [a, b] we obtain the basic
Simpsons rule: ab f (x)dx 16 (b a)[ f (a) + 4 f ( a+b
R
2 ) + f (b)].
From these we obtain a series representation for the right-hand side of the Simpsons rule:
h 4 2
[ f (a) + 4 f (a + h) + f (a + 2h)] = 2h f + 2h2 f 0 + h3 f 00 + h4 f 000 + . . . ()
3 3 3
with Z x
F(x) = f (t)dt
a
Thus, F 0 = f , F 00 = f 0 , F 000 = f 00 and so on.
15
Now use the Taylor series for F(a + 2h):
4
F(a + 2h) = F(a) + 2hF 0 (a) + 2h2 F 00 (a) + h3 F 000 (a) + . . .
3
4
= F(a) + 2h f (a) + 2h2 f 0 (a) + h3 f 00 (a) + . . . ()
3
R a+2h
F(a) = 0, so F(a + 2h) = a f (x)dx.
Subtracting (**) from (*) gives us an estimate of the error:
h5
Z a+2h
h
f (x)dx [ f (a) + 4 f (a + h) + f (a + 2h)] = f (4) . . .
a 3 90
This is due to the fact that all lower order terms cancel out.
We now develop the test for deciding whether subintervals should continue to be divided.
The Simpsons rule over the interval [a, b] can be written as
Z b
I f (x)dx = S(a, b) + E(a, b)
a
16
Two applications of the Simpsons rule give
I = S(2) + E (2)
where
S(2) = S(a, c) + S(c, b)
and 5 5
(2) 1 h/2 (4) 1 h/2 1 (1)
E = f f (4) = E
90 2 90 2 16
Here c = (a + b)/2.
We can now subtract the first evaluation from the second one:
This can be used to evaluate the quality of our approximation of I. We can use the follow-
ing inequality to test whether to continue the splitting process:
1 (2)
|S S(1) | <
15
If the test is not satisfied, the interval [a, b] is split into two subintervals [a, c] and [c, b].
On each of these subintervals we perform the test again with replaced by /2 so that the
resulting tolerance will be over the entire interval [a, b].
17
4.3 Algorithm
The adaptive Simpsons algorithm is programmed using a recursive procedure.
We define two variables one_simpson and two_simpson that are used to calculate
two Simpson approximations: one with two subintervals another with four half-width
subintervals.
one_simpson is given by the basic Simpsons rule:
h
S(1) = S(a, b) = [ f (a) + 4 f (c) + f (b)]
6
a c b
a d c e b
h/2 h/2
18
Implementation
The followingR C implementation of the recursive procedure Simpson calculates the def-
inite integral ab f (x)dx for the function f specified by an external function f (given as
input).
h = b-a;
c = 0.5*(a+b);
one_simpson = h*(f(a)+4.0*f(c)+f(b))/6.0;
d = 0.5*(a+c);
e = 0.5*(c+b);
two_simpson = h*(f(a)+4.0*f(d)+2.0*f(c)+4.0*f(e)+f(b))/12.0;
/* Check for level */
if(level+1 >= level_max) {
result = two_simpson;
printf("Maximum level reached\n");
}
else{
/* Check for desired accuracy */
if(fabs(two_simpson-one_simpson) < 15.0*eps)
result = two_simpson + (two_simpson-one_simpson)/15.0;
/* Divide further */
else {
left_simpson = Simpson(a,c,eps/2.0,level+1,level_max);
right_simpson = Simpson(c,b,eps/2.0,level+1,level_max);
result = left_simpson + right_simpson;
}
}
return(result);
}
19
Application example
As an example, the program is used to calculate the integral
Z 2
cos(2x)
dx
0 ex
The desired accuracy is set to = 21 104 . An external function procedure is written for
f and its name is given as the first argument to Simpson.
Running the program with double precision floating-point numbers, we obtain the result
0.1996271. It is correct within the tolerance we set beforehand.
We can use Matlab or Maple to determine the correct answer. The following Matlab
commands
syms t
res = int(cos(2*t)./exp(t),0,2*pi)
eval(res)
first return the exact value 15 (1 e2 ) and then evaluate the integral giving (with ten
digits) 0.19962 65115.
20
5 Gaussian quadrature formulas
We have already seen that the various numerical integration formulas all have the common
feature that the integral is approximated by the sum of its functional values at a set of
points, multiplied by certain aptly chosen weighting coefficients. We have seen that by
a better selection of the weights, we can gain integration formulas of higher and higher
order. The idea of Gaussian quadrature formulas is to give ourselves the freedom to
choose not only the weighing factors but also the points where the function values are
calculated. They will no longer be equally spaced.
Now the catch: high order is not the same as high accuracy! High order translates to high
accuracy only when the integrand is very smooth, in the sense of being well-approximated
by a polynomial.
5.1 Description
Most numerical integration formulas conform to the following pattern:
Z b
f (x)dx A0 f (x0 ) + A1 f (x1 ) + . . . + An f (xn )
a
To use such a formula, it is only necessary to know the nodes x0 , x1 , . . . , xn and the weights
A0 , A1 , . . . , An .
The theory of polynomial interpolation has greatly influenced the development of integra-
tion formulas. If the nodes have been fixed, there is a corresponding Lagrange interpola-
tion formula:
n n xx
j
pn (x) = li (x) f (xi ) where li (x) =
i=0 j=0 xi x j
j6=i
where Z b
Ai = li (x)dx
a
21
Example.
Determine the quadrature formula when the interval is [2, 2] and the nodes are -1, 0 and
1.
We get l0 (x) = 21 x(x 1), l1 (x) = (x + 1)(x 1) and l2 (x) = 12 x(x + 1).
22
5.2 Gaussian nodes and weights
The mathematician Karl Friedrich Gauss (1777-1855) discovered that by a special place-
ment of the nodes the accuracy of the numerical integration process could be greatly
increased.
with these xi s as nodes will be exact for all polynomials of degree at most 2n + 1. Fur-
thermore, the nodes lie in the open interval (a, b).
To summarize: With arbitrary nodes, the equation (*) is exact for all polynomials of de-
gree n. With the Gaussian nodes, the equation (*) is exact for all polynomials of degree
2n + 1.
The quadrature formulas that arise as applications of this theorem are called Gaussian or
Gauss-Legendre quadrature formulas.
23
Example
As an example, we derive a Gaussian quadrature formula that is not too complicated.
We
R1
determine the formula with three Gaussian nodes and three weights for the integral
1 f (x)dx.
We must find the polynomial q and compute its roots. The degree of q is 3, so it has the
form
q(x) = c0 + c1 x + c2 x2 + c3 x3
q(x) = 5x3 3x
p
The roots are 0 and 3/5. These are the desired Gaussian nodes for the quadrature
formula.
To obtain the weights A0 , A1 and A2 , we use a procedure known as the method of unde-
termined coefficients. Consider the formula
Z 1 r ! r !
3 3
f (x)dx A0 f + A1 f (0) + A2 f
1 5 5
We want to select the coefficients Ai in such a way that the approximate equality () is an
exact equality (=) whenever f is of the form ax2 + bx + c.
24
Since integration is a linear process, the above formula will be exact for all polynomials
of degree 2 if the formula is exact for 1, x and x2 . In tabular form
Thus we get
A0 + A1
+A2 = 2
A0 A2 = 0
A0 +A2 = 10/9
can be used over an arbitrary interval [a, b]. The substitution gives
Z b Z 1
1 1 1
f (x)dx = (b a) f (b a)t + (b + a) dt
a 2 1 2 2
25
We have
Z 1 Z 1
1 1 1
f (x)dx = f t+ dt
0 2 1 2 2
" r ! r !#
1 5 1 1 3 8 1 5 1 1 3
f + f + f +
2 9 2 2 5 9 2 9 2 2 5
2
Letting f (x) = ex , we get
Z 1
2
ex dx 0.746814584
0
The true solution is 21 erf(1) 0.7468241330. Thus the error in the approximation given
by the quadrature formula is of the order 105 . This is excellent, considering that only
three function evaluations were made!
p the case for n = 2 was discussed in the previous example; i.e. for xi =
Aspan example,
3/5, 0, 3/5 and Ai = 5/9, 8/9, 5/9.
26
5.4 Multidimensional integration
In general, multidimensional integration is more difficult and computationally much more
time consuming than evaluating one-dimensional integrals. The subject is discussed only
briefly here. We return to the subject later on in connection with Monte Carlo methods.
If the area of integration is relatively simple and the integrand is smooth, we can use a
sequence of one-dimensional integrals:
ZZZ Z x2 Z y2 (x) Z z2 (x,y)
I= f (x, y, z)dxdydz = dx dy dz f (x, y, z)
x1 y1 (x) z1 (x,y)
For simplicity, we illustrate with the trapezoid rule for the interval [0,1], using n + 1
equally spaced points. The step size is therefore h = 1/n. The composite trapezoid rule is
Z 1 n1
1 i
f (x)dx [ f (0) + 2 f ( ) + f (1)]
0 2h i=1 n
The error is O (h2 ) = O (n2 ) for functions having a continuous second derivative.
If we now want to evaluate a two-dimensional integral over the unit square, we can
apply the trapezoid rule twice:
Z 1Z 1 Z 1 n
i
f (x, y)dxdy Ai f ( n , y)dy
0 0 0 i=0
n Z 1
i
= Ai f ( , y)dy
i=0 0 n
n n
i j
Ai A j f ( , )
i=0 j=0 n n
n n
i j
= Ai A j f ( n , n )
i=0 j=0
The error is again O (h2 ) because each of the two applications of the trapezoid rule entails
this error.
27
In the same way, we can integrate a function of k variables. The trapezoid rule in the
general case for integration over the multidimensional unit hypercube is given by
Z n n
1 k
f (x)dx ... A1 . . . Ak f ( ,..., )
[0,1]k 1 =0 k =0 n n
Now consider the computational effort that is required for evaluating the integrals using
the trapezoid rule. In the one-dimensional case, the work involved is O (n). In the two-
variable case, it is O (n2 ), and O (nk ) for k variables.
Thus for a constant number of nodes (constant effort) the quality of the numerical ap-
proximation to the value of the integrals declines very quickly as the number of variables
increases. This explains why the Monte Carlo methods for numerical integration become
an attractive option for high-dimensional integration.
Example.
2
In order to evaluate the integral of f (x, y) = xyex y over the square x, y [0, 1], we can
modify the procedure Trapezoid to obtain a two-dimensional version:
double Trapezoid_2D(double (*f)(double x,double y),
double ax, double bx,
double ay, double by, int n)
{
int i, j;
double hx, hy, result;
hx = (bx-ax)/(double)n;
hy = (by-ay)/(double)n;
result = 0.5*(f(ax,ay)+f(bx,by));
return(result);
}
Using the procedure Trapezoid_2D with n = 100 (overall 104 iterations), we get the fol-
lowing estimate of the integral
I 0.183649773143
28