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
involved in approximating ( )
b
a
f x dx

is called numerical quadrature. It uses a sum


( )
0
n
i i
i
a f x

to approximate ( )
b
a
f x dx

.
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 { }
0 1
, , ,
n
x x x L
from the
interval [ ]
, a b
. Then we integrate the Lagrange interpolating polynomial
( ) ( ) ( )
0
n
n i i
i
P x f x L x

and its truncation error term over [ ]


, a b
to obtain
( ) ( ) ( ) ( )
( ) ( )
( )
( )
( )
( ) ( ) ( )
( 1)
0 0
( 1)
0 0
1 !
1

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

+

+

+
+
+
+


where ( ) x
is in [ ]
, a b
for each
x
and
( )
b
i i
a
a L x dx

, for each
0,1, , i n L
.
The quadrature formula is, therefore,
( ) ( )
0
b
n
i i
i
a
f x dx a f x

,
with error given by
( )
( )
( ) ( ) ( )
( 1)
0
1
1 !
b
n
n
i
i
a
E f x x f x dx
n

+

.
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.
1
To derive the Trapezoidal rule for approximating ( )
b
a
f x dx

, let
0 1
, x a x b
,
h b a and use the linear Lagrange polynomial:
( )
( )
( )
( )
( )
( )
( )
1 0
1 0 1
0 1 1 0
x x x x
P x f x f x
x x x x

+

.
Then,
( )
( )
( )
( )
( )
( )
( ) ( ) ( ) ( ) ( )
1 1
0 0
1 0
0 1 0 1
0 1 1 0
1
2
x x b
a x x
x x x x
f x dx f x f x dx f x x x x x dx
x x x x

1
+ +
1

]

Since ( ) ( )
0 1
x x x x
does not change sign on [ ]
0 1
, x x
, the Weighted Mean Value
Theorem for Integrals can be applied to the error term to give, for some ( )
0 1
in , x x
,
( ) ( ) ( ) ( ) ( ) ( ) ( )
( )
( )
( )
1 1
0 0
1
0
0 1 0 1
3 3
1 0
0 1
.
3 2 6
x x
x x
x
x
f x x x x x dx f x x x x dx
x x
x h
f x x x f



+ 1
+
1
]

Thus, we have
( )
( )
( )
( )
( )
( )
( ) ( )
( )
( ) ( ) ( )
1
0
2 2
3
1 0
0 1
0 1 1 0
3
1 0
0 1
2 2 12
.
2 12
x
b
a
x
x x x x
h
f x dx f x f x f
x x x x
x x
h
f x f x f

1

+ 1

1
]

+ 1
]

Since
1 0
h x x
, we have the following rule:
Trapezoidal Rule:
( ) ( ) ( ) ( )
3
0 1
.
2 12
b
a
h h
f x dx f x f x f + 1
]
This is called the Trapezoidral rule because when
f
is a function with positive
values, ( )
b
a
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.
2
Simpsons rule results from integrating over [ ]
, a b
the second Lagrange polynomial
with nodes
0 2
, x a x b
, and
1
x a h +
, where ( ) 2 h b a
. (See Figure 4.4).
Therefore,
( )
( ) ( )
( ) ( )
( )
( ) ( )
( ) ( )
( )
( ) ( )
( ) ( )
( )
( ) ( ) ( )
( ) ( )
1
0
3
0
1 2 0 2
0 1
0 1 0 2 1 0 1 2
0 1 0 1 3 (3)
2
2 0 2 1
.
6
x b
a x
x
x
x x x x x x x x
f x dx f x f x
x x x x x x x x
x x x x x x x x x x
f x dx f x dx
x x x x


+

1
+ +
1

]

Similarly, we have the following rule:


Simpsons Rule:
( ) ( ) ( ) ( ) ( )
5
(4)
0 1 2
4 .
3 90
b
a
h h
f x dx f x f x f x f + + 1
]
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
Theorem 4.4 Let [ ]
4
, f C a b
,
n
be even, ( ) h b a n
, and
j
x a jh +
, for each
0,1, , . j n L
There exists a ( ) , a b
for which the Composite Simpsons rule for
n
subintervals can be written with its error term as
( ) ( ) ( ) ( ) ( )
( )
( )
1
4
2 2
(4)
2 2 1
1 1
2 4 .
3 180
n n
b
j j
j j
a
b a h
h
f x dx f a f x f x f b f

1
+ + +
1
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
To approximate the integral ( ) :
b
a
I f x dx

Input: endpoints
, ; a b
even positive integer
n
.
Output: approximation to XI I .
Step 1 ( ) h b a n
Step 2 ( ) ( ) 0 XI f a f b +
3
1 0 XI (*summation of ( )
2 1 i
f x

*)
2 0 XI (*summation of ( )
2i
f x
*)
Step 3 DO
1, 1 i n
Step 4 X a ih +
Step 5 If i is even then
( ) 2 2 XI XI f X +
Else
( ) 1 1 XI XI f X +
End If
Step 6 ( 0 2* 2 4* 1) 3 XI h XI XI XI + +
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)) ***
4
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
0
sin xdx

with an absolute error less than


0.00002, using the Composite Simpsons rule.
The Composite Simpsons rule gives, for some ( ) in 0,
,
( ) ( ) ( ) ( ) ( ) ( )
1
4
2 2
2 2 1
1 1
0
sin sin 0 2 sin 4 sin sin sin .
3 180
n n
j j
j j
h h
x dx x x


1
1
+ + +
1
1
]

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


4 4 5
4
sin 0.0002
180 180 180
h h
n

<
is used to determine
n
and h . Completing these calculations gives
( ) 18 17.075 n n >
.
If 18 n , we use Algorithm 4.1 (see code C4p1.f) and the formula gives
( ) ( )
( )
( )
8 9
1 1
2 1
sin sin 0 2 sin 4 sin sin 2.000010
54 9 18
b
j j
a
j
j
x dx


1 _
_
+ + +
1
, 1
, ]

The exact answer is 2, so Composite Simpsons rule with 18 n 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.
Theorem 4.5 Let [ ]
2
, f C a b
, ( ) h b a n
, and
j
x a jh +
, for each
0,1, , . j n L

There exists a ( ) , a b
for which the Composite Trapezoidal rule for
n

subintervals can be written with its error term as
( ) ( ) ( ) ( )
( )
( )
2
1
1
2 .
2 12
b
n
j
j
a
b a h
h
f x dx f a f x f b f

1
+ +
1
]

Algorithm 4.1T Composite Trapezoidal Rule


To approximate the integral ( )
b
a
I f x dx

.
Input: endpoints
, ; a b
positive integer N, function ( ) f x
.
Output: approximation to XI I .
Step 1 ( ) h b a n
Step 2 ( ) ( ) 0 XI f a f b +
1 0 XI (*summation of ( )
i
f x
*)
Step 3 DO
1, 1 i n
Step 4 X a ih +
( ) 1 1 XI XI f X +
Step 5 ( ) * 0 1 / 2 XI h XI XI +
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
0
sin xdx

with an absolute error less than


0.00002, using the Composite Trapezoidal rule.
The Composite Trapezoidal rule gives, for some ( ) in 0,
,
( ) ( ) ( ) ( ) ( )
2 1
1
0
sin sin 0 2 sin sin sin .
2 12
n
j
j
h h
x dx x

1
+ +
1
]

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


( )
2 2 3
2
sin 0.00002
12 12 12
h h
n

<
is used to determine
n
and h . Completing these calculations gives
( ) 360 359.434 n n >
.
If 360 n , we use Algorithm 4.1T (see code C4p1T.f) and the formula gives
8
( ) ( ) ( )
359
1
0
sin sin 0 2 sin sin 1.999987
720 360
j
j
x dx

1
_
+ +
1
,
]

.
If 18 n , we use Algorithm 4.1T (see code C4p1T.f) and the formula gives
( ) ( ) ( )
17
1
0
sin sin 0 2 sin sin 1.994920
36 18
j
j
x dx

1
_
+ +
1
,
]

.
From the above two equations, it can be seen that the Composite Trapezoidal rule
with 360 n gave an answer well within the required error bound. However, the
Composite Trapezoidal rule with 18 n clearly did not.
Homework 6
Exercise set 4.4
Q7. Determine the values of
n
and h required to approximate ( )
2
2
0
sin 3
x
e x dx

to
within
4
10

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).
9

You might also like