Chapter 4 Numerical Differentiation and Integration 4.3 Elements of Numerical Integration

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 9

Chapter 4 Numerical Differentiation and Integration

4.3 Elements of Numerical Integration


The need often arises for evaluating the definite integral of a function that has no
explicit antiderivative or whose antiderivative is not easy to obtain. The basic method
b

involved in approximating

f x dx

is called numerical quadrature. It uses a sum

a f x
i 0

to approximate

f x dx .
a

The methods of quadrature in this section are based on the interpolation polynomials
given in Chapter 3. We first select a set of distinct nodes

x0 , x1 ,L

, xn from the

interval a, b . Then we integrate the Lagrange interpolating polynomial


n

Pn x f xi Li x
i 0

and its truncation error term over a, b to obtain


b

a i 0

f ( n 1) x

f x dx f x L x dx x x n 1 !
i

dx

a i 0

ai f xi
i 0

n
1
x xi f ( n1) x dx

n 1 ! a i 0

where x is in a, b for each x and


b

ai Li x dx , for each i 0,1,L , n .


a

The quadrature formula is, therefore,


b

f x dx ai f xi ,
i 0

with error given by


b

n
1
E f
x xi f ( n1) x dx .

n 1 ! a i 0

Before discussing the general situation of quadrature formulas, let us consider


formulas produced by using first and second Lagrange polynomials with equally
spaced nodes. This gives the Trapezoidal rule and Simpsons rule, which are
commonly introduced in calculus courses.

To derive the Trapezoidal rule for approximating

f x dx ,

let x0 a, x1 b ,

h b a and use the linear Lagrange polynomial:


P1 x

x x1 f x x x0 f x
0
1 .
x0 x1
x1 x0

Then,

x x1 f x x x0 f x
f
x
dx

a
x x1 0 x1 x0 1
x 0

x1

11
dx f x x x0 x x1 dx
2 x0

Since x x0 x x1 does not change sign on x0 , x1 , the Weighted Mean Value


Theorem for Integrals can be applied to the error term to give, for some in x0 , x1 ,
x1

f x x x0 x x1 dx f

x0

x1

x x x x dx
0

x0

x 3 x1 x0

x0 x1 x
2
3

x1

x0

h3
f .
6

Thus, we have
b

f x dx

x x1 f x x x0 f x
0
1
2 x0 x1
2 x1 x0

x1 x0
2

x1

x0

h3
f
12

h
f x0 f x1 12 f .

Since h x1 x0 , we have the following rule:


Trapezoidal Rule:
b

f x dx

h
h3
f x0 f x1
f .
2
12

This is called the Trapezoidral rule because when f is a function with positive
b

values,

f x dx

is approximated by the area in a trapezoid, as shown in Figure 4.3.

Since the error term for the Trapezoidral rule involves f , the rule gives the exact
result when applied to any function whose second derivative is identically zero, that
is, any polynomial of degree one or less.

Simpsons rule results from integrating over a, b the second Lagrange polynomial
with nodes x0 a, x2 b , and x1 a h , where h b a 2 . (See Figure 4.4).
Therefore,
b

x x1 x x2 f x x x0 x x2 f x
0
1
x x1 x0 x2
x1 x0 x1 x2
x 0
x

x x0 x x1

x x0 x x1 x x3

f x2 dx
6
x2 x0 x2 x1
x

x1

f x dx
0

f (3) x dx.

Similarly, we have the following rule:


Simpsons Rule:
b

f x dx

h
h5 (4)
f x0 4 f x1 f x2
f .
3
90

Since the error term involves the fourth derivative of f , Simpsons rule gives exact
results when applied to any polynomial of degree three or less.
4.4 Composite Numerical Integration
4
Theorem 4.4 Let f C a, b , n be even, h b a n , and x j a jh , for each

j 0,1,L , n. There exists a a, b for which the Composite Simpsons rule for
n subintervals can be written with its error term as
b

n
n
1

2
2
b a h 4 (4)

f x dx
f a 2 f x2 j 4 f x2 j 1 f b
f .

3
180
j 1
j 1

Algorithm 4.1 uses the Composite Simpsons rule on n subintervals. This is the most
frequently used general-purpose quadrature algorithm.
Algorithm 4.1 Composite Simpsons Rule
b

To approximate the integral I f x dx :


a

Input: endpoints a, b; even positive integer n .


Output: approximation XI to I .
Step 1 h b a n
Step 2 XI 0 f a f b
3

XI1 0

(*summation of f x2i 1 *)

XI 2 0

(*summation of f x2i *)

Step 3 DO i 1, n 1
Step 4 X a ih
Step 5 If i is even then
XI 2 XI 2 f X
Else
XI1 XI1 f X
End If
Step 6

XI h( XI 0 2* XI 2 4* XI1) 3

Step 7

Output XI

Stop
The Algorithm 4.1 can be described in the following SUBROUTINE.
SUBROUTINE SIMPSONS(N,A,B,F,XI)
C================================================
C ALGORITHM 4.1 Composite Simpson's rule
C PURPOSE
C
Using Composite Simpson's rule,
C
approximate I = INTEGRAL(f(x) dx)) from A TO B:
C INPUT Endpoints A and B; even positive number N; function f
C OUTPUT Approximation XI TO I
C
C-------------------------------------------------------------------------------C
INTEGER N
REAL A,B,XI,
EXTERNAL F
REAL H,X,XI0,XI1,XI2
INTEGER I
H = (B-A)/N
XI0 = F(A)+F(B)
C *** Summation of f(x(2*i-1)) ***

XI1 = 0.0
C *** Summation of f(x(2*i)) ***
XI2 = 0.0
DO 10 I = 1,N-1
X = A+I*H
IF (I.EQ.(2*(I/2))) THEN
XI2 = XI2+F(X)
ELSE
XI1 = XI1+F(X)
END IF
10 CONTINUE
XI = H*(XI0+2.0*XI2+4.0*XI1)/3.0
WRITE(*,29) A,B,XI
29 FORMAT(1X,'Integral of F from',3X,F4.1,3X,'TO',3X,F10.6,3X,'is'
1
,3X,F12.6)
RETURN
END

Example 1 Consider approximating

sin xdx

with an absolute error less than

0.00002, using the Composite Simpsons rule.


The Composite Simpsons rule gives, for some in

0, ,

h
h4

sin x2 j 4 sin x2 j 1 sin


sin .
0 sin x dx 3 sin 0 2

180
j 1
j 1

n
1
2

n
2

Since the absolute error to be less than 0.00002, the inequality

h4
h4
5
sin

0.0002
180
180 180n 4
is

used

to

determine

and

h.

Completing

these

calculations

gives

n 18 n 17.075 .
If n 18 , we use Algorithm 4.1 (see code C4p1.f) and the formula gives
b

sin x dx
a

8
9

2 j 1

j
sin 0 2 sin
sin 2.000010
4 sin
54
18
9
j 1
j 1

The exact answer is 2, so Composite Simpsons rule with n 18 gave an answer well
5

within the required error bound.


Code C4p1.f:
C*****************************************************************
C
C Example 1 (pp201): Using Composite Simpson's rule
C approximate I = INTEGRAL(sinx dx)) from 0 TO PI:
C
C Input: Even positive number N
C
C Outpou: Approximation XI TO I
C****************************************************************
C
INTEGER N
REAL A,B,XI,PI
EXTERNAL F
OPEN(UNIT=10,FILE='c0.doc',STATUS='UNKNOWN')
PI = 3.1415926
A = 0.0
B = PI
N = 18
CALL SIMPSONS(N,A,B,F,XI)
WRITE(10,29) A,B,XI
29 FORMAT(1X,'Integral of F from',3X,F4.1,3X,'TO',3X,F10.6,3X,'is'
1
,3X,F12.6)
STOP
END
C
REAL FUNCTION F(X)
C====================================
C PURPOSE
C
Find the value of function sin(x)
C------------------------------------------------------------C
REAL X
INTRINSIC SIN
F = SIN(X)
RETURN
END
The extension of the Trapezoidal rule (see Figure 4.8) is given. Since the Trapezoidal
6

rule requires only one interval for each application, the integer n can be either odd or
even.
2
Theorem 4.5 Let f C a, b , h b a n , and x j a jh , for each j 0,1,L , n.

There exists a a, b

for which the Composite Trapezoidal rule for n

subintervals can be written with its error term as


b

n 1

b a h f .
h
f x dx f a 2 f x j f b

2
12
j 1

Algorithm 4.1T Composite Trapezoidal Rule


b

To approximate the integral I f x dx .


a

Input: endpoints a, b; positive integer N, function f x .


Output: approximation XI to I .
Step 1 h b a n
Step 2 XI 0 f a f b
XI1 0

(*summation of f xi *)

Step 3 DO i 1, n 1
Step 4

X a ih
XI1 XI1 f X

Step 5 XI h * XI 0 XI1 / 2
Step 6 Output XI
Stop
SUBROUTINE TRAPEZOIDAL(N,A,B,F,XI)
C=================================================
C ALGORITHM 4.1 Composite Trapezoidal rule
C PURPOSE
C
Using Composite Trapezoidal rule
C
approximate I = INTEGRAL(f(x) dx)) from A TO B.
C INPUT Endpoints A and B; positive integer number N; function f
C OUTPUT Approximation XI TO I
C
C----------------------------------------------------------------------------------7

C
INTEGER N
REAL A,B,XI
EXTERNAL F
REAL H,XI0,XI1
INTEGER I
H = (B-A)/N
XI0 = F(A)+F(B)
C *** Summation of f(x(i)) ***
XI1 = 0.0
DO 10 I = 1,N-1
X = A+I*H
XI1 = XI1+F(X)
10 CONTINUE
XI = H*(XI0+2.0*XI1)/2.0
WRITE(*,29) A,B,XI
29 FORMAT(1X,'Integral of F from',3X,F4.1,3X,'TO',3X,F10.6,3X,'is'
1
,3X,F12.6)
RETURN
END
C

Example 2 Consider approximating

sin xdx

with an absolute error less than

0.00002, using the Composite Trapezoidal rule.


The Composite Trapezoidal rule gives, for some in

sin x dx
0

0, ,

n 1

h
h2
sin
0

2
sin
x

sin

sin .

2
12
j 1

Since the absolute error to be less than 0.00002, the inequality

h2
h2
3
sin

0.00002
12
12 12n 2
is

used

to

determine

and

h.

Completing

these

calculations

n 360 n 359.434 .
If n 360 , we use Algorithm 4.1T (see code C4p1T.f) and the formula gives
8

gives

sin x dx
0

359


j
sin 0 2 sin
sin 1.999987 .
720
360
j 1

If n 18 , we use Algorithm 4.1T (see code C4p1T.f) and the formula gives

sin x dx
0

17


j
sin
0

2
sin

sin 1.994920 .
36
18
j 1

From the above two equations, it can be seen that the Composite Trapezoidal rule
with n 360 gave an answer well within the required error bound. However, the
Composite Trapezoidal rule with n 18 clearly did not.

Homework 6
Exercise set 4.4
2

Q7. Determine the values of n and h required to approximate

2x

sin 3 x dx to

within 104 and find the approximation.


(a) Use the Composite Trapezoidal rule (using Algorithm 4.1T and C4p1T.f).
(b) Use the Composite Simpsons rule (using Algorithm 4.1 and C4p1.f).

You might also like