Numerical Differentiation

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

4.

1 INTRODUCTION
Numerical differentiation deals with the following problem: given the function y = f (x) find one of its derivatives
at the point x = x
k
. Here, the term given implies that we either have an algorithm for computing the function,
or possesses a set of discrete data points (x
i
, y
i
), i = 1, 2, ., n. In other words, we have a finite number of
(x, y) data points or pairs from which we can compute the derivative. Numerical differentiation is a method
to compute the derivatives of a function at some values of independent variable x, when the function f (x) is
explicitly unknown, however it is known only for a set of arguments.
Like the numerical interpolation discussed in Chapter 5, a number of formulae for differentiation are
derived in this chapter. They are:
(a) Derivatives based on Newtons forward interpolation formula. This formula is used to find the
derivative for some given x lying near the beginning of the data table.
(b) Derivatives based on Newtons backward interpolation formula. This formula is suitable to find
the derivative for a point near the end of the data table.
(c) Derivatives based on Stirlings interpolation formula. This formula is used to find the derivative
for some point lying near the middle of the tabulated value.
A method to find the maxima and minima of a given function is also discussed in this chapter.
4.2 DERIVATIVES BASED ON NEWTONS FORWARD
INTERPOLATION FORMULA
Suppose the function y = f (x) is known at (n + 1) equispaced points x
0
, x
1
, ., x
n
and they are y
0
, y
1
, ., y
n
respectively i.e., y
i
= f (x
i
), i = 0, 1, ., n. Let x
i
= x
0
+ ih and
i
x x
u
h

, where h is the spacing.


Referring to Chatper 5, the Newtons forward interpolation formula is
2
0 0 0 0
...
( 1) ( 1) ( 1)
...
( )
2! !
n
u u u u u n
y f x y u y y y
n

- - - -
C
H
A
P
T
E
R
4
Numerical Differentiation
4
Sanjay IInd Proof13/8/2009
108 // Numerical Methods //

2 3 2 4 3 2
2 3 4
0 0 0 0 0
3 2 6 11 6
2! 3! 4!
u u u u u u u u u
y u y y y y
+ +
= + + + +

5 4 3 2
5
0
10 35 50 24
...
5!
u u u u u
y
+ +
+ + (4.1)
Differentiating Eq.(4.1) w.r.t. x, we get

2 3 2
2 3 4
0 0 0 0
1 2 1 3 6 2 4 18 22 6
( )
2! 3! 4!
u u u u u u
f x y y y y
h

+ +
= + + +


4 3 2
5
0
5 40 105 100 24
5!
u u u u
y
+ +
+ (4.2)
Note here that
1 du
dx h
=
Differentiating Eq.(4.2) w.r.t. x, we obtain
2 3 2
2 3 4 5
0 0 0 0
2
1 6 6 12 36 22 20 120 210 100
...
( )
3! 4! 5!
u u u u u u
f x y y y y
h

+ +
= + + + +



(4.3)
and so on.
Equations (4.2) and (4.3) give the approximate derivatives of f (x) at arbitrary point x = x
0
+ uh.
When x = x
0
, u = 0, Eqs.(4.2) and (4.3) become
2 3 4 5
0 0 0 0 0 0
1 1 1 1 1
...
( )
2 3 4 5
f x y y y y y
h

= + +


(4.4)
and
2 3 4 5
0 0 0 0 0
2
1 11 5
...
( )
12 6
f x y y y y
h

= + +


(4.5)
and so on.
Example E4.1
From the following table find the value of
dy
dx
and
2
2
d y
dx
at the point x = 1.0.
x 1 1.1 1.2 1.3 1.4 1.5
y 5.4680 5.6665 5.9264 6.2551 6.6601 7.1488
// Numerical Differentiation // 109
Solution:
The forward difference table is
x y y
2
y
3
y
1.0 5.4680
0.1985
1.1 5.6665 0.0614
0.2599 0.0074
1.2 5.9264 0.0688
0.3287 0.0074
1.3 6.2551 0.0763
0.4050 0.0074
1.4 6.6601 0.0837
0.4887
1.5 7.1488
Here x
0
= 1.0 and h = 0.1. Then u = 0 and hence

2 3
0 0 0
1 1 1 1 1 1
...
(1.0) 0.1985 (0.0614) (0.0074) 1.7020
2 3 0.1 2 3
dy
y y y y
dx h

= = + = + =


[ ]
2
3
0 0
2 2 2
1 1
...
(1.0) 0.0614 0.0074 5.4040
(0.1)
d y
y y y
dx h

= = + = =

Example E4.2
Obtain the first and second derivatives of the function tabulated below at the points x = 1.1 and x = 1.2.
x : 1 1.2 1.4 1.6 1.8 2.0
y : 0 0.128 0.544 1.298 2.440 4.02
Solution:
We first construct the forward difference table as shown below.
x y y
2
y
3
y
4
y
1.0 0
0.128
1.2 0.128 0.288
0.416 0.05
1.4 0.544 0.338 0
0.754 0.05
1.6 1.298 0.388 0
1.142 0.05
1.8 2.440 0.438
1.580
2.0 4.02
Since x = 1.1 is a non-tabulated point near the beginning of the table, we take x
0
= 1.0 and compute

110 // Numerical Methods //

0
1.1 1.0
0.5
0.2
x x
p
h


Hence,
2
2 3
0 0 0
1 2 1 3 6 2
2 6
dy p p p
y y y
dx h
(
-
- -
(
(


2
1 3(0.5) 6(0.5) 2
0.128 0 (0.05) 0.62958
0.2 6
(
-
- -
(
(

2
2 3
0 0
2 2 2
1 1
( 1) [0.288 (0.5 1)0.05] 6.575
(0.2)
d y
y p y
dx h
(
- -

Now, x = 1.2 is a tabulated point near the beginning of the table. For x = x
0
= 1.2, p = 0 and
2 3
0 0 0
1 1 1 1 1 1
0.416 (0.338) (0.05) 1.31833
2 3 0.2 2 3
dy
y y y
dx h
( (
- -
( (

2
2 3
0 0
2 2 2
1 1
[ ] [0.338 0.05] 7.2
(0.2)
d y
y y
dx h

Example E4.3
Find the first and second derivatives of the functions tabulated below at the point x = 1.1 and x = 1.2.
x 1 1.2 1.4 1.6 1.8 2.0
y 0 0.1 0.5 1.25 2.4 3.9
Solution:
First, we construct the forward difference table:
x y y
2
y
3
y
4
y
1.0 0
0.1
1.2 0.1 0.3
0.4 0.05
1.4 0.5 0.35 0
0.75 0.05
1.6 1.25 0.40 0
1.15 0.05
1.8 2.40 0.45
1.5
2.0 3.90
Here x = 1.1 is a non-tabulated point near the beginning of the table. For x
0
= 1.0,

0
1.1 1.0
0.5
0.2
x x
p
h



// Numerical Differentiation // 111
Hence
2
2 3
0 0 0
1 2 1 3 6 2
2 6
dy p p p
y y y
dx h
(
-
- -
(
(


2
1 3(0.5) 6(0.5) 2
0.1 0 (0.05) 0.48958
0.2 6
(
-
- -
(
(

2
2 3
0 0
2 2 2
1 1
( 1) [0.3 (0.5 1)0.05] 6.875
(0.2)
d y
y p y
dx h
(
- -

For x = 1.2, it is a tabulated point near the beginning of the table.
Let x = x
0
= 1.2, p = 0
2 3
0 0 0
1 1 1 1 1 1
0.4 (0.35) (0.05) 1.208
2 3 0.2 2 3
dy
y y y
dx h
( (
- -
( (

2
2 3
0 0
2 2 2
1 1
[ ] [0.35 0.05] 7.5
(0.2)
d y
y y
dx h

4.3 DERIVATIVES BASED ON NEWTONS BACKWARD
INTERPOLATION FORMULA
Here, we assume the function y = f (x) is known at (n + 1) points x
0
, x
1
, ., x
n
, i.e., y
i
= f (x
i
), i = 0, 1, 2, .,
n are known. Let x
i
= x
0
+ ih, i = 0, 1, 2, ., n and
n
x x
v
h

.
Then, the Newtons backward interpolation formula from Chapter 5 is given by
2 3 4
( 1) ( 1)( 2) ( 1)( 2)( 3)
( )
2! 3! 4!
n n n n n
v v v v v v v v v
f x y v y y y y
- - - - - -
- - - -

5
( 1)( 2)( 3)( 4)
...
5!
n
v v v v v
y
- - - -
- - (4.6)
When the Eq.(4.6) is differentiated w.r.t. x successively, we obtain
2 3 2
2 3 4
1 2 1 3 6 2 4 18 22 6
( )
2! 3! 4!
n n n n
v v v v v v
f x y y y y
h

- - - - - -
- - -

(4.7)

4 3 2
5
5 40 105 100 24
...
5!
n
v v v v
y
(
- - - -
- -
(
(


2 3 2
2 3 4 5
2
1 6 6 12 36 22 20 120 210 100
...
( )
3! 4! 5!
n n n n
v v v v v v
f x y y y y
h
(
- - - - - -
- - - -
(
(

and so on. (4.8)
112 // Numerical Methods //
Equations (4.7) and (4.8) can be used to determine the approximate differentiation of first, second, etc. order
at any point x, where x = x
n
+ vh.
If x = x
n
, then v = 0.
Equations (4.7) and (4.8) become
2 3 4 5
1 1 1 1 1
...
( )
2 3 4 5
n n n n n n
f x y y y y y
h
(
- - - - -
(

(4.9)
and
2 3 4 5
2
1 11 5
...
( )
12 6
n n n n n
f x y y y y
h
(
- - - -
(

(4.10)
Example E4.4
A slider in a machine moves along a fixed straight rod. Its distance x(m) along the rod are given in the
following table for various values of the time t (seconds).
t(sec.) 1 2 3 4 5 6
x(m) 0.0201 0.0844 0.3444 1.0100 2.3660 4.7719
Find the velocity and acceleration of the slider at time t = 6 sec.
Solution:
The backward difference table is
t x x
2
x
3
x
4
x
5
x
1.0 0.0201
2.0 0.0844 0.0643
3.0 0.3444 0.2600 0.1957
4.0 1.0100 0.6656 0.4056 0.2100
5.0 2.3660 1.3560 0.6904 0.2847 0.0748
6.0 4.7719 2.4059 1.0499 0.3595 0.0748 0.0000
Here h = 1.0
2 3 4 5
1 1 1 1 1
...
2 3 4 5
dx
x x x x x
dt h
(
- - - - -
(

1 1 1 1 1
2.4059 (1.0499) (0.3595) (0.0748) (0.0) 3.0694
1.0 2 3 4 5
(
- - - -
(


2
2 3 5
2 2 2
1 11 1 11 5
...
1.0499 0.3595 (0.0748) (0) 1.4780
12 12 6
(1.0)
d x
x x x
dt h
( (
- - - - - -
( (

4.4 DERIVATIVES BASED ON STIRLINGS INTERPOLATION FORMULA
Suppose y
i
= f (x
i
), i = 0, 1, ., n are given for 2n + 1 equispaced points x
0
, x
1
, x
2
, ., x
n
, where
x
i
= x
0
ih, i = 0, 1, ., n.
// Numerical Differentiation // 113
The Stirlings interpolation polynomial is given by

3 3 2 3
2 1 0 2 1
0 1
( )
1! 2 2! 3! 2
y y y y u u u u
f x y y

(
- - (
- - -
(
(
(


5 5 4 2 5 3
4 3 2
2
5 4
...
4! 5! 2
y y u u u u u
y

(
- -
- - -
(
(

(4.11)
where
0
x x
u
h

When Eq.(4.11) is differentiated with respect to x successively, we obtain


3 3 2
2 1 0 2 1
1
1 3 1
( )
2 6 2
y y y y u
f x u y
h


- -
- -
(
,

5 5 3 4 2
4 3 2
2
2 5 15 4
...
12 120 2
y y u u u u
y

(

- -
- - - (
(
, (

(4.12)
and
5 5 3 3 2 3
2 4 3 2 2
1 2
2
1 1 6 1 2 3
...
( )
2 12 12 2
y y y u u u
f x y u y
h


(
- -
- - - - (
(
, (

(4.13)
At x = x
0
, u = 0 and Eqs.(4.12) and (4.13) become
5 5 3 3
0 1 2 3 1 2
0
1 1 1
...
( )
2 6 2 30 2
y y y y y y
f x
h

(
- - -
- - (
( (
, , (

(4.14)
2 4
0 1 2
2
1 1
...
( )
12
f x y y
h

(
-
(

(4.15)
Example E4.5
Find
dy
dx
and
2
2
d y
dx
for x = 0.2 for the data given in the following table
x 0 0.1 0.2 0.3 0.4 0.5
y 0 0.10017 0.20134 0.30452 0.41076 0.52115
114 // Numerical Methods //
Solution:
Construct the following difference table.
x y y
2
y
3
y
4
y
0 0
0.10017
0.1 0.10017 0.001
0.10017 0.00101
0.2 0.20134 0.00201 0.00004
0.10318 0.00105
0.3 0.30452 0.00306 0.00004
0.10624 0.00109
0.4 0.41076 0.00415
0.11039
0.5 0.52115
Here, we use Stirlings formula. Hence, for x = 0.2, we have
3 3
1 0 2 1
1 1
2 6 2
y y y y dy
dx h

(
- -

(
(


1 0.10117 0.10318 1
(0.00101 0.00105 1.020033
0.1 2 12
- (
-
(

2
2 4
1 2
2 2 2
1 1 1 1
0.00201 (0.00004) 0.200666
12 12
(0.1)
d y
y y
dx h

( (

( (

Example E4.6
Compute the values of f '(3.1) and f '(3.2) using the following table.
x 1 2 3 4 5
f(x) 0 1.4 3.3 5.6 8.1
Solution:
The central difference table is
x y = f(x) y
2
y
3
y
4
y
x
2
= 1 0
1.4
x
1
= 2 1.4 0.5
1.9 0.1
x
0
= 3 3.3 0.4 0.1
2.3 0.2
x
1
= 4 5.6 0.2
2.5
x
2
= 5 8.1

// Numerical Differentiation // 115
Let
x
0
= 3, h = 1,
3.1 3
0.1
1
u


.3 3 2 3
2 4 1 0 2 1
1 2
1 3 1 2
...
(3.1)
2 6 2 12
y y y y u u u
f u y y
h


(

- -
- - - - (
(
, (

2 3
1 1.9 2.3 3(0.1) 1 0.1 0.2 2(0.1) 0.1
0.1(0.4) ( 0.1)
1 2 6 2 12
(
-
- - -
(
(
,
(

= [2.1 + 0.04 + 0.02425 + 0.00082] = 2.16507
2 3 2
2 4 2 1
1 2
2
1 6 1
...
(3.1)
2 12
y y u
f y u y
h


(
-
- - - (
(
, (

2
2
1 0.1 0.2 6(0.1) 1
0.4 0.1 ( 0.1) [0.4 0.015 0.00783] 0.39283
2 12 1
(

- - -
(
(
,
(

4.5 MAXIMA AND MINIMA OF A TABULATED FUNCTION
From calculus, we know that if a function is differentiable, then the maximum and minimum value of that
function can be determined by equating the first derivative to zero and solving for the variable. This method
is extendable for the tabulated function.
Now, consider the Newtons forward difference formula given in Eq.(4.1).
Differentiating Eq.(4.1) w.r.t. u, we obtain
2
2 3
0 0 0
2 1 3 3 2
...
2 6
dy u u u
y y y
du
-
- - - (4.16)
For maximum or minimum,
dy
du
= 0. Neglecting the term after the third difference to obtain a quadratic equation
in u.
Hence
2
2 3
0 0 0
1 1
0
2 2 2 3
u u
y u y y


- - -
( (
,
,
(4.17)
or
3
2 2 2 2 3 0
0 0 0 0 0
1 1 1
0
2 2 2 3
y
u y y u y y y
( (
- - -
( (

or a
0
u
2
+ a
1
u + a
2
= 0 (4.18)
which gives the values of u.
116 // Numerical Methods //
Here
3
0 0
1
2
a y
2 3
1 0 0
1
2
a y y
2 3
2 0 0 0
1 1
2 3
a y y y - (4.19)
The values of x will then be obtained from x = x
0
+ uh.
Example E4.7
Find x correct to four decimal places for which y is maximum from the following data given in tabular form.
Find also the value of y.
x 1 1.2 1.4 1.6 1.8
y 0 0.128 0.544 1.298 2.44
Solution:
We first construct the forward difference table as shown below:
x y y
2
y
3
y
1.0 0
0.128
1.2 0.128 0.288
0.416 0.05
1.4 0.544 0.338
0.754 0.05
1.6 1.298 0.388
1.142
1.8 2.44
Let x
0
= 1.0
Here
0
1
(0.05) 0.025
2
a
1
1
0.288 (0.05) 0.2630
2
a
2
1 1
0.128 (0.288) (0.05) 0.128 0.144 0.0166 0.000666
2 3
a - -
Hence a
0
u
2
+ a
1
u + a
2
= 0, which gives the value of u.
or 0.025u
2
+ 0.263u + 0.000666 = 0

2
1,2
0.263 (0.263) 4(0.025)(0.000666)
(0, 10.5175)
2(0.025)
u


Hence u = 0 or u = 10.5175
Therefore, x = 1.0 and x = 1.0 10.5175(0.2) = 1.1035
// Numerical Differentiation // 117
At x = 1.0, y = 0 and at x 1.1035, we apply the Newtons forward interpolation formula.

2 3
0 0 0 0
( 1) ( 1)( 2)
...
2! 3!
u u u u u
y y u y y y

- - - -

( 10.5175)( 11.5175)
0 ( 10.5175)(0.128) (0.288)
2
( 10.5175)( 11.5175)( 12.5175)
(0.05)
(3)(2)(1)

- -

-
= 3.46132 (maximum value)
4.6 CUBIC SPLINE METHOD
The cubic spline method described in Section 5.7 of Chapter 5 can be used to find the first and second
derivatives of a function. The method requires two steps. In the first step, the cubic splines are constructed
with suitable intervals. In the second step, the first and second derivatives are determined from the appropriate
cubic spline. These steps are illustrated by an example as follows:
Example E4.8
Given y = f (x) = cos x, 0 x /2. Determine
(a) the natural cubic spline in the interval 0 x /4 and /4 x /2
(b) the first and second derivatives f (/8) and f (/8).
Solution:
Here h =
4

, y
0
= cos 0 = 1, y
1
= cos
1
4 2

and y
2
= cos
4

= 0. Also k
0
= k
2
= 0.
From Eq.(5.85) of Sec.5.7 of Chapter 5,
1 1 1 1
2
6
4 [ 2 ]
i i i i i i
k k k y y y
h
- -
- - -
, i = 2, 3, ., n 1 (E.1)
or
0 1 2 0 1 2
2
6
4 [ 2 ] k k k y y y
h
- - -
or ( ) 1
2
96
4 1 2 k

or
( ) 1
2
24
1 2 1.007247 k

Therefore, the cubic spline is given by Eq.(5.81) of Chapter 5,


4
4 2
0,
,
( )
( )
( )
f x
f x
f x

'

'
(E.2)

You might also like