Вы находитесь на странице: 1из 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 = i∆x 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 ∆x→0 ∆x ∆x→0 ∆x
u(x̄ + ∆x) − u(x̄ − ∆x)
= lim (by definition)
∆x→0 2∆x
Approximation of first-order derivatives

Geometric interpretation

u forward ba kward
entral ∂u
 ui+1 −ui
∂x i ≈ ∆x forward difference

∂u
 ui −ui−1

x x exa t ∂x i ≈ ∆x backward difference

∂u
 ui+1 −ui−1
∂x i ≈ 2∆x central difference

xi 1 xi xi+1 x ∞ 
P (x−xi )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 : ui−1 = 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 − ui−1 ∆x ∂2u (∆x)2 ∂3u
T2 ⇒ = + − + ...
∂x i ∆x 2 ∂x2 i 6 ∂x3 i

backward difference truncation error O(∆x)


   
∂u ui+1 − ui−1 (∆x)2 ∂3u
T1 − T2 ⇒ = − + ...
∂x i 2∆x 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 + ui−1 2
T1 + T2 ⇒ = + O(∆x)
∂x2 i (∆x)2
Alternative derivation
 2     ∂u
 ∂u

∂ u ∂ ∂u ∂x i+1/2 − ∂x i−1/2
= = lim
∂x2 i ∂x ∂x i ∆x→0 ∆x
ui+1 −ui
∆x − ui −u
∆x
i−1
ui+1 − 2ui + ui−1
≈ =
∆x (∆x)2

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


∂x diffusive flux

di+1/2 ui+1 − di−1/2 ui −u


  −ui
∂f fi+1/2 − fi−1/2 ∆x ∆x
i−1

≈ =
∂x i ∆x ∆x
di+1/2 ui+1 − (di+1/2 + di−1/2 )ui + di−1/2 ui−1
=
(∆x)2
Approximation of mixed derivatives
  
∂2u ∂ ∂u ∂ ∂u y
2D: ∂x∂y = ∂x ∂y = ∂y ∂x
“ ” “ ”
∂u ∂u

∂2u
„ «
∂y ∂y
i+1,j i−1,j
= + O(∆x)2 yj +1
∂x∂y i,j 2∆x
yj
„ «
∂u ui+1,j+1 − ui+1,j−1
= + O(∆y)2 yj 1
∂y i+1,j 2∆y
„ «
∂u ui−1,j+1 − ui−1,j−1
= + O(∆y)2
∂y i−1,j 2∆y xi 1 xi xi+1 x

Second-order difference approximation


 2 
∂ u ui+1,j+1 − ui+1,j−1 − ui−1,j+1 + ui−1,j−1
= + O[(∆x)2 , (∆y)2 ]
∂x∂y i,j 4∆x∆y
One-sided finite differences
 
∂u u1 − u0
= + O(∆x) forward difference
∂x 0 ∆x
? x0 x1 x2
backward/central difference approximations
would need u−1 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 c∆x2 = u1 − u0 − b∆x

u1 = a + b∆x + c∆x2 −3u0 + 4u1 − u2
b=
u2 = a + 2b∆x + 4c∆x2 2∆x
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 (2∆x) ∂ u (2∆x) ∂ u
ui+2 = ui + 2∆x + + + ...
∂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 = 2∆x + 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 (2∆x) ∂ u (2∆x) ∂ u
ui+2 = ui + 2∆x + + + ...
∂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 − 6ui−1 + ui−2
= + O(∆x)3 backward difference
∂x i 6∆x
 
∂u −ui+2 + 6ui+1 − 3ui − 2ui−1
= + O(∆x)3 forward difference
∂x i 6∆x
 
∂u −ui+2 + 8ui+1 − 8ui−1 + ui−2
= + O(∆x)4 central difference
∂x i 12∆x
 
∂2u −ui+2 + 16ui+1 − 30ui + 16ui−1 − ui−2
= + 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 = i∆x, ∆x = , i = 0, 1, . . . , N
N
Central difference approximation O(∆x)2

 − ui−1 −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 −1×N −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
2∆x

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 + 2∆xg1
2∆x
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 − 2∆xαuN + 2∆xg2
2∆x
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 )



 − ui−1,j +ui,j−1 −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(h−2 )
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 Cramer’s 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

∂ξ ∂η
⇒  =
∂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 ∂ξ

Оценить