Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 17

Numerical Differentiation

C. S. Mathpati
Institute of Chemical Technology
Mumbai
Taylor series expansion
Inclusion of the 2
nd
derivative term improves the accuracy to O(h
2
).
Similar improved versions can be developed for the backward and centered
formulas as well as for the approximations of the higher derivatives
) (
) ( ) ( ) (
) (

) (
) ( ) ( ) (
2
1
2
1
2
2
h O h
x f
h
x f x f
x f
h
x f
h x f x f x f
i i i
i
i
i i i
+
' '

=
'
+
' '
+
'
+ =
+
+

) (
) ( ) ( ) (
) ( h O
h
x f x f x f
x f
i i i
i
+
+
= ' '
+ +
2
1 2
2
) (
) ( ) ( ) (
) (
) (
) ( ) ( ) ( ) ( ) (
) (
2
1 2
2
2
1 2 1
2
3 4
2
2
h O
h
x f x f x f
x f
h O h
h
x f x f x f
h
x f x f
x f
i i i
i
i i i i i
i
+
+
= '
+
+

= '
+ +
+ + +
High Accuracy Differentiation Formulas
Forward finite-divided-difference formulas
( )
( ) ( )
( )
( ) ( ) ( )
( )
( ) ( ) ( )
( )
( ) ( ) ( ) ( )
1
2 1
2 1
2
3 2 1
2
'
4 3
'
2
2
''
4 5 2
''
i i
i
i i i
i
i i i
i
i i i i
i
first derivative
f x f x
f x
h
f x f x f x
f x
h
second derivative
f x f x f x
f x
h
f x f x f x f x
f x
h
+
+ +
+ +
+ + +

=
+
=
+
=
+ +
=
( )
( )
( )
( )
2
2
O h
O h
O h
O h
4
Backward finite-divided-difference formulas
5
Centered finite-divided-difference formulas
Numerical differentiation
x
y y
x x
y y
mB
A

=
2 3
2 3
2 3
x
1
x
2
x
3
y
1
y
2
y
3
x x
y=f(x)
True slope
A

B

C

x
y y
x
y y
x
y y m m
m
B A
C
A

=
|
.
|

\
|
A

+
A

=
+
=
2 2
1
2
1 3 2 3 1 2
x
y y
x x
y y
mA
A

=
1 2
1 2
1 2
lim
0
dy y
x dx x
A
A
=

: backward
difference
: central difference
: forward difference
The diff function
The diff Function
d= diff (x)

Backward difference & central difference method



example x=[5, 7, 12, -20];
d= diff(x)

d = 2 5 -32
First, create a file called fx1.m which contains y=f(x):
function y = fx1(x)
y = 1.2 - .25*x - .5*x.^2 - .15*x.^3 -.1*x.^4 ;

Command window:
>> x=0:.25:1
0 0.25 0.5 0.75 1
>> y = fx1(x)

1.2 1.1035 0.925 0.6363 0.2

>> d = diff(y) ./ diff(x)

d = -0.3859 -0.7141 -1.1547 -1.7453

Forward: x = 0 0.25 0.5 0.75 1
Backward: x = 0.25 0.5 0.75 1
x f(x)
i-2 0 1.2
i-1 0.25 1.1035
i 0.50 0.925
i+1 0.75 0.6363
i+2 1 0.2
Differentiation Using MATLAB
Forward difference of accuracy O(h
2
) is computed as:



Backward difference of accuracy O(h
2
) is computed as:



9
Example : High Accuracy Differentiation Formulas
f(x) = -0.1x
4
0.15x
3
0.5x
2
0.25x + 1.2
At x = 0.5 True value for First Derivative = -0.9125
Using finite divided differences and a step size of h = 0.25

t
0.2 4(0.6363) 3(0.925)
(0.5) 0.8593 5.82%
2(0.25)
f c
+
' = = =
Forward
O(h)
Backward
O(h)
Estimate -1.155 -0.714

t
(%) 26.5 21.7
x f(x)
i-2 0 1.2
i-1 0.25 1.1035
i 0.50 0.925
i+1 0.75 0.6363
i+2 1 0.2
t
3(0.925) 4(1.1035) 1.2
(0.5) 0.8781 3.77%
2(0.25)
f c
+
' = = =
Polynomial derivatives
n n
n n n
a x a x a x a x a x f + + + + + =

1
2
3
1
2 1 ) (
1
2
2
1
1 ) 1 (

+ + + = n
n n
a x a n x na
dx
df

1
2
2
1
1

+ + + = n
n n
b x b x b
Command Description
d=diff(x) Returns a vector d containing the differences between adjacent elements
in the vector x.
b=polyder(p) Returns a vector b containing the coefficients of the derivative of the
polynomial represented by the vector p.
b=polyder(p1,p
2)
Returns a vector b containing the coefficients of the polynomial that is
the derivative of the product of the polynomials represented by p1 and
p2. cf. equivalent comment: b=polyder(conv(p1,p2))
[num, den]=
polyder(p2,p1)
Returns the vector num and den containing the coefficients of the
numerator and denominator polynomials of the derivative of the
quotient p
2
/p
1
, where p1 and p2 are polynomials.
Numerical differentiation functions
b = polyder (p)
p = [a1,a2,,an]
b = [b1,b2,,bn-1]
b = polyder (p1,p2)
[num, den] = polyder(p2,p1)

Polynomial derivatives
Result: der2 = [10, 4, -3]
prod = [150, 80, -7]
num = [50, 40, 23]
den = [25, 20, 4]
Example p1= 5x +2
p2=10x2+4x-3
12
Richardson Extrapolation
There are two ways to improve derivative estimates when employing finite divided
differences:
Decrease the step size, or
Use a higher-order formula that employs more points.
A third approach, based on Richardson extrapolation, uses two derivative estimates (with O(h
2
) error) to
compute a third (with O(h
4
) error) , more accurate approximation. We can derive this formula following
the same steps used in the case of the integrals:


) (
3
1
) (
3
4
2 /
1 2 1 2
h D h D D h h ~ =
Example: using the previous example and Richardsons formula, estimate the first
derivative at x=0.5 Using Centered Difference approx. (with error O(h
2
)) with
h=0.5 and h=0.25 :

D
h=0.5
(x=0.5) = (0.2-1.2)/1 = -1 [
t
=(-.9125+1)/-.9125 = -9.6% ]
D
h=0.25
(x=0.5) = (0.6363-1.103)/0.5=-0.9343 [
t
=(-.9125+0.9343)/-.9125 = -2.4% ]

The improved estimate is:
D = 4/3(-0.9343) 1/3(-1) = -0.9125 [
t
=(-.9125+.9125)/-.9125 = 0% perfect!]
f(x) = -0.1x4 0.15x3 0.5x2 0.25x + 1.2
At x = 0.5 True value for First Derivative = -0.9125
13
Derivatives of Unequally Spaced Data
Data from experiments or field studies are often collected at unequal intervals. One
way to handle such data is to fit a Lagrange interpolating polynomial, and then
calculate the 1
st
derivative. As an example, second order Lagrange interpolating
polynomial is used below:








*Note that any three consecutive points, x
i-1
x
i
and x
i+1
can be used to calculate the
derivative. The points do not need to be spaced equally.
( )( )
( )( )
( )( )
i i i i
i i
i
i i i i
i i
i
i i i i
i i
i
x x x x
x x x
x f
x x x x
x x x
x f
x x x x
x x x
x f x f


+


+


=
'
+ +

+
+
+
+
+

1 1 1
1
1
1 1
1 1
1 1 1
1
1
2
) (
2
) (
2
) ( ) (
[

=
=
=

=
=
n
i j
j
j i
j
i
n
i
i i n
x x
x x
x L
x f x L x f
0
0
) (
) ( ) ( ) (
14
( )( )
( )( )
( )( )
2 7 -
0
W/m 56 . 70 ) 333 . 1 )( 840 )( 1800 ( 3.5x10 - 0) q(z
: compute to used be can which
/ 333 . 1 333 . 1 4 . 14 4 . 14
25 . 1 75 . 3 0 75 . 3
25 . 1 0 ) 0 ( 2
10
75 . 3 25 . 1 0 25 . 1
75 . 3 0 ) 0 ( 2
12
75 . 3 0 25 . 1 0
75 . 3 25 . 1 ) 0 ( 2
5 . 13 ) 0 (
= = =
= + =


+


+


= = '
cm C
z f
0
) 0 (
=
= =
z
dz
dT
C k z q
A temperature gradient can be measured down into the soil as shown in the figure below.
The heat flux at the soil-air interface can be computed with Fouriers Law:
q = heat flux
k = coefficient of thermal diffusivity in soil (3.5x10
-7
m
2
/s)
= soil density( 1800 kg/m
3
)
C = soil specific heat( 840 J/kg . C
o

*Positive flux value means heat is transferred from the air to the soil

Calculate dT/dz (z=0) and determine the heat flux.
Example
Tutorial - 5
The velocity v (m/s) of air flowing past a flat
surface is measured at several distances y (m) away
from the surface. Determine the shear stress,
(N/m2) at the surface (y=0). Assume a value of
dynamic viscosity =1.8 x 10
-5

dv
dy
t =
y m

0

0.002

0.006

0.012

0.018

0.024

v m/s

0

0.287

0.899

1.915

3.048

4.299

Tutorial - 6
For a certain chemical reaction AB time vs
concentration data is given.
Find out values of k& n
-r
A
= -dC
A
/dt =k (C
A
)
n
t

10

20

30

40

50

60

C

3.52

2.48

1.75

1.23

0.87

0.61

Solution
% numerical integration
clc;
clear all;
close all;

t=[10 20 30 40 50 60];
C=[3.52 2.48 1.75 1.23 0.87 0.61];

difft=diff(t);
diffC=diff(C);

dCdt=diffC./difft;

% plot(log(C(2:end)),log(-dCdt),'LineWidth',2);
% figure;plot((C(2:end)),(dCdt),'LineWidth',2);

a=polyfit(log(C(1:end-1)),log(-dCdt),1);
n=ceil(a(1))
k=a(2);

b=polyfit((C(1:end-1)),(-dCdt),0);
n=b
n = 1
k = 0.0296

You might also like