You are on page 1of 20

Finite difference method

Principle: derivatives in the partial differential equation are approximated


by linear combinations of function values at the grid points

1D: = (0, X), ui u(xi ), i = 0, 1, . . . , N


X
grid points xi = ix mesh size x = N

0 X
x0 x1 xi 1 xi xi+1 xN 1 xN
First-order derivatives
u u(x + x) u(x) u(x) u(x x)
(x) = lim = lim
x x0 x x0 x
u(x + x) u(x x)
= lim (by definition)
x0 2x
Approximation of first-order derivatives

Geometric interpretation

u forward ba kward
entral u
 ui+1 ui
x i x forward difference

u
 ui ui1

x x exa t x i x backward difference

u
 ui+1 ui1
x i 2x central difference

xi 1 xi xi+1 x 
P (xxi )n nu
Taylor series expansion u(x) = n! xn i , u C ([0, X])
n=0
     
u (x)2 2 u (x)3 3 u
T1 : ui+1 = ui + x + 2
+ 3
+ ...
x
i 2 x i 6 x i
     
u (x)2 2 u (x)3 3 u
T2 : ui1 = ui x + + ...
x i 2 x2 i 6 x3 i
Analysis of truncation errors

Accuracy of finite difference approximations


     
u ui+1 ui x 2u (x)2 3u
T1 = + ...
x i x 2 x2 i 6 x3 i

forward difference truncation error O(x)


     
u ui ui1 x 2u (x)2 3u
T2 = + + ...
x i x 2 x2 i 6 x3 i

backward difference truncation error O(x)


   
u ui+1 ui1 (x)2 3u
T1 T2 = + ...
x i 2x 6 x3 i
central difference truncation error O(x)2

Leading truncation error

= m (x)m + m+1 (x)m+1 + . . . m (x)m


Approximation of second-order derivatives
Central difference scheme
 
2u ui+1 2ui + ui1 2
T1 + T2 = + O(x)
x2 i (x)2
Alternative derivation
 2     u
 u

u u x i+1/2 x i1/2
= = lim
x2 i x x i x0 x
ui+1 ui
x ui u
x
i1
ui+1 2ui + ui1
=
x (x)2

Variable coefficients f (x) = d(x) u


x diffusive flux

di+1/2 ui+1 di1/2 ui u


  ui
f fi+1/2 fi1/2 x x
i1

=
x i x x
di+1/2 ui+1 (di+1/2 + di1/2 )ui + di1/2 ui1
=
(x)2
Approximation of mixed derivatives
  
2u u u y
2D: xy = x y = y x

u u

2u

y y
i+1,j i1,j
= + O(x)2 yj +1
xy i,j 2x
yj

u ui+1,j+1 ui+1,j1
= + O(y)2 yj 1
y i+1,j 2y

u ui1,j+1 ui1,j1
= + O(y)2
y i1,j 2y xi 1 xi xi+1 x

Second-order difference approximation


 2 
u ui+1,j+1 ui+1,j1 ui1,j+1 + ui1,j1
= + O[(x)2 , (y)2 ]
xy i,j 4xy
One-sided finite differences
 
u u1 u0
= + O(x) forward difference
x 0 x
? x0 x1 x2
backward/central difference approximations
would need u1 which is not available
Polynomial fitting
     
u x2 2u x3 3u
u(x) = u0 + x + + + ...
x 0 2 x2 0 6 x3 0
 
u u
u(x) a + bx + cx2 , b + 2cx, b
x x 0

approximate u by a polynomial and differentiate it to obtain the derivatives

u0 = a cx2 = u1 u0 bx

u1 = a + bx + cx2 3u0 + 4u1 u2
b=
u2 = a + 2bx + 4cx2 2x
Analysis of the truncation error
 
u ui + ui+1 + ui+2
One-sided approximation
x i x
    
u (x)2 2 u (x)3 3 u
ui+1 = ui + x + + + ...
x i 2 x2 i 6 x3 i
  2
 2  3
 3 
u (2x) u (2x) u
ui+2 = ui + 2x + + + ...
x i 2 x2 i 6 x3 i

  
ui +ui+1 +ui+2 ++ u x 2u
x = x ui + ( + 2) x i + 2 ( + 4) x2 + O(x2 )
i

Second-order accurate if + + = 0, + 2 = 1, + 4 = 0
 3ui +4ui+1 ui+2
= 23 , = 2, = 21 u
x i = 2x + O(x2 )
Application to second-order derivatives
 
2u ui + ui+1 + ui+2
One-sided approximation
x2 i x2

    
u (x)2 2 u (x)3 3 u
ui+1 = ui + x + + + ...
x i 2 x2 i 6 x3 i
  2
 2  3
 3 
u (2x) u (2x) u
ui+2 = ui + 2x + + + ...
x i 2 x2 i 6 x3 i

  
ui +ui+1 +ui+2 ++ +2 u +4 2u
x2 = x2 ui + x x i + 2 x2 + O(x)
i

First-order accurate if + + = 0, + 2 = 0, + 4 = 2

u
 ui 2ui+1 +ui+2
= 1, = 2, =1 x i = x2 + O(x)
High-order approximations
 
u 2ui+1 + 3ui 6ui1 + ui2
= + O(x)3 backward difference
x i 6x
 
u ui+2 + 6ui+1 3ui 2ui1
= + O(x)3 forward difference
x i 6x
 
u ui+2 + 8ui+1 8ui1 + ui2
= + O(x)4 central difference
x i 12x
 
2u ui+2 + 16ui+1 30ui + 16ui1 ui2
= + O(x)4 central difference
x2 i 12(x) 2

Pros and cons of high-order difference schemes


more grid points, fill-in, considerable overhead cost
high resolution, reasonable accuracy on coarse grids

Criterion: total computational cost to achieve a prescribed accuracy


Example: 1D Poisson equation

Boundary value problem


2u
2 =f in = (0, 1), u(0) = u(1) = 0
x

One-dimensional mesh 0 1
x0 x1 xi 1 xi xi+1 xN 1 xN

1
ui u(xi ), fi = f (xi ) xi = ix, x = , i = 0, 1, . . . , N
N
Central difference approximation O(x)2

ui1 2ui +u i+1
= fi , i = 1, . . . , N 1
(x)2
u0 = uN = 0 Dirichlet boundary conditions

Result: the original PDE is replaced by a linear system for nodal values
Example: 1D Poisson equation

Linear system for the central difference scheme



u0 2u1 +u2


i = 1 (x)2 = f1



i=2

u1 2u 2 +u3
(x)2 = f2
i=3 u2 2u
(x)
3 +u4
2 = f3





...


uN 2 2uN 1 +uN
i=N 1 (x)2 = fN 1

Matrix form Au = F A RN 1N 1 u, F RN 1

2 1 u1 f1
1 1 2 1 u2 f2

A= 1 2 1 , u= u3 , F = f3
(x)2 ...
1 2 uN 1 fN 1

The matrix A is tridiagonal and symmetric positive definite invertible.


Other types of boundary conditions
u
Dirichlet-Neumann BC u(0) = x (1) =0
uN +1 uN 1
u0 = 0, =0 uN +1 = uN 1 central difference
2x

Extra equation for the last node


uN 1 2uN + uN +1 uN 1 + uN 1
= fN = fN
(x)2 (x)2 2

Extended linear system Au = F A RN N u, F RN



2 1 u1 f1
1 2 1 u2 f2
1 1 2 1



u3



f3


A= , u= , F =
(x)2 ...
1 2 1 u
N 1
f
N 1

1
1 1 uN 2 fN

The matrix A remains tridiagonal and symmetric positive definite.


Other types of boundary conditions

Non-homogeneous Dirichlet BC u(0) = g0 only F changes


2u1 u2 g0
u0 = g0 = f1 + first equation
(x)2 (x)2

u
Non-homogeneous Neumann BC x (1) = g1 only F changes
uN +1 uN 1
= g1 uN +1 = uN 1 + 2xg1
2x
uN 1 2uN + uN +1 uN 1 + uN 1 g1
= fN = fN +
(x)2 (x)2 2 x
u
Non-homogeneous Robin BC x (1) + u(1) = g2 A and F change
uN +1 uN 1
+ uN = g2 uN +1 = uN 1 2xuN + 2xg2
2x
uN 1 2uN + uN +1 uN 1 + (1 + x)uN 1 g2
= fN = fN +
(x)2 (x)2 2 x
Example: 2D Poisson equation
y h h ve-point sten il
Boundary value problem 1

2 u2 2 u2 = f in = (0, 1) (0, 1) yj +1
x y
h
yj
u=0 on = h
yj 1

1
Uniform mesh: x = y = h, N= h xi 1 xi xi+1 x
0 1

ui,j u(xi , yj ), fi,j = f (xi , yj ), (xi , yj ) = (ih, jh), i, j = 0, 1, . . . , N

Central difference approximation O(h2 )



ui1,j +ui,j1 4ui,j +ui+1,j +ui,j+1
= fi,j , i, j = 1, . . . , N 1
h2
ui,0 = ui,N = u0,j = uN,j = 0 i, j = 0, 1, . . . , N
Example: 2D Poisson equation
2
(N 1)2 2
Linear system Au = F A R(N 1) u, F R(N 1)

row-by-row u = [u1,1 . . . uN 1,1 u1,2 . . . uN 1,2 u1,3 . . . uN 1,N 1 ]T


node numbering F = [f1,1 . . . fN 1,1 f1,2 . . . fN 1,2 f1,3 . . . fN 1,N 1 ]T

B I 4 1
I B I 1 4 1

A= ... ... , B= ... ...
I B I 1 4 1
I B 1 4

1
The matrix A is sparse, block-tridiagonal
1 (for the above numbering) and SPD.

I=
1 |max |
cond2 (A) = = O(h2 )
1 |min |
Caution: convergence of iterative solvers deteriorates as the mesh is refined
Treatment of complex geometries

2D Poisson equation 3

2 u2 2 u2 = f in
x y
4 R 0 Q 2
u = g0 on Q
h
P 1
Difference equation
stencil of Q
u1 + u2 4u0 + u3 + u4
= f0
h2 curvilinear boundary

Linear interpolation
u4 (h ) + u0 h
u(R) = = g0 (R) u4 = u0 + g0 (R)
h h h
 
h
Substitution yields u1 + u2 4 + h u0 + u3 = h2 f0 + g0 (R) h

Neumann and Robin BC are even more difficult to implement


Grid transformations

Purpose: to provide a simple treatment of curvilinear boundaries

d nozzle dire t mapping d re tangle


P
P

inverse mapping

y a b  a b
physi al domain omputational domain
body- tted grid artesian grid
x 

The original PDE must be rewritten in terms of (, ) instead of (x, y) and


discretized in the computational domain rather than the physical one.

u u u u
Derivative transformations , ,... , ,...
x y
| {z } | {z }
difficult to compute easy to compute
PDE transformations for a direct mapping
Direct mapping = (x, y), = (x, y)

Chain rule u u u u u u
= + , = +
x x x y y y
 2  2
2u u 2 u 2 2 u 2 u 2 u
= + +2 + 2 + 2
x2 x2 x2 x x x x
 2  2
2u u 2 u 2 2 u 2 u 2 u
= + +2 + 2 + 2
y 2 y 2 y 2 y y y y

Example: 2D Poisson equation u = f turns into


"   2 # "   2 #  
2 2 2 2
u u 2 u
2 + 2 + 2 +
x y x y x x y y
 2 2
  2 2

u u transformed equations
+ + = f
x2 y 2 x2 y 2 contain many more terms
The metrics need to be determined (approximated by finite differences)
PDE transformations for an inverse mapping

Inverse mapping x = x(, ) y = y(, )

x x y y
Metrics transformations , , , , , ,
x y x y
| {z } | {z }
unknown known
Chain rule

u u x u y u x y u
= x + y x
=
y

u u x u y u x u
= x + y y
| {z }
J
(x,y)
where J = (,) is the Jacobian which can be inverted using Cramers rule

Derivative transformations
   
u 1 u y u y u 1 u x u x
= , =
x det J y det J
Direct versus inverse mapping

Total differentials for both coordinate systems




= (x, y) d = x dx + y dy d dx
x y
=


= (x, y) d dy
d = x dx + y dy x y


x x
x = x(, ) dx = d + d dx x x
d

=
y y

y = y(, ) y y dy d
dy = d + d

1
x x y
x y 1 x

= =
y y det J y x
x y

Relationship between the direct and inverse metrics


1 y 1 y 1 x 1 x
= , = , = , =
x det J x det J y det J y det J

You might also like