1D FDM
1D FDM
METHOD
Basic Theory
Taylors Theorem
If a function f and its first n+1 derivatives are continuous in an
interval containing the points xi and xi+1, then the value of the
function f at the point xi+1 is given by:
f ( xi 1 ) f ( xi ) f '( xi ) ( xi 1 xi )
f ''( xi )
f '''( xi )
( xi 1 xi ) 2
( xi 1 xi )3
2!
3!
f ( n ) ( xi )
( xi 1 xi ) n Rn
n!
...
xi 1
xi
( xi 1 t ) n ( n 1)
f
(t ) dt
n!
where
f ( n 1) ( )
( xi 1 xi ) n 1
(n 1)!
xi xi 1 .
f ' ' ( xi )
( x i 1 x i ) 2
2!
f ''( xi )
f '''( xi )
f ( n ) ( xi )
h3
... h n
Rn
n!
2!
3!
f ( n 1) ( )
(n 1)!
Finite Differences
Simple difference formulas can be created by truncating the
Taylor series after the first derivative term. Examples are:
Forward Difference:
f ( xi 1 ) f ( xi ) h f '( xi ) O (h 2 )
f '( xi )
f ( xi 1 ) f ( xi )
O ( h)
h
Backward Difference:
f ( xi 1 ) f ( xi ) h f '( xi ) O ( h 2 )
f '( xi )
f ( xi ) f ( xi 1 )
O ( h)
h
f ''( xi )
O ( h3 )
2!
f ( xi 2 ) f ( xi ) 2h f '( xi ) (2h) 2
f ''( xi )
O(h3 )
2!
(1)
f ( xi 2 ) f ( xi ) 2h f '( xi ) 2h 2 f ''( xi ) O ( h3 )
(2)
f ( xi 2 ) 4 f ( xi 1 ) 3 f ( xi )
O(h 2 )
2h
3 f ( xi ) 4 f ( xi 1 ) f ( xi 2 )
O(h 2 )
2h
f ( xi 1 ) f ( xi ) h f '( xi ) h 2
f ''( xi )
O ( h3 )
2!
f ( xi 1 ) f ( xi ) h f '( xi ) h 2
f ''( xi )
O ( h3 )
2!
f ( xi 1 ) f ( xi 1 )
O(h 2 )
2h
difference
f ( xi 1 ) 2 f ( xi ) f ( xi 1 )
O(h 2 )
h2
Graphical Interpretation
Given a function y = u(x), the derivative du/dx at a point x = xi is
given by:
u ( xi h) u ( xi )
du
lim h0
h
dx x xi
This represents the slope of the tangent to the curve at the point
x = xi.
T
Q
f (a + h)
f (a)
y = f (x)
P
a+h
h
dx x xi
h
dx x xi
2h
dx x xi
Example 1
Estimate the first derivative of the function:
f ( x) 0.1x 4 0.15 x 3 0.5 x 2 0.25 x 12
f ( xi 1 ) f ( xi )
xi 1 xi
% error t
xi = 0.5, thus
f (1.0) f (0.5)
1.45
0.5
58.9%
True
0.9125
f ( xi ) f ( xi 1 )
xi xi 1
% error t
f (0.5) f (0.0)
0.55
0.5
0.9125 (0.55)
39.7%
0.9125
f ( xi 1 ) f ( xi 1 )
xi 1 xi 1
f (1.0) f (0.0)
1.0
1.0
% error t
0.9125 ( 1.0)
9.6%
0.9125
f (0.75) f (0.5)
1.155
0.25
% error t 26.5%
Backward difference:
f '(0.5)
f (0.5) f (0.25)
0.714
0.25
% error t 21.7%
Central difference:
f '(0.5)
f (0.75) f (0.25)
0.934
0.5
% error t 2.4%
Example 2
Use high accuracy formulas to estimate the first derivative of the
function:
f ( x) 0.1x 4 0.15 x 3 0.5 x 2 0.25 x 12
f ( xi 2 ) 4 f ( xi 1 ) 3 f ( xi )
2h
t 5.82%
3 f ( xi ) 4 f ( xi 1 ) f ( xi 2 )
2h
f ( xi 2 ) 8 f ( xi 1 ) 8 f ( xi 1 ) f ( xi 2 )
12h
Here xi = 0.5, xi+1 = 0.75, xi+2 = 1.0, xi-1 = 0.25, xi-2 = 0.0 and h =
0.25, thus
f '(0.5)
t 0
at
x0
f ( x) 0
at
x 1
f ( xi 1 ) 2 f ( xi ) f ( xi 1 )
h2
2 f ( xi ) f ( xi 1 ) f ( xi 1 ) h 2 xi
In this case, xi = 0.5, xi+1 = 1.0, xi-1 = 0.0 and h = 0.5, thus
0.5
0.52 0.5
0.0714
0.52 2
2 f ( xi ) f ( xi 1 ) f ( xi 1 ) h 2 xi
0.25
(1)
0.25
(2)
0.25
(3)
Substituting equations (1) and (3) above into equation (2), the
value of f(0.5) can be calculated as follows:
0.252 2 f (0.5)
0.252 0.5
2
2
0.25 2
0.25 2
f (0.5) 0.0701
with an error
% error t 0.6%
f (0.75) 0.0604
Exact solution:
f ( x)
sin x
x
sin1
Example 4
Given the problem of mass balance within a chemical reactor,
described by the following advection-dispersion equation with
first-order chemical reaction:
D
d 2c
dc
U
kc 0
2
dx
dx
where:
c: concentration, moles/m3
U: velocity of the water flowing through the reactor, m/h
D: dispersion coefficient, m2/h
k: first-order decay coefficient, h-1
and the boundary conditions
c 1
at
x0
c0
at
xL
2
h2
dx x xi
c( xi 1 ) c( xi 1 )
dc
2h
dx x xi
the
c( xi 1 ) 2c ( xi ) c( xi 1 )
c( xi 1 ) c( xi 1 )
U
kc( xi ) 0
2
h
2h
D U
2 c( xi 1 ) 2 k c( xi ) 2 c( xi 1 ) 0
2h
h 2h
h
3.2 2 1.2
2
2
h 2h 0.25 2 0.25
2D
2 0.2
k
0.5 6.4 0.5 6.9
2
h
0.252
D U
0.2
1
3.2 2 5.2
2
2
h
2h 0.25 2 0.25
Thus:
1.2c (0.5) 6.9c (0.25) 5.2c(0.0) 0
c(0.25)
5.2
0.753
6.9
12.8 4 8.8
2
2
h 2h 0.125 2 0.125
2D
2 0.2
k
0.5 25.6 0.5 26.1
2
h
0.1252
D U
0.2
1
12.8 4 16.8
2
2
h
2h 0.125 2 0.125
General equation:
8.8c( xi 1 ) 26.1c ( xi ) 16.8c( xi 1 ) 0
8.8c(0.25) 16.8
26.1
16.8c (0.25)
26.1
Therefore:
8.8
16.8c (0.25)
8.8c (0.25) 16.8
26.1c (0.25) 16.8
0
26.1
26.1
f '( xi )
f ( xi 1 ) f ( xi )
O ( h)
h
f ( xi 2 ) 4 f ( xi 1 ) 3 f ( xi )
O(h 2 )
2h
Second Derivative
f ''( xi )
f ''( xi )
f ( xi 2 ) 2 f ( xi 1 ) f ( xi )
O ( h)
h2
f ( xi 3 ) 4 f ( xi 2 ) 5 f ( xi 1 ) 2 f ( xi )
O(h 2 )
2
h
Third Derivative
f '''( xi )
f '''( xi )
f ( xi 3 ) 3 f ( xi 2 ) 3 f ( xi 1 ) f ( xi )
O ( h)
h3
3 f ( xi 4 ) 14 f ( xi 3 ) 24 f ( xi 2 ) 18 f ( xi 1 ) 5 f ( xi )
O(h2 )
3
2h
Fourth Derivative
f ''''( xi )
f '''( xi )
f ( xi 4 ) 4 f ( xi 3 ) 6 f ( xi 2 ) 4 f ( xi 1 ) f ( xi )
O ( h)
h4
2 f ( xi 5 ) 11 f ( xi 4 ) 24 f ( xi 3 ) 26 f ( xi 2 ) 14 f ( xi 1 ) 3 f ( xi )
O(h 2 )
4
h
f '( xi )
f ( xi ) f ( xi 1 )
O ( h)
h
3 f ( xi ) 4 f ( xi 1 ) f ( xi 2 )
O(h 2 )
2h
Second Derivative
f ''( xi )
f ''( xi )
f ( xi ) 2 f ( xi 1 ) f ( xi 2 )
O (h)
h2
2 f ( xi ) 5 f ( xi 1 ) 4 f ( xi 2 ) f ( xi 3 )
O(h 2 )
2
h
Third Derivative
f '''( xi )
f '''( xi )
f ( xi ) 3 f ( xi 1 ) 3 f ( xi 2 ) f ( xi 3 )
O ( h)
h3
5 f ( xi ) 18 f ( xi 1 ) 24 f ( xi 2 ) 14 f ( xi 3 ) 3 f ( xi 4 )
O(h 2 )
3
2h
Fourth Derivative
f ''''( xi )
f '''( xi )
f ( xi ) 4 f ( xi 1 ) 6 f ( xi 2 ) 4 f ( xi 3 ) f ( xi 4 )
O ( h)
h4
3 f ( xi ) 14 f ( xi 1 ) 26 f ( xi 2 ) 24 f ( xi 3 ) 11 f ( xi 4 ) 2 f ( xi 5 )
O (h 2 )
4
h
f '( xi )
f ( xi 1 ) f ( xi 1 )
O(h 2 )
2h
f ( xi 2 ) 8 f ( xi 1 ) 8 f ( xi 1 ) f ( xi 2 )
O(h 4 )
12h
Second Derivative
f ''( xi )
f ''( xi )
f ( xi 1 ) 2 f ( xi ) f ( xi 1 )
O(h 2 )
2
h
f ( xi 2 ) 16 f ( xi 1 ) 30 f ( xi ) 16 f ( xi 1 ) f ( xi 2 )
O(h 4 )
2
12h
Third Derivative
f '''( xi )
f '''( xi )
f ( xi 2 ) 2 f ( xi 1 ) 2 f ( xi 1 ) f ( xi 2 )
O(h 2 )
3
2h
f ( xi 3 ) 8 f ( xi 2 ) 13 f ( xi 1 ) 13 f ( xi 1 ) 8 f ( xi 2 ) f ( xi 3 )
O(h4 )
3
8h
Fourth Derivative
f ''''( xi )
f '''( xi )
f ( xi 2 ) 4 f ( xi 1 ) 6 f ( xi ) 4 f ( xi 1 ) f ( xi 2 )
O (h 2 )
4
h
f ( xi 3 ) 12 f ( xi 2 ) 39 f ( xi 1 ) 56 f ( xi ) 39 f ( xi 1 ) 12 f ( xi 2 ) f ( xi 3 )
O(h 4 )
6h 4