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),
grid points

x0 x1

ui u(xi ),
xi = ix

xi

i = 0, 1, . . . , N
mesh size

xi xi+1

x =

X
N

xN 1 xN

First-order derivatives
u
(
x)
x

u(
x + x) u(
x)
u(
x) u(
x x)
= lim
x0
x0
x
x
u(
x + x) u(
x x)
(by definition)
= lim
x0
2x
=

lim

Approximation of first-order derivatives


Geometric interpretation
u

forward ba kward
entral

ui+1 ui
x

forward difference

ui ui1
x

backward difference

ui+1 ui1
2x

central difference

u
x i

exa t

x x

u
x i

u
x i

x
xi 1 xi xi+1
P
Taylor series expansion
u(x) =

n=0

T1 :

ui+1

T2 :

ui1

(xxi )n
n!

nu
xn i

u C ([0, X])





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




u
(x)2 2 u
(x)3 3 u
= ui x

+
+ ...
x i
2
x2 i
6
x3 i


u
x

Analysis of truncation errors


Accuracy of finite difference approximations
T1

T2




u
x

u
x




ui+1 ui x

=
x
2

forward difference

truncation error O(x)

ui ui1 x
+
=
x
2

backward difference

T1 T2

u
x

2u
x2

2u
x2

(x)2

(x)2




3u
x3

3u
x3

truncation error O(x)

ui+1 ui1 (x)2

=
2x
6
central difference

3u
x3

+ ...
i

truncation error O(x)2

Leading truncation error


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

+ ...
i

+ ...
i

Approximation of second-order derivatives


Central difference scheme
T1 + T2

2u
x2

=
i

ui+1 2ui + ui1


2
+
O(x)
(x)2

Alternative derivation

 
 2 
u
u
=
= lim
x2 i
x x i x0

Variable coefficients
 
f

x i
=

ui+1 ui
x

u
x i+1/2

u
x i1/2

i1
ui u
ui+1 2ui + ui1
x
=
x
(x)2

f (x) = d(x) u
x

diffusive flux

ui
i1
di1/2 ui u
fi+1/2 fi1/2
di+1/2 ui+1
x
x
=
x
x

di+1/2 ui+1 (di+1/2 + di1/2 )ui + di1/2 ui1


(x)2

Approximation of mixed derivatives


2D:

2u
xy

u
y

u
y

u
x
u
y

2u
xy

u
y

ui+1,j+1 ui+1,j1
+ O(y)2
2y

u
y

ui1,j+1 ui1,j1
+ O(y)2
2y

i,j

2x

i1,j

+ O(x)2

yj +1
yj

i+1,j

i1,j

i+1,j

yj 1

xi 1

xi

xi+1

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

x0

x1

x2

u
x

=
0

u1 u0
+ O(x) forward difference
x

backward/central difference approximations


would need u1 which is not available
Polynomial fitting
u(x) = u0 + x

u
x

x2
+
2
0

2u
x2

x3
+
6
0

u
b + 2cx,
x

u(x) a + bx + cx2 ,


3u
+ ...
x3 0
 
u
b
x 0

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


cx2 = u1 u0 bx

u0 = a
u1 = a + bx + cx2
u2 = a + 2bx + 4cx2

b=

3u0 + 4u1 u2
2x

Analysis of the truncation error


One-sided approximation

ui+1
ui+2

u
x

ui + ui+1 + ui+2
x






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


ui +ui+1 +ui+2
x

++
x ui

Second-order accurate if
= 23 ,

= 2,

+ ( + 2)

u
x i

+ + = 0,

= 21

u
x i

x
2 (

+ 4)

+ 2 = 1,
=

2u
x2

+ O(x2 )

+ 4 = 0

3ui +4ui+1 ui+2


2x

+ O(x2 )

Application to second-order derivatives




One-sided approximation

ui+1
ui+2

ui + ui+1 + ui+2
x2






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


ui +ui+1 +ui+2
x2

++
x2 ui

First-order accurate if
= 1,

2u
x2

= 2,

+2
x

u
x i

+ + = 0,

=1

u
x i

+4
2

2u
x2

+ 2 = 0,
ui 2ui+1 +ui+2
x2

+ O(x)

+ 4 = 2
+ O(x)

High-order approximations


u
x
u
x
u
x





2u
x2

2ui+1 + 3ui 6ui1 + ui2


+ O(x)3
6x

ui+2 + 6ui+1 3ui 2ui1


+ O(x)3
6x

forward difference

ui+2 + 8ui+1 8ui1 + ui2


+ O(x)4
12x

central difference

=
i

backward difference

ui+2 + 16ui+1 30ui + 16ui1 ui2


+ O(x)4
2
12(x)

central difference

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
x
One-dimensional mesh
ui u(xi ),

in = (0, 1),

x0

fi = f (xi )

x1

xi 1

xi = ix,

u(0) = u(1) = 0

xi

xi+1

x =

xN 1 xN

1
,
N

i = 0, 1, . . . , N

Central difference approximation


O(x)2

i+1
ui1 2ui +u
= 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

2 +u3

u1 2u

(x)2
i=2
3 +u4
i=3
u2 2u
2
(x)

...

uN 2 2uN 1 +uN

i=N 1

Matrix form

Au = F

(x)2

A RN 1N 1

2 1
1 2 1

1
2
1
A=
,

(x)2
...
1 2

= f1
= f2
= f3
= fN 1

u, F RN 1

u=

u1
u2
u3

uN 1

F =

f1
f2
f3

fN 1

The matrix A is tridiagonal and symmetric positive definite invertible.

Other types of boundary conditions


Dirichlet-Neumann BC

u(0) =

u
x (1)

uN +1 uN 1
=0
2x

u0 = 0,

=0

uN +1 = uN 1

central difference

Extra equation for the last node

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

Extended linear system

A RN N

Au = F

2 1
1 2 1
1
1 2 1

A=

...
(x)2

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

2 1
1 1

u, F RN

u=

u1
u2
u3

N 1

uN

The matrix A remains tridiagonal and symmetric positive definite.

F =

f1
f2
f3

N 1
1
2 fN

Other types of boundary conditions


Non-homogeneous Dirichlet BC
u0 = g0

u(0) = g0

2u1 u2
g0
=
f
+
1
(x)2
(x)2
u
x (1)

Non-homogeneous Neumann BC
uN +1 uN 1
= g1
2x

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

Non-homogeneous Robin BC
uN +1 uN 1
+ uN = g2
2x

only F changes

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

= g1

only F changes

uN +1 = uN 1 + 2xg1

u
x (1)

first equation

uN 1 + uN
1
g1
=
f
+
N
(x)2
2
x

+ u(1) = g2

A and F change

uN +1 = uN 1 2xuN + 2xg2
uN 1 + (1 + x)uN
1
g2
=
f
+
N
(x)2
2
x

Example: 2D Poisson equation


y

Boundary value problem

2 u2 2 u2 = f in = (0, 1) (0, 1)
x
y

u=0

ui,j u(xi , yj ),

N=

ui,0 = ui,N = u0,j = uN,j = 0

h
h

yj 1

xi 1

(xi , yj ) = (ih, jh),

Central difference approximation


O(h2 )

+ui+1,j +ui,j+1
ui1,j +ui,j1 4ui,j
= fi,j ,
h2

ve-point sten il

yj

1
h

fi,j = f (xi , yj ),

yj +1

on =

Uniform mesh: x = y = h,

xi

xi+1

i, j = 0, 1, . . . , N

i, j = 1, . . . , N 1

i, j = 0, 1, . . . , N

Example: 2D Poisson equation


A R(N 1)

(N 1)2

u, F R(N 1)

Linear system

Au = F

row-by-row
node numbering

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


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

B I
I B I

... ...
A=

I=

1
1

1
1

,
B I
I B

4 1
1 4 1

... ...
B=

4 1
1 4

The matrix A is sparse, block-tridiagonal


(for the above numbering) and SPD.
cond2 (A) =

|max |
= O(h2 )
|min |

Caution: convergence of iterative solvers deteriorates as the mesh is refined

Treatment of complex geometries


2D Poisson equation

2 u2 2 u2 = f
x
y

u = g0

in
on

Difference equation
u1 + u2 4u0 + u3 + u4
= f0

h2

4
Q

0 Q

h
P

stencil of Q
curvilinear boundary

Linear interpolation
u4 (h ) + u0
h

= g0 (R) u4 = u0
+ g0 (R)
h
h
h


h

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


u(R) =

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

re tangle

P
inverse mapping

a
x

physi al domain
body- tted grid

a


omputational domain
artesian grid

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


discretized in the computational domain rather than the physical one.
Derivative transformations

u u
,
,...
x y
|
{z
}

difficult to compute

u u
,
,...

|
{z
}

easy to compute

PDE transformations for a direct mapping


Direct mapping
Chain rule

= (x, y),

= (x, y)

u
u
u
=
+
,
x
x x

2u
u 2
u 2
=
+
x2
x2
x2
2u
u 2
u 2
=
+
y 2
y 2
y 2

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

x
 2
 2
2 u
2 u 2 u
+ 2
+2
+ 2
y y

u = f
turns into
Example: 2D Poisson equation
" 
" 


 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
contain many more terms
x2
y 2
x2
y 2
The metrics need to be determined (approximated by finite differences)

PDE transformations for an inverse mapping


Inverse mapping

x = x(, )

Metrics transformations


,
,
,
x y x y
|
{z
}

unknown

Chain rule
u

where J =

y = y(, )

u x
x

u x
x

(x,y)
(,)

u y
y

u y
y

x x y y
,
,
,

|
{z
}
known

{z
J

u
x
u
y

is the Jacobian which can be inverted using Cramers rule

Derivative transformations


u
1
u y
u y
=

,
x
det J

u
1
=
y
det J

u x u x

Direct versus inverse mapping


Total differentials for both coordinate systems
= (x, y)
= (x, y)
x = x(, )
y = y(, )

x dx

d =

x dx

y dy

dx =

x
d

x
d

y
d

d =

dy =

y dy

y
d
x

Relationship between the direct and inverse metrics

1 y
=
,
x
det J

1 y
=
,
x
det J

d
d
dx
dy

1
det J

x
x

1 x
=
,
y
det J

y
x

dx
dy
d
d

1 x
=
y
det J

You might also like