Chapter 4 Numerical Differentiation and Integration 4.3 Elements of Numerical Integration
Chapter 4 Numerical Differentiation and Integration 4.3 Elements of Numerical Integration
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
+
+
+
+
+
+
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
1
+ +
1
]
1
+ + +
1
1
]
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
1
1
+ + +
1
1
]
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
]
.
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
1
+ +
1
]
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