Professional Documents
Culture Documents
Chapter 4 Numerical Differentiation and Integration 4.3 Elements of Numerical Integration
Chapter 4 Numerical Differentiation and Integration 4.3 Elements of Numerical Integration
Chapter 4 Numerical Differentiation and Integration 4.3 Elements of Numerical Integration
involved in approximating
f x dx
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
Pn x f xi Li x
i 0
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
f x dx ai f xi ,
i 0
n
1
E f
x xi f ( n1) x dx .
n 1 ! a i 0
f x dx ,
let x0 a, x1 b ,
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
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 .
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
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.
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
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
sin xdx
0, ,
h
h4
180
j 1
j 1
n
1
2
n
2
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
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
n 1
b a h f .
h
f x dx f a 2 f x j f b
2
12
j 1
(*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
sin xdx
sin x dx
0
0, ,
n 1
h
h2
sin
0
2
sin
x
sin
sin .
2
12
j 1
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
2x
sin 3 x dx to