Numerical Integration
Numerical Integration
1 Introduction
The topic for today's discussion is numerical integration of functions. In particular, we
would like to obtain a numerical approximation to the integral I (f ), of a suciently
smooth, integrable function f (x), de ned in the closed interval a x b. i.e.,
Z b
I (f ) = f (x)dx: (1)
a
This problem arises in many engineering applications. In some cases, the function may be
too complicated to allow for an analytical evaluation of the integral. In some other cases,
the `function' itself may be available at a discrete set of points, either from experiment or
from simulation.
2 Numerical Methods
Numerical methods are developed based on the results of mathematical analyses. For
today's lecture, our understanding of elementary calculus suces. The class of numerical
integration techniques we discuss today can be compactly expressed as:
iX
=n
I (f ) i f (xi ): (2)
i=0
i.e., we express the numerical approximation to the integral I (f ) as a sum of a series of
nite number of terms, each term containing the value of the function at a given point
xi weighted by a coecient i . One way to determine the coecients i is to replace
the original function f (x) by an interpolating function which is simple enough, or in the
terminology of numerical analysts, to use a quadrature technique.
Let us explore this idea a little further:
1
2.1 Simple Quadrature Rules
Consider the function f (x) whose integral we wish to approximate over the interval a
x b. The simplest of the interpolating functions we can think of is a constant function,
i.e., we assume that for a x b, the function f (x) is approximated by the interpolant
function C (x) which is simply given by C (x) = f (a). We now proceed to compute the
integral as the area enclosed by the rectangle formed by y = 0, y = f (a), x = a, and
x = b in the x , y Cartesian plane. This gives
I (f ) (b , a)f (a); (3)
The interpolation rule given by Eq.3 is known as the Rectangle Rule.
Alternatively, we can use the constant interpolant C (x), given by the value of f (x)
at the midpoint of the interval, i.e., C (x) is now given by C (x) = f ( a b ). This results in
+
2
the Midpoint Rule given by
a+b
I (f ) (b , a)f (
2 ): (4)
Evidently, Eq.3 is exact for constant functions and Eq.4 is exact for constant or
linear functions of the type f (x) = x + (Verify this, why so?). However, for the
integrals of functions of higherR order and complexity, Eqs.3 and 4 in general o er poor
approximations. For instance, x dx = 1=3 when approximated with the midpoint rule
1
0
2
gives a value of 1=4. This error would amplify as the polynomial order is increased further.
This motivates us to look for higher order interpolant functions. We could use a
linear interpolant, i.e., we approximate the function f (x) as a straight line, say y (x),1
which passes passes through the points (a; f (a)) and (b; f (b)) (What is the equation
representing this line?). We now evaluate the integral as the area enclosed by the trapezoid
enclosed by y = 0, y = y (x), x = a and x = b. This results in the Trapezoidal Rule, given
1
by
b,a
I (f )
2 [f (a) + f (b)]: (5)
2
In a similar fashion, we can derive higher order quadrature rules through the use
of polynomials of arbitrary degree as interpolants. The general class of quadrature rules
which result from such exercises are called Newton-Cotes rules. However, this approach
has many drawbacks from mathematical as well as computational points of view. Math-
ematically, the convergence of the right hand side of Eq.2 is not guaranteed as n ! 1
even for in nitely di erentiable functions. Computationally, for n 8, we run into neg-
ative coecients ( i in Eq.2) resulting in unacceptably high roundo errors (We will
discuss roundo errors in $4:2). Hence, we seek an alternative approach, the one of using
composite formula as explained below.
i =1
i=1 6 1
answer close to the known analytical result. I have used an exponential function which
may be thought of as a polynomial of in nite order.
Well, thinking of good test cases for your programs is Ran art in itself. For instance,
to test the program, we could try to evaluate the integral , xexp(x )dx using ctrule.c
1
1
2
f (x) , p(x) =
(x , x )(x , x ):::(x , xn ) f n (z);
0 1 ( +1)
(11)
(n + 1)!
5
Covergence Comparison: CM, CT and CS Rules.
1
’CT’
’CM’
’CS’
Absolute Error
1e-10
1e-20
0.001 0.01 0.1
h
where f n ( +1)
denotes the (n + 1)th derivative of f and z is some point in the interval
[x ; xn]. 0
Let's apply this theorem for the trapezoidal rule. Here, for each one of the n subin-
tervals, we use linear interpolation functions, so that the error in this approximation in
the ith subinterval is given by x,x ,1 x,x f (z). So the absolute value of the error in
( i
2
)( i) (2)
the integral evaluation from this subinterval is given by (verify this result)
EA;i = j
Z x
(x , xi, )(x , xi ) f (z)dxj
i
1 (2)
x +1 2 i
h 3
= 12 f (z ): (12) (2)
Now, the if we add up the error contributions from all the n subintervals and replace
f z by its maximum value in [a,b], say M , and utilize the fact that nh = (b , a), we
(2( ))
2
arrive at
M
EA (h) (b , a)h ; 2
(13)
2
12
which correctly describes the error behavior we saw in the computations, see Figure 6.
Similar analyses show that for the CM rule, EA(h) M2 (b , a)h and for the CS rule, 24
2
6
EA (h) 2880
M4 (b , a)h4 , where M is the maximum value of the fourth derivative of the
4
integrand function. Try to reconcile the numerical results of Figure 6 with the theoretically
obtained error bounds for the three di erent integration rules.
We see from Eq.13 that the absolute error arising from the numerical discretization
should go to 0 as h ! 0. However, the smaller h is, the larger the number of additions
and function evaluations the computer has to perform and the rounding error incurred
in each evaluation and addition adds up to a signi cant value. For the double precision
calculations we employ, the rounding error in each operation is of the order 10, . Then
16
if we do 10 evaluations/ additions, the total rounding error could be of the order 10, .
8 8
7
Effect of Rounding Error: Composite Trapezoidal Rule
0.01
0.001
0.0001
1e-05
1e-06
Absolute Error
1e-07
1e-08
1e-09
1e-10
1e-11
1e-12
1e-13
1e-07 1e-06 1e-05 0.0001 0.001 0.01 0.1 1
h