Ch04 DiffInt
Ch04 DiffInt
4.1 INTRODUCTION
The derivative of a function gives a clear indication of the rate of increase or decrease of the function
with respect to its domain. The rate of increase or decrease of a function contributes in the overall
understanding of a system that is governed by such parameters. In general, the derivatives of a function
contribute in the modeling of a given problem which describe the properties and dynamics of the
elements in the problem. For example, in studying the electrostatic field properties of an area the
solution requires a graphing of the first and second derivatives of the points in the area. We will discuss
some useful properties of the derivatives of a function for modeling in the next few chapters.
The analytical derivative of a function f ( x) with respect to the variable x is denoted by f ( x) , or
df
. This derivative returns the exact solution in the form of a function of x . For example, if
dx
f ( x) = x 2 sin x then its analytical derivative is f ( x) = 2 x cos x .
By default, a digital computer does not have the processing capability to produce the analytical
derivative of a function. However, this analytical solution can still be produced through a software that
stores a list of primitive functions and its derivatives in a numerical database. On the computer, the
analytical solution to a derivative is a complex problem that requires several recursive calls to the
functions in its numerical database. Symbolic computing is one area of study that addresses this problem
which involves the construction of a database of mathematical functions for generating the analytical
solution to their derivatives.
The computer gives a good approximation to the derivatives of a function based on some finite
points in the given domain. A numerical approximation to the derivative of a function f ( x) is a solution
expressed as a number, rather than a function of x . The domain of f ( x) is first decomposed into its
discrete form, xi for i = 1, 2,..., m , and the derivatives are computed at these points based on their
relative points.
An integral of a function does the opposite of derivative. Integral returns the differentiated
function to its original value. A numerical approach for finding the integral of a function is needed in
cases where the integral is difficult to compute.
h h2 h3 h n (n)
y i +1 y i + y i + y i + y i + ... + yi . (4.1)
1! 2! 3! n!
Equation (4.1) gives the one-term forward expansion of y = f ( x) . In this equation, h = x is the width
of the x subintervals which are assumed to be uniform. The term yi +1 = f ( xi +1 ) above is equivalent to
yi +1 = f ( xi +1 ) = f ( xi + h) .
f ( xi+2 )
f ( xi+1 )
f ( xi )
f ( xi-1 )
f ( xi-2 )
h h h h
x
xi-2 xi-1 xi xi+1 xi+2
Figure 4.1. Relative position of discrete points of f ( x) in the Taylor series expansion.
Figure 4.1 shows the relative position of the discrete points of f ( x) in the interval xi 2 x xi + 2 .
Taking only up to the second derivative term, this equation reduces to
h h2
yi+1 yi + yi + yi . (4.2a)
1! 2!
( h) ( h) 2 h h2
yi1 yi + yi + yi = yi yi + yi . (4.2c)
1! 2! 1! 2!
(2h) ( 2h) 2 2h 4h 2
yi2 yi + yi + yi = yi yi + yi . (4.2d)
1! 2! 1! 2!
The simplest rule for the first derivative is obtained by taking yi as a subject in the first two terms
of Equation (4.1). It produces the forward-difference rule for the first derivative,
h
yi +1 yi + yii ,
1!
y yi
yi i +1 . (4.3a)
h
The forward-difference rule for the second derivative is obtained by subtracting twice Equation (4.2c) from
Equation (4.2d), and simplying the terms to produce
yi+2 2 yi+1 + yi
yi . (4.3b)
h2
There is also the backward-difference rule for the first derivative, obtained by taking yi as a subject in
the first two terms of Equation (4.2c), to produce
yi yi 1
yi . (4.4a)
h
Similarly, subtracting twice Equation (4.2c) from Equation (4.2d) gives the backward-difference rule for the
second derivative:
yi 2 yi 1 + yi 2
yi . (4.4b)
h2
As the names suggest, the forward-difference and backward-difference rules have the
disadvantages in that they are bias towards the forward and backward points, respectively. A more
balanced approach is the central-difference rules which consider both forward and backward points.
Subtracting Equation (4.2a) from (4.2c), and simplify the terms we obtain the central-difference rule for the
first derivative,
yi +1 yi 1
yi . (4.5a)
2h
yi +1 2 yi + yi 1
yi . (4.5b)
h2
f (a )
f (b )
f (x)
x
a b
Figure 4.2. The area under the curve between f ( x) and the x axis.
The exact methods for evaluating definite integrals have been widely discussed in elementary
calculus classes. Some popular methods involve techniques such as direct integration, substitutions,
integration by parts, partial fractions, and substitutions with the use of trigonometric functions.
However, not all definite integral problems can be solved using such methods. This may be the case
when the function f ( x) is too difficult to integrate, or in the case where f ( x) is given in the form of a
discrete data set. Furthermore, it may not be possible to implement the exact method on the computer
as the computer does not have the analytical skill like a human does. Therefore, approximations using
numerical methods may prove to be an alternative approach and practical for implementation here.
Numerical solutions for evaluating definite integrals are obtained using several methods. Some of
the most fundamental methods include the trapezium, Simpson, Simpsons 3/8 and Gaussian quadrature
methods. We discuss these methods in this section.
h
( f ( xi ) + f ( xi+1 ) ) .
xi +1
xi
f ( x) dx
2
b
With m subintervals, a
f ( x)dx is approximated as the sum of all the areas of the trapeziums,
given as
m1
h
f ( x) dx ( f ( xi ) + f ( xi+1 ) )
b
a
i =0 2
h
= [ f ( x0 ) + f ( xm ) + 2 {( f ( x1 ) + f ( x2 ) + ... + f ( xm1 )}] (4.6)
2
f (x0 )
f (x3 )
f (x2 )
f (x1 )
f (x4 )
f (x)
A1 A2 A3 A4
x
x0 x1 x2 x3 x4
Figure 4.3. Trapezium method with four subintervals.
Equation (4.6) gives the trapezium method for finding the definite integral of a function f ( x) in the
interval from x = a to x = b with m equal subintervals. The method is illustrated in Figure 4.3 using
x4
four equal subintervals. From the figure, it is clear that
x0
f ( x) dx for x0 x x4 is a problem in finding
b
a
f ( x) dx A1 + A2 + A3 + A4
h h h h
= ( f ( x0 ) + f ( x1 ) ) + ( f ( x1 ) + f ( x2 ) ) + ( f ( x2 ) + f ( x3 ) ) + ( f ( x3 ) + f ( x4 ) )
2 2 2 2
h
= [ f ( x0 ) + f ( x4 ) + 2( f ( x1 ) + f ( x2 ) + f ( x3 ))] .
2
Simpsons Method
The trapezium method uses a straight line approximation on two successive f ( x) values in the
b
subintervals for evaluating a
f ( x) dx for a x b . A straight line approximation may not produce a
good solution especially when the number of subintervals is small. This is due to the fact that a straight
line does not approximate a given curve well. Due to the large error imposed, the trapezium method
requires a large number of subintervals in order to produce a good solution.
The accuracy of the trapezium method can be improved by replacing the straight line with a
quadratic function. This idea makes sense as a quadratic curve lies closer to the real curve in the given
function than a straight line. Since a quadratic function requires three points for interpolation, the
method suggests pairs of two subintervals in the approximation. The approach is called the Simpsons
method.
yi
yi+2
yi+1
Ai
x
xi xi+1 xi+2
Figure 4.4. Quadratic polynomial fitting on two subintervals in the Simpsons method.
As the Simpsons method requires two subintervals in each pair, the total number of subintervals
in the given interval must be an even number. Consider the subintervals [ xi , xi+1 ] and [ xi+1 , xi+2 ] which
involve the points, ( xi , f ( xi )) , ( xi+1 , f ( xi+1 )) and ( xi+2 , f ( xi+2 )) . Figure 4.4 shows this scenario with the
dotted curve as the quadratic approximation to the real curve. This curve has an equation given by
where a0 , a1 and a2 are the constants that are to be determined. The quadratic curve interpolates
( xi , yi ) , ( xi+1 , yi+1 ) and ( xi+2 , yi+2 ) . This produces a system of linear equations, given as
yi = a0 + a1 xi + a2 xi2 ,
yi +1 = a0 + a1 xi+1 + a2 xi2+1 ,
yi +2 = a0 + a1 xi +2 + a2 xi2+2 .
h
( yi + 4 yi+1 + yi+2 ) .
xi + 2
xi
f ( x) dx
3
(4.7)
Equation (4.7) can be extended into the case of m even subintervals for x = x0 to x = xm . The
total area is found as a sum of m / 2 subareas where each subarea combines two subintervals. Applying
Equation (4.7), we obtain the Simpsons method for m subintervals, as follows:
h h h
( y0 + 4 y1 + y2 ) + ( y2 + 4 y3 + y4 ) + ... + ( ym2 + 4 ym1 + ym )
xm
x0
f ( x) dx
3 3 3
h
= [ ( y0 + yn ) + 4( y1 + y3 + ... + yn1 ) + 2( y2 + y4 + ... + yn2 ) ] (4.8)
3
An extension to the Simpsons method is the Simpsons 3/8 method. In this method, three
subintervals are combined to produce one subarea based on a cubic polynomial. This approach requires
four points for each subarea, and, therefore, it provides a more accurate approximation to the problem.
With three subintervals for each subarea, the total number of subintervals in the Simpsons 3/8 method
becomes a multiple of three.
Interpolation over four points for each subarea in the Simpsons 3/8 method produces a system of
four linear equations. The interpolating curve is a cubic polynomial given by
y = a0 + a1 x + a2 x 2 + a3 x 3 ,
where a0 , a1 and a2 are constants. The above curve interpolates ( xi , yi ) , ( xi+1 , yi+1 ) , ( xi +2 , yi+2 ) and
( xi+3 , yi+3 ) to produce an approximated subarea, given by
3h
( yi + 3 yi+1 + 3 yi+2 + yi+3 ) .
xi +3
xi
f ( x) dx
8
(4.9)
3h
[( y0 + yn ) + 2( y3 + y6 + ... + yn3 ) + 3( y1 + y2 + y4 + y5 + ... + yn2 + yn1 )] . (4.10)
xm
f ( x) dx
x0 8
1 dn
Pn ( x) =
2n n ! dx n
( ( x2 1)n ) . (4.11)
Table 4.1 lists some of the Legendre polynomials of lower degrees. Legendre polynomials have the
characteristics of being orthogonal over x (1,1) , and satisfy
1
2
Pm ( x) Pn ( x) dx = mn ,
1
2n + 1
f (a )
g (t)
f (b )
f (x)
x
a b t
-1 1
b 1
Figure 4.5. Transformation from a
f ( x) dx to
1
g (t ) dt in the Gauss-Legendres method.
1
interval a x b is transformed into the form of g (t ) dt . This is achieved through a linear
1
relationship given by
(b a )t + (b + a)
x= , (4.12)
2
1
preserves the area under the curve in g (t ) dt , as illustrated in Figure 4.5. We have
1
ba (b a )t + (b + a )
f ( x) dx = f dt .
2 2
b b
ba (b a )t + ( b + a )
f ( x) dx = f dt .
a a
2 2
ba ( b a )t + ( b + a )
g (t ) = f . (4.13)
2 2
1
The second step in this method consists of an approximation to the integral g (t ) dt through a
1
quadrature, given by
n
g (t ) dt wi g (ti ) .
1
(4.14)
1
i =1
The above equation is called the n point Gauss quadrature where n is an integer number greater than
1. The constant wi is called the weight while ti is the argument in the quadrature. The argument ti in
Equation (4.14) is actually a root of the Legendre polynomial given in Table 4.1.
Example 4.1. Given y = x sin x , find y and y at the points along 0 x 2 whose subintervals have
an equal width given by h = 0.3333 using the forward, backward and central-difference methods.
Solution
In this problem, m = (2 0) / .3333 = 6 . The analytical derivatives are given by y = sin x + x cos x and
y = 2 cos x x sin x . Table 4.1 plots the values of xi and yi for i = 0,1,..., 6 . The analytical derivatives at
these points are shown in the fourth and fifth columns of the table.
At i = 0 , y0 and y0 can be evaluated using the forward-difference rules using Equation (4.3a) and
(4.3b):
y y 0.1090 0
y0 1 0 = = 0.3270 ,
h 0.3333
y 2 y1 + y0 0.4122 2(0.1090) + 0
y0 2 = = 1.7481 .
h2 0.33332
It is not possible to evaluate y0 and y0 using the backward and central-difference rules as both
methods involve the terms y1 and y2 which do not exist. The results at other points are shown in
Table 4.1. Through comparison with the analytical (exact) method, the central-difference method
produces the closest approximations for the derivatives.
h
[ yi + yi +1 ]
xi +1
One-Interval Trapezium Method: xi
f ( x)dx
2
h
[( y0 + y n ) + 2( y1 + y 2 + .... + y n1 ]
xn
General Trapezium Method: x0
f ( x) dx
2
1.9
Find 1
f ( x) dx using the Trapezium method from the following data:
i 0 1 2 3 4 5 6 7 8 9
xi 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9
yi = f i 2.5 2.8 2.7 2.6 2.3 2.1 2.4 2.3 2.6 3.0
Solution
Therefore,
1.9
1
f ( x) dx A = A1 + A2 + A3 + A4 + A5 + A6 + A7 + A8 + A9
h h h h
= ( y 0 + y1 ) + ( y1 + y 2 ) + ( y 2 + y 3 ) + ( y 3 + y 4 )
2 2 2 2
h h h h h
+ ( y 4 + y 5 ) + ( y 5 + y 6 ) + ( y 6 + y 7 ) + ( y 7 + y8 ) + ( y8 + y 9 )
2 2 2 2 2
=
h
[( y 0 + y9 ) + 2( y1 + y 2 + y3 + y 4 + y5 + y6 + y 7 + y8 ]
2
=
0.1
[(2.5 + 3.0) + 2(2.8 + 2.7 + 2.6 + 2.3 + 2.1 + 2.4 + 2.3 + 2.6)] = 2.255
2
Formula
h
( f i + 4 f i +1 + f i + 2 )
xi + 2
2-interval Simpson Method: xi
f ( x) dx
3
h
[( y 0 + y n ) + 4( y1 + y3 + ... + y n1 ) + 2( y 2 + y 4 + ... + y n2 )],
xn
General Simpson Method: x0
f ( x) dx
3
1.8
Find 1
f ( x) dx using the Simpson method from the following data:
i 0 1 2 3 4 5 6 7 8
xi 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8
yi = f i 2.5 2.8 2.7 2.6 2.3 2.1 2.4 2.3 2.6
Solution
1.8
1
f ( x) dx A = A1 + A2 + A3 + A4
h h h h
= ( y 0 + 4 y1 + y 2 ) + ( y 2 + 4 y 3 + y 4 ) + ( y 4 + 4 y 5 + y 6 ) + ( y 6 + 4 y 7 + y8 )
3 3 3 3
= [( y 0 + y8 ) + 4( y1 + y 3 + y 5 + y 7 ) + 2( y 2 + y 4 + y 6 )]
h
3
=
0.1
[(2.5 + 2.6) + 4(2.8 + 2.6 + 2.1 + 2.3) + 2(2.7 + 2.3 + 2.4)] = 1.970
3
Formula
3h
( f i + 3 f i +1 + 3 f i + 2 + f i +3 ) ,
xi + 3
3-interval rule: xi
f ( x) dx
8
General rule:
3h
[( f 0 + f n ) + 2( f 3 + f 6 + f 9 + ... + f n3 ) + 3( f1 + f 2 + f 4 + f 5 + ... + f n2 + f n1 )]
xn
x0
f ( x) dx
8
1.9
Find 1
f ( x) dx using the Simpson 3/8 method from the following data:
i 0 1 2 3 4 5 6 7 8 9
xi 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9
yi = f i 2.5 2.8 2.7 2.6 2.3 2.1 2.4 2.3 2.6 3.0
Solution
We get,
3h
( f 0 + 3 f1 + 3 f 2 + f 3 ) + 3h ( f 3 + 3 f 4 + 3 f 5 + f 6 ) + 3h ( f 6 + 3 f 7 + 3 f 8 + f 9 )
b
a
f ( x) dx
8 8 8
= [ f 0 + f 9 + 2( f 3 + f 6 ) + 3( f1 + f 2 + f 4 + f 5 + f 7 + f 8 )]
3h
8
Finally,
3(0.1)
[2.5 + 3.0 + 2(2.6 + 2.4) + 3(2.8 + 2.7 + 2.3 + 2.1 + 2.3 + 2.6)]
1.9
1
f ( x) dx
8
= 2.246
Formula
Gauss Quadrature:
n
(b a)t + (b + a)
g (t ) dt ci g (t i ) , where x =
b 1
n-point: a
f ( x) dx =
1
i =1 2
b 1 1 1
2-point: a
f ( x) dx =
1
g (t ) dt g
+ g
3 3
5 3 8 5 3
+ g (0 ) + g
b 1
3-point: a
f ( x) dx =
1
g (t ) dt g
9
5 9
9 5
2
Find 1
x sin x dx using the 3-point Gauss quadrature method.
Solution
(2 1)t + (2 + 1) t 3
Compute x = = + .
2 2 2
dx 1 dt
Therefore = , and we get dx =
dt 2 2
2 1 t + 3 t + 3 dt 1 t +3 t + 3
1
x sin x dx =
-1 2
sin = -1
2 2 4
sin
2
dt
t + 3 t + 3
We obtain g (t ) = sin .
4 2
5 3 8 5 3
+ g (0) + g
2
1
x sin x dx g
9 5 9
9 5
5 8 5
= g (0.775) + g (0) + g (0.775)
9 9 9
5 8 5
= (0.499) + (0.748) + (0.897)
9 9 9
= 1.440