<<

10.001: Numerical Integration

R. Sureshkumar

1 Intro duction

The topic for to day's discussion is numerical integration of functions. In particular, we

would like to obtain a numerical approximation to the I f , of a suciently

smo oth, integrable f x, de ned in the closed interval a  x  b. i.e.,

Z

b

I f = f xdx: 1

a

This problem arises in many applications. In some cases, the function maybe

to o complicated to allow for an analytical evaluation of the integral. In some other cases,

the `function' itself maybeavailable at a discrete set of p oints, either from exp erimentor

from simulation.

2 Numerical Metho ds

Numerical metho ds are develop ed based on the results of mathematical analyses. For

to day's lecture, our understanding of elementary suces. The class of numerical

integration techniques we discuss to day can b e compactly expressed as:

i=n

X

I f   f x : 2

i i

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 p oint

x weighted by a co ecient . One way to determine the co ecients is to replace

i i i

the original function f x by an interpolating function which is simple enough, or in the

terminology of numerical analysts, to use a 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 interp olating 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 interp olant

function C x which is simply given by C x = f a. We now pro ceed to compute the

integral as the 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 af a; 3

The interp olation rule given by Eq.3 is known as the Rectangle Rule.

Alternatively,we can use the constant interp olant C x, given by the value of f x

a+b

at the midp oint of the interval, i.e., C x is now given by C x=f  . This results in

2

the Midpoint Rule given by

a + b

: 4 I f   b af 

2

Evidently, Eq.3 is exact for constant functions and Eq.4 is exact for constant or

linear functions of the typ e f x = x + Verify this, why so?. However, for the

of functions of higher order and complexity, Eqs.3 and 4 in general o er poor

R

1

2

approximations. For instance, x dx =1=3 when approximated with the midp oint rule

0

gives a value of 1=4. This error would amplify as the p olynomial order is increased further.

This motivates us to lo ok for higher order interp olant functions. We could use a

linear interp olant, i.e., we approximate the function f x as a straight line, say y x,

1

which passes passes through the p oints a; f a and b; f b What is the equation

representing this line?. Wenowevaluate the integral as the area enclosed by the trap ezoid

enclosed by y =0, y = y x, x = a and x = b. This results in the , given

1

by

b a

I f   [f a+f b]: 5

2

A more accurate quadrature rule can be obtained by using a quadratic interp olant

function, which passes through the p oints a; f a, b; f b and a + b=2;fa + b=2.

If wenowevaluate the integral as the area enclosed by this parab ola, the x axis, and the

lines x = a and x = b, we arrive at the Simpson's rule, given by

b a a + b

I f   [f a+4f  +f b]: 6

6 2 2

In a similar fashion, we can derive higher order quadrature rules through the use

of p olynomials of arbitrary degree as interp olants. 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 p oints 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 co ecients  in Eq.2 resulting in unacceptably high roundo errors We will

i

discuss roundo errors in $4:2. Hence, we seek an alternative approach, the one of using

comp osite formula as explained b elow.

2.2 Comp osite Rules

I will illustrate this concept using the midp oint rule de ned by Eq.4. The idea is to divide

the interval [a; b] into n subintervals, the ith subinterval b eing [x ;x ]. The length of

i1 i

the the ith subinterval, h , is then given by h = x x . Notice that x = a and x = b.

i i i i1 0 n

Now the contribution to the integral from the ith subinterval, according to the midp oint

rule is given by h f x + x =2. Hence, adding up the elemental contributions from

i i1 i

each one of the n subintervals, we arriveatthe Composite Midpoint Rule, given by

n

X

x + x

i1 i

I = h f  ; 7

CM i

2

i=1

where the subscript CM denotes Comp osite Midp oint.

A similar pro cedure using Eq.5 and 6 results in the Composite Trapezoidal and the

Composite Simpson's rules resp ectively, given by

n

X

h

i

[f x +f x ] for the comp osite trap ezoidal rule 8 I =

i1 i CT

2

i=1

and

n

X

h x + x

i i1 i

I = [f x +4f  +f x ] for the Comp osite Simpson's rule: 9

CS i1 i

6 2

i=1

3 Writing C Programs to Use the Comp osite Rules

We would now like to develop a C program which would compute the integral of a user

de ned function using the comp osite trap ezoidal rule using equally spaced p oints, i.e., 3

h = h = b a=n for each subinterval. The program I wrote for this purp ose is given

i

in /mit/10.001/Examples/ctrule.c. Let's analyze this little C program brie y. First of

all, we would need an based on the comp osite trap ezoidal rule given by Eq.8.

A simple algorithm would be to compute the n elemental contributions with h factored

out to the integral and add them up. This would result in 2n function evaluations and

2n additions. However, let's expand Eq.8 to examine the CT rule more closely. When we

expand the series, we get

h

I = [f a+2f x +2f x + +2f x +f b] : 10

CT 1 2 n1

2

The following algorithm can now be develop ed based on Eq. 10:

 Initialize Integral = f a+f b=2.

 Within a for lo op which runs from i =2 to n 1, let integral = Integral + f x .

i

 Multiply the nal answer with h.

This metho d requires only n function evaluations and n additions and hence would yield

answers in half the time as compared to the original algorithm which required 2n function

evaluations and 2n additions. This is the algorithm that has b een implemented in the C

program ctrule.c in the /mit/10.001/Examples directory.

Further of program ctrule.c shows that the function to be integrated is

de ned separately in a subprogram: double fdouble x. This allows for the modularity of

the program, i.e., if we need to use this program to integrate another function, we only

need to change the function de nition in this subprogram.

We could write similar programs to implement the comp osite midp oint and the

comp osite Simpson's rules. In fact we could incorp orate in one program, all the three

metho ds discussed ab ove so that a given user can cho ose from any one of the given

options. It is evident that this exibility can b e obtained through an if .. then construct

discussed in the previous lectures.

4 Testing the C program

Once we have written and successfully compiled the program, we need to test its p erfor-

mance. The common practice is to solve a problem whose solution is known to us using 4

the new program. For instance, in the program ctrule.c mentioned ab ove, we could put in

2

various functions, suchasf x=1;x;x ;:: etc. and check whether the program yields an

answer close to the known analytical result. I have used an exp onential function which

maybe thought of as a p olynomial of in nite order.

Well, thinking of go o d test cases for your programs is an art in itself. For instance,

R

1

2

to test the program, we could try to evaluate the integral xexpx dx using ctrule.c

1

since we know the analytical result is 0 Why?.

4.1 Testing for Convergence

The concept of convergence is of cardinal imp ortance in . This can be

explained as follows with our current example. If we use any of the equations 7 - 9 to

evaluate the integral, how fast do es the numerically computed answer, say I , converge to

h

the exact answer I ? To quantify this, we could de ne di erent measures of convergence.

One of them is the absolute error, E , de ned by E = jI I j and we will use this

A A h

measure for the purp ose of our discussion.

Let's evaluate from the program ctrule.c E for di erent values of h. If we do this

A

and plot E vs. h in a log-log plot see gure b elow, we see that the error decreases as h

A

is decreased and on a log-log plot we get a straight line of slop e 2. This error b ehavior is

typical of quadratical ly convergent . It can b e shown that b oth the CM and the

CT rules are quadratically convergent, whereas the CS rule converges as the fourth-p ower

quartically,ifyou like of the subinterval size h.

Once wehave made such observations, we should try to reconcile them with relevant

theoretical analysis. For instance, why do es the trap ezoidal rule converge as the second-

power of h? A theoretical explanation of this requires the following theorem which arises

primarily from the concept of the expansion of a smo oth function, and

we simply state the theorem without pro of and use it to see how we can explain the

convergence characteristics of the trap ezoidal rule.

Polynomial Interp olation Theorem: Let f xbe a function with n + 1 contin-

uous derivatives on an interval containing distinct p oints x < x < ::: < x . If px is

0 1 n

the unique p olynomial of degree n or less such that px =f x , then for any value of

i i

x in the interval [x ;x ], we have

0 n

x x x x :::x x 

0 1 n

n+1

f x px= f z ; 11

n + 1! 5 Covergence Comparison: CM, CT and CS Rules. 1 'CT' 'CM' 'CS'

1e-10 Absolute Error

1e-20 0.001 0.01 0.1

h

n+1

where f denotes the n + 1th derivative of f and z is some p oint in the interval

[x ;x ].

0 n

Let's apply this theorem for the trap ezoidal rule. Here, for each one of the n subin-

tervals, we use linear interp olation functions, so that the error in this approximation in

xx xx 

i1 i

2

the ith subinterval is given by f z . So the absolute value of the error in

2

the integral evaluation from this subinterval is given by verify this result

Z

x

i

x x x x 

i1 i

2

E = j f z dxj

A;i

2

x

i+1

3

x x 

i i1

2

f z  =

12

3

h

2

= f z : 12

12

Now, the if we add up the error contributions from all the n subintervals and replace

2z 

f by its maximum value in [a,b], say M , and utilize the fact that nh =b a, we

2

arrive at

M

2

2

E h  b ah ; 13

A

12

which correctly describ es the error b ehavior we saw in the computations, see Figure 6.

M

2

2

Similar analyses show that for the CM rule, E h  b ah and for the CS rule,

A

24 6

M

4

4

E h  b ah , where M is the maximum value of the fourth derivative of the

A 4

2880

integrand function. Try to reconcile the numerical results of Figure 6 with the theoretically

obtained error b ounds for the three di erent integration rules.

4.2 Roundo Error

The analysis in the previous subsection addressed the discretization error resulting from

the numerical approximation. Another typ e of error whichwehave to guard our programs

against is the Roundo Error, arising purely due to the nite precision arithmetic of the

computer. For instance, the number 0:0 can b e represented in the computer as a decimal

which has a nite number of digits after the decimal p oint as 0. In single precision,

this could be for instance 0.00000000*** where *** denotes an integer over which we

generally have little control. So if the rst non-zero digit is greater than or equal to 5, the

representation of 0 as a real numb er, correct to eight decimal digits on the computer is

8

0:00000001, or 10 . In double precision calculations, this representation is more accurate,

16 32

up to 10 , and in more accurate quadruple precision, up to 10 .

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 p erform and the rounding error incurred

in each evaluation and addition adds up to a signi cant value. For the double precision

16

calculations we employ, the rounding error in each op eration is of the order 10 . Then

8 8

if we do 10 evaluations/ additions, the total rounding error could be of the order 10 .

This reasoning is substantiated by the numerical results presented in a gure ab ove. We

7 10

can see from there that for h =10 , the error is greater than 10 . 7 Effect of Rounding Error: Composite Trapezoidal Rule 0.01

0.001

0.0001

1e-05

1e-06

1e-07

1e-08

Absolute Error 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 8