Finite Difference Method For PDE
Finite Difference Method For PDE
for PDE
Y V S S Sanyasiraju
Professor, Department of Mathematics
IIT Madras, Chennai 36
1
Classification of the Partial Differential Equations
LT a b c s (4.1.1)
t x x x
where , s 0, T , d , d
3
Classification
• Differential equation (4.1.2) is called
2 2 2
A( x, y ) 2 B( x, y ) C ( x, y ) 2 D E F 0 (4.13)
x xy y x y
2 2
Poisson’s equation 2 f ( x, y )
x 2
y
2 2
2
2
Unsteady diffusion equation K 2 , respectively.
t x
6
Classification of Non – Stationary Equations
8
Initial and Boundary Conditions
• And the boundary conditions, on ∂Ω, (0 < t < T), which can be
any one of the following:
1. Dirichlet t, x f t, x
2. Neumann K t, x f t, x (4.1.4b)
n
3. Robin K t , x at , x f t , x
n 9
• Indirectly boundary conditions play a very important role in the
classification of the PDE.
14
• The stationary form of the convection-diffusion equation is
given by
2
u s (4.1.14)
x x x
• The convection – diffusion equation which is also known as
Burger’s equation is similar to some of the governing equations
of the fluid dynamics but without any pressure term.
15
Non Dimensionlization
16
• Using the non–dimensional variables (4.1.15), equation (4.1.14)
can be written as (after dropping the symbol )׳
1 2
u s (4.1.16)
Pe x x x
VL
where
is
Pe the Peclet number which gives the ratio of the
convection and diffusion coefficients.
18
• For regular domains placing the nodal points uniformly and
connecting them by straight lines gives the required grid.
19
Fig. 4.2.1 Discretization of an interval
• In this discretization, a set of uniformly distributed points x0,
x1,….xn are identified such that x0 = a, xn = b and xi – xi-1 = ∆ x
for i = 1,2,…n. where, ∆x is the step length.
20
• If Ω is a rectangular two-dimension domain bounded by [a,b] x
[c,d], then it can be discretized as:
21
• Further, the dependent variable u at any point P is represented
using ui,j = u(xi, yj).
22
• Let the function u be sufficiently smooth (differentiable) and it
has values ui and ui + 1 at any two neighboring points, i and i + 1
respectively.
x
• This error is called Truncation error, in which ui is the
2!
leading term.
• Since the degree of the step size is one in the leading term of the
error, (4.2.4) is a first order approximation for u׳.
24
• The order of approximation is an important concept in the
process of discretization which gives an immediate insight about
what kind of accuracy can be expected from the scheme.
' ui ui 1 x x 2 1
ui
ui
ui ..... ui O ( x ) (4.2.6)
x 2! 3! x
25
• From (4.2.5) and (4.2.4) (subtracting (4.2.5) from (4.2.4)) one
can write
x 2
ui
1
x
ui 1 ui 1
6
ui
1 0
x
ui O (x 2 ) (4.2.8)
26
• Alternatively, adding (4.2.4) and (4.2.5) gives
x 2 x 4 iv
ui 1 ui 1 2ui ui ui .... (4.2.9)
2! 4!
ui 1 2ui ui 1 x 2 iv 1 2
ui u u O ( x 2
) (4.2.10)
x x
2 i 2 i
12
27
Numerical Implementation
1
1 1 2 2 u 1
2 i 1
u ui 1 sin log( xi )
x xi (x) x 2 x 2
i
i
x 2 xi
x (4.2.15)
i 1, 2,... n 1
30
• After solving Eq. (4.2.15) with step lengths (∆x) 1/40, 1/80 and
1/160, the absolute errors in the numerical solution are computed
and compared in the Fig. 4.2.3
x x
ui n ui
• Let Dui , D ui n , Eui ui 1
n
be the first and nth order
x x
derivative and shifting operators.
1 1
Ee xD
, D log( E ) D log(1 ) (4.3.2)
x x
32
• Expanding the log function gives
1 ( ) 2 ( )3 ( ) 4
Dui ... ui (4.3.3)
x 2 3 4
1 3 5
Dui ... ui (4.3.5)
x 24 640
33
• Similarly, using the same procedure, the formulae for second
derivative can be obtained as
1 2 11 4 5 5
3
...
x 2 12 6
1 11 5 (4.3.6)
D 2ui 2 2 3 4 5 ...
x 12 6
1 2 1 4 1 6
2
...
x 12 90
1 2 1 2 1 2 1 2
2 x 1 x 2 y 1 y ui , j f i , j
x 12 y 12
• Since
2u 1 2 1 2
2 x 1 x ui , j and
x 2
x 12
2u 1 2 1 2
2 y 1 y ui , j
y 2
y 12
36
• For the sake of simplicity assume, x = y (the following
procedure is also valid without this assumption).
2 1 2 2 1 2
x 1
x y 1 y i, j
u x 2
fi , j
12 12
1 1
1 2 1 2
• Multiplying with 1 x 1 y
gives (after using the
12 12
commutative nature of the finite difference operators)
2 1 2
1
2 1 2
1
x 1 y y 1 x ui , j
12 12
1 1
2 1 2 1 2
x 1 x 1 y fi , j
12 12
37
1 1
1 2 1 2
• Expanding the terms 1 x , 1 y on the left hand side
12 12
using
1 2
1 2 1 2 1 2
1 x 1 x x ...
12 12 12
1 2
1 2 1 2 1 2
1 y 1 y y ...
12 12 12
• and neglecting the terms which are having the order more than
two gives
2 1 2 2 1 2 2 1 2 1 2
x 1 y y 1 x i, j
u x 1 x 1 y fi, j
12 12 12 12
38
2 2 2 2 2 1 2 2
x
2
y
12
x y i, j
u x 1
12
1 2
x 2
y
144
x y fi, j
2 1 2 2 2 2
x y x y ui , j x 1
2
6 12
1 2
x y fi , j
or
12 x2 12 y2 2 x2 y2 ui , j x 2 12 x2 y2 f i , j
39
• Expanding the central difference operator in the above scheme
gives
8 ui1, j ui1, j ui, j 1 ui, j 1 2 ui1, j 1 ui1, j 1 ui1, j 1 ui1, j 1
(4.3.7)
40ui, j x2 8 fi, j fi1, j fi1, j fi, j 1 fi, j 1
40
Higher Order By Computing the Coefficients of a Stencil
u 2u
A B 2 a0ui a1ui 1 a2ui 1 a3ui 2 a4ui 2 (4.3.8)
x x
41
• Expanding the terms on the right hand side using Taylor series
and simplifying gives
u 2u ui , j
A B (a0 a1 a2 a3 a4 )ui , j (a1 a2 2a3 2a4 )x
x x 2
x
x2 ui , j x3 ui , j
2 3
x4 ui , j x5 ui , j
4 5
42
Finite Difference Schemes for Elliptic Equations
2u 2u
f ( x , y ), ( x , y ) 2
(4.4.1)
x 2 y 2
• Then at each grid point of the domain, discretize the given Eq.
(4.4.1) by replacing the partial derivatives of the equation with
second order finite difference approximations (4.2.10) to get
43
ui 1, j 2ui, j ui 1, j 2 ui, j 1 2ui, j ui, j 1
O ( x ) O ( y 2 ) f i , j
x 2 y 2 (4.4.2)
for i 1, 2,..., n x 1, j 1, 2,..., n y 1
44
• The derivative terms in Eq. (4.4.3) are also approximated using
the first order accurate forward or backward difference
approximations at every grid point of the boundary, that is, at i =
0 or nx or j = 0 or ny.
45
• Finally, the algebraically closed system is solved using any
linear solver
• Further, the iterative methods like Jacobi, Gauss Siedel are too
slow in their convergence, therefore, alternatively one can apply
the ADI (Alternate Direction Implicit) method which is based on
line-Gauss Seidel solver.
47
Implementation of the ADI method
• In the first step, on each vertical line , the tri diagonal system
1 k 1 1 1 k 1 1 k 1 1
ui 1, j 2 ui, j ui 1, j fi , j uik, j 1 uik, j 1 y
x 2 2 y2 x 2 y2
x
for i 1, 2,..., n x 1
1 k 1 1 1 k 1 1 k 1 1
ui , j 1 2 ui , j ui , j 1 fi , j uik1, j uik1, j
y 2 x 2 y 2 y 2 x 2
for j 1, 2,..., n y 1
is solved (nx-1 times) once again using Thomas algorithm
49
Figure 4.4.2 Domain and boundary conditions
Solution:
50
• The discretization of the given Laplace equation using second
order central difference approximations is given by
ui 1, j 2ui , j ui 1, j ui , j 1 2ui , j ui , j 1
0 ( where i 1, 2,3 & j 1, 2) (4.4.6)
x 2
y 2
• Solving the six equations in the (4.4.7), using any direct method,
gives
u1,1 1.1719 u1,2
u2,1 4.9805 u2,2
u3,1 19.9954 u3,2
51
Note:
1. If Gauss-Seidel iterative method with stopping criteria
( k +1) -5
u -u (k )
£ 10
¥
53
Laplace Equation in Circular Geometries
2u 1 u 1 2u
2 0 0 r ra , 0 2 (4.4.8)
r 2
r r r 2
54
• If Dirichlet conditions are given at r = 0 and ra, and periodic
conditions are used in angular direction then the second order
central difference approximation of (4.38) gives
1
dr 2 ui 1, j 2ui, j ui 1, j
1
ri dr
ui 1, j ui 1, j
1
ri d
2 2 i , j 1
u 2ui, j ui, j1 0 (4.4.9)
55
• In the absence of any boundary condition at r = 0, discretization
of Eq. (4.4.8) in the conventional way leads to one over zero in
second and third terms of Eq. (4.4.9). In such cases, u at r = 0 is
obtained using
1 n
u0 ui (4.4.10)
n 1 i 0 r ra
56
Difference Schemes for Parabolic Equations
One-dimensional problems:
u 2u
k 2 , x (a, b), t 0 (4.5.1)
t x
57
Forward time central space (FTCS) scheme
• A simple and easiest scheme to compute the numerical solution
of (4.5.1) is the FTCS (forward time and central space) scheme
which is an explicit method.
1 n 1 n
t
1
ui ui k 2 uin1 2uin uin1
x
(4.5.2)
t
uin 1 uin r uin1 2uin uin1 , r k
x 2
uin 1 ruin1 (1 2r )uin ruin1 i 1, 2, , nx 1, n 0,1,
where, the superscript ‘n’ represents the time level. The
discretization and the stencil of the FTCS Eq. (4.5.2) is shown in
the Fig. 4.5.1. 58
Fig. 4.5.1 Discretization and the stencil of FTCS scheme
• In the Fig. 4.5.1, there is only one stencil point at n+1th time
level which is the unknown and three points at nth time level
which anyway are known.
t
1 n n1
1
ui ui k 2 uin1 2uin uin1
x
(4.5.3)
t
ruin1 (1 2r )uin ruin1 uin1 , r k , i 1, 2,, nx 1, n 1, 2,
x2
60
• This is done by grouping all the discretized equations at a
particular time level and solving them in one step using say,
Thomas algorithm if the resultant system is a tri-diagonal one.
61
• The computational stencil for the BTCS scheme is as shown in
Fig. 4.5.2.
Fig
• At each time step, the scheme Eq. (4.5.3) also can be written as
t (4.5.4)
ruin11 (1 2r )uin 1 ruin11 uin , r k , i 1, 2, , nx 1, n 0,1,
x 2
62
Weighted average scheme
• Weighted average of Eqs. (4.5.1) and (4.5.3), gives
1 n 1 n
t
k
ui ui 2 (1 ) uin1 2uin uin1 uin11 2uin 1 uin11
x
(4.5.4)
r uin11 (1 2 r )uin 1 r uin11 (1 )r uin1 (1 2(1 )r )uin (1 )r uin1
t
rk , 0 1, i 1, 2, , nx , n 0,1,
x 2
65
FTCS BTCS Crank-Nicolson
X Analytical
Solution Error Solution Error Solution Error
Table 4.5.1 Comparison of the analytical and numerical solutions and their errors
66
Two-dimensional problems
• Consider the unsteady diffusion over a flat plate governed by the
differential equation
u u u 2 2
(4.5.7)
k , ( x, y ) (a, b) X (c, d ), t 0
2
t x y 2
Explicit scheme
• Approximating the time derivative with forward difference and
space derivatives with central differences gives a scheme
1
1
t
u u k
n 1
i, j
x
n
u 2u u
i, j
1
y
2
u 2u u
n
i 1, j
n
i, j
n
i 1, j 2
n
i , j 1
n
i, j
n
i , j 1
uin, j 1 uin, j r1 uin1, j 2uin, j uin1, j r2 uin, j 1 2uin, j uin, j 1
t t
(4.5.8)
r1 k 2 , r2 k 2
x y
uin, j 1 r2uin, j 1 ru
1 i 1, j (1 2 r1 2 r2 )ui , j ru
n n
1 i 1, j r2 ui , j 1
n n
67
i 1, 2, , n x 1, j 1, 2, , n y 1, n 0,1,
• Here, ∆y is the step length in y direction. Taylor series
expansion of Eq. (4.5.8) shows that the explicit scheme is first
order accurate in time and second order in space (both in x and y
directions).
• In the second step, using the solution at the n+1/2 time level over
the i is constant lines and using second tri-diagonal part of Eq.
(4.5.9), the solution is marched to the n+1 time level.
• Therefore, for each time level, Eq. (4.5.9) gives (nx-1 + ny-1) tri-
diagonal systems in x and y directions which are mush easier to
solve using Thomas algorithm than the penta-diagonal system
(of size L X L, where L = (nx-1) * (ny-1)) appears in the
weighted average or Crank-Nicolson schemes for two
dimensional problems.
69
Convergence
• For parabolic and also for all time dependent problems, the
convergence of the numerical solution to the corresponding
analytical solution is carried out through the testing for
consistency and stability since, according to Lax equivalence
theorem,
70
Consistency of a Numerical Scheme
• If the truncation error tends to zero as the step lengths tend zero
then the numerical scheme is said to be consistent.
71
Numerical Illustration
given by
u 2u 1 ui t ui k x ui k t x ui
2 n 2 3 n 2 4 n 2 2 4 n
k 2 t 2
t x 2 t 2 t 3 12 x 4 2 t 2x 2
72
Stability of a Numerical Scheme
73
FTCS Crank-Nicolson
X Analytical
Solution Error Solution Error
FTCS Crank-Nicolson
X Analytical
Solution Error Solution Error
74
• Its clear from the Tables 4.5.2 and 4.5.3 that, with r = 0.45, the FTCS
scheme produces solutions with errors less than .2% and CN scheme
with errors less than 1.3%.
• This is due to the instable nature of the FTCS scheme when the value
of r is greater than 0.5. Mathematically this can be understood using
the following analysis:
75
• Let £(u) = 0 be a linear difference scheme and uin is its numerical
solution, U in is the exact solution and Ein is its error at nth time level
at the nodal point xi then we have
uin U in Ein (4.5.10)
and
£( ui ) = £( U in Ein ) = £( U in ) + £( Ei ) = £( Ei ) = 0
n n n
(4.5.11)
• That is, the error also satisfies the same difference equation
which numerical solution satisfies.
76
• Further, if the numerical solution is assumed to be periodic,
achieved by reflecting the solution in the region (0, L) in to (-L,
L) and expressible in terms of finite Fourier series (since the
domain is of finite length which is discretized with finite step
length) then it can be written as
N N N
ui K j e
n n Ik x
j i
Kje
n Ik ix
j
K nj e Ii (4.5.12)
j N j N j N
where
N is the number of points in the discretization, (N=L/∆x)
I 1
K nj is the amplitude of the jth harmonic,
kj is the wave number
is the phase angle given by kj ∆x.
77
Note: kj varies from –N to N instead of - to , because the
maximum and minimum resolvable wavelengths (λ) are only 2L
and 2∆x, respectively and the maximum wavelength 2L is
discretized with 2N+1 points, that is from –N to N.
n Ii
Substituting u K e in Eq.
n
i (4.5.15) and simplifying for
amplification factor G, gives
K n 1 I
(1 ) re (1 2(1 ) r ) (1 ) re I 1 4(1 ) r sin 2
G 2
Kn re I (1 2r ) re I 1 4r sin 2
(4.5.16)
2
• For θ = 0, that is, for FTCS scheme, |G|<1 implies 1 4 r sin 2
2
1
which is true whenever the value of r < 1/2.
79
• Therefore, FTCS scheme is only conditionally stable. However,
for θ greater or equal to ½, Eq. (4.5.16) is unconditionally stable.
• Once again looking at the Tables 4.5.2 and 4.5.3, it is clear that
for r = 0.45, FTCS scheme is able to produce accurate solutions
but the round of errors are dominated when the value of r is
raised to 0.55 because FTCS scheme is not stable at r = 0.55.
• On the other hand due its stable nature of CN scheme for all
values of r, the round of errors are under control at both r = 0.45
and 0.55.
80
Difference schemes for hyperbolic equations
81
Explicit Scheme
• Discretizing the second derivatives in Eq. (4.6.1) with central
differences gives
i 1, 2,3,, nx 1, n 1, 2,3,
• For n=1, Eq. (4.6.5) requires the solution at t = -∆t, which can be
obtained, using the derivative boundary condition at t=0 Eq.
(4.6.2). 82
• Discretizing the initial condition: At n=1, Discretising the
derivative initial condition with central difference gives
ui1 ui1
O ( t 2 ) g ( xi ) ui1 ui1 2 tgi O ( t 2 ) (4.6.6)
2t
83
• Alternatively, using Taylor series at n=1, one can write
t 2 t 3
u ( xi , t1 ) u ( xi , 0 t ) u ( xi , 0) tut ( xi , 0) utt ( xi , 0) uttt ( xi , 0)
2! 3! (4.6.7)
t 2 f i 1 2 f i f i 1
f i tgi O ( x 2
, t 3
) using(4.6.2)
2! x 2
84
Consistency and Stability of Eq. (4.6.7)
85
• For verifying stability, uin K n e Ii in Eq. (4.6.5) and simplify to get
K K1 2eI (1) 2(1 2 ) 2KneI (1) K1 2 2cos 2(1 2 )
2 2
K 2(1 2sin ) 2(1 2 ) K 1 0
2
2 (4.6.9)
2 2
K 21 2 sin K 1 0 K 2AK 1 0 with A 1 2 sin
2 2 2 2
2 2
• Computing K from Eq. (4.6.9) gives,
86
• Equation (4.6.10) guarantees |K|=1 which is better than |K|>1
because in the former, at least the error only grows in accordance
with the solution instead of blowing up in the later situation.
However, Eq. (4.6.10) is valid only when
A2 1 1 A 1 1 1 22 sin 2 1 2 sin 2 1 (4.6.11)
2 2
87
CFL ( Courant – Friedrichs – Lewy ) Condition
88
• Therefore, any numerical scheme which is developed to solve
Eq. (4.6.3) should also use all the data from this region.
• Since, Eq. (4.6.13) uses f and g in the region (xi-atn, xi+atn) on the
x-axis to compute the solution at any given point (xi, tn),
therefore, according to CFL condition (the numerical domain of
dependence should bound the analytical domain of dependence)
that is
c t
xi n xi ctn xi nx xi cnt x ct 1 (4.6.14)
x
89
• Condition Eq. (4.6.14) is equivalent to the stability condition σ ≤
1 for the explicit scheme Eq. (4.6.5).
• Further, if c=1 and the step lengths ∆t=∆x, then from Eq. (4.6.8),
the scheme Eq. (4.6.5) is fourth order accurate.
• Therefore, if the step lengths are fixed such that σ = 1, then the
explicit scheme Eq. (4.6.5) gives the best solution (in this
particular case, that is, for c=1 and ∆t=∆x, the entire right hand
side of Eq. (4.6.8) becomes zero and Eq. (4.6.5) produces exact
solution).
90
Numerical Illustration
91
• Fixing ∆x = 0.1 and ∆t = 0.075 (so that σ = 0.75), scheme Eq.
(4.6.5), after 10 time steps (that is at time t = .75), produces the a
numerical solution which has been compared with the analytical
solution in the Table 4.6.1,
92
x=0.1 x=0.2 x=0.3 x=0.4 x=0.5
93