Professional Documents
Culture Documents
Per-Olof Persson
persson@berkeley.edu
Department of Mathematics
u(x̄ + h) − u(x̄) h
D+ u(x̄) = = u0 (x̄) + u00 (x̄) + O(h2 )
h 2
u(x̄) − u(x̄ − h) 0 h 00
D− u(x̄) = = u (x̄) − u (x̄) + O(h2 )
h 2
u(x̄ + h) − u(x̄ − h) h2
D0 u(x̄) = = u0 (x̄) + u000 (x̄) + O(h4 )
2h 6
u(x̄ − h) − 2u(x̄) + u(x̄ + h)
D2 u(x̄) =
h2
h2
= u00 (x̄) + u0000 (x̄) + O(h4 )
12
Method of Undetermined Coecients
1
u(xi ) = u(x̄) + (xi − x̄)u0 (x̄) + · · · + (xi − x̄)k u(k) (x̄) + · · ·
k!
Seek approximation of the form
u(xn )
Insert the exact solution u(x) into the dierence scheme to get
the local truncation error:
1
τj = (u(xj−1 ) − 2u(xj ) + u(xj+1 )) − f (xj )
h2
h2
= u00 (xj ) + u0000 (xj ) + O(h4 ) − f (xj )
12
h2 0000
= u (xj ) + O(h4 )
12
or
τ1
..
τ = . = Aû − f
τn
Errors
Ah eh = −τ h
eh = −(Ah )−1 τ h
keh k = k(Ah )−1 τ h k ≤ k(Ah )−1 k · kτ h k
keh k ≤ C · kτ h k → 0 if kτ h k → 0 as h → 0
Stability, Consistency, and Convergence
Denition
A method Ah uh = f h is stable if (Ah )−1 exists and
k(Ah )−1 k ≤ C for h ≤ h0
It is consistent with the DE if kτ h k → 0 as h → 0
It is convergent if keh k → 0 as h → 0
h2 0000 h2 00
kτ h k2 ≈ ku k2 = kf k2 =⇒ keh k2 = O(h2 )
12 12
While this implies convergence in the max-norm, 1/2 order is
lost because of the grid function norm:
1
keh k∞ ≤ √ keh k2 = O(h3/2 )
h
∂2u ∂2u
∇2 u(x, y) ≡ (x, y) + (x, y) = f (x, y)
∂x2 ∂y 2
xi = a + ih, i = 0, 1, . . . , n
yi = c + jk, j = 0, 1, . . . , m
Finite Dierence Discretization
"""
A, b, x, y = assemblePoisson(n, f, g)
return A, b, x, y
end
FDM for Poisson, Julia implementation
"""
error = testPoisson(n=20)
# Plotting
clf(); contour(x, y, u, 10, colors="k"); contourf(x, y, u, 10)
axis("equal"); colorbar()
x = x(ξ, η)
y = y(ξ, η)
Ω
Ω̂ y
ξ
x
Poisson in 2D non-rectangular domain
Transformed derivatives
ux = ξx uξ + ηx uη
u(x, y) = u(x(ξ, η), y(ξ, η)) =⇒
uy = ξy uξ + ηy uη
where J = xξ yη − xη yξ
Poisson in 2D non-rectangular domain
Transformed equations
1
− (auξξ − 2buξη + cuηη + duη + euξ ) = f
J2
where
x = x(ξ, η)
y = y(ξ, η)
Ω
Ω̂ y
ξ
x
Finite Dierence Methods for Parabolic Problems
Parabolic equations
u(x, t0 ) = η(x)
ut = κuxx , 0≤x≤1
t
Introduce nite dierence grid:
xi = ih, tn = nk
tn+1
Uin+1 − Uin 1 n
− 2Uin + Ui+1
n
= 2 Ui−1
k h
or, as an explicit expression for Uin+1 ,
k
Uin+1 = Uin + n
− 2Uin + Ui+1
n
2
Ui−1
h
Explicit one-step method in time
Boundary conditions naturally implemented by setting
U0n = g0 (tn ), n
Um+1 = g1 (tn )
FTCS, Julia implementation
using PyPlot, LinearAlgebra, DifferentialEquations
"""
Solves the 1D heat equation with the FTCS scheme (Forward-Time,
Centered-Space), using grid size `m` and timestep multiplier `kmul`.
Integrates until final time `T` and plots each solution.
"""
function heateqn_ftcs(m=100; T=0.2, kmul=0.5)
# Discretization
h = 1.0 / (m+1)
x = h * (0:m+1)
k = kmul*h2
N = ceil(Int, T/k)
Uin+1 − Uin 1
D2 Uin + D2 Uin+1
=
k 2
1 n+1
n
− 2Uin + Ui+1
n
− 2Uin+1 + Ui+1
n+1
= 2 Ui−1 + Ui−1
2h
or
n+1
−rUi−1 + (1 + 2r)Uin+1 − rUi+1
n+1 n
= rUi−1 + (1 − 2r)Uin + rUi+1
n
where r = k/2h2
Implicit one-step method in time =⇒ need to solve tridiagonal
system of equations
Crank-Nicolson, Julia implementation
"""
Solves the 1D heat equation with the C r a n k Nicolson scheme,
using grid size `m` and timestep multiplier `kmul`.
Integrates until final time `T` and plots each solution.
"""
function heateqn_cn(m=100; T=0.2, kmul=50)
# Discretization
h = 1.0 / (m+1)
x = h * (0:m+1)
k = kmul*h2
N = ceil(Int, T/k)
# Animate solution
clf(); axis([0, 1, -0.1, 1.1]); grid(true); ph, = plot(x,u) # Setup plotting
for n = 1:length(sol)
ph[:set_data](x,sol.u[n]), pause(1e-3) # Update plot
end
end
Method of Lines, Stability
Stability requires kλ to be inside the
absolute stability region, for all Im(kλ)
eigenvalues λ of A
For the centered dierences, the
eigenvalues are −1 Re(kλ)
2
λp = (cos(pπh) − 1), p = 1, . . . , m
h2
Forward-Euler stability region
or, in particular, λm ≈ −4/h2
Euler gives −2 ≤ −4k/h2 ≤ 0, or Im(kλ)
k 1
2
≤
h 2
−1 Re(kλ)
=⇒ time step restriction for FTCS
Trapezoidal method A-stable =⇒
Crank-Nicolson is stable for any time
Trapezoidal method stability region
step k > 0
Convergence
For convergence, k and h must in general approach zero at
appropriate rates, for example k → 0 and k/h2 ≤ 1/2
Write the methods as
U n+1 = B(k)U n + bn (k) (*)
where, e.g., B(k) = I + kA for forward Euler and
−1
B(k) = I − k2 A I + k2 A for Crank-Nicolson
Denition
A linear method of the form (*) is Lax-Richtmyer stable if, for each
time T , these is a constant CT > 0 such that
kB(k)n k ≤ CT
for all k > 0 and integers n for which kn ≤ T .
Example
−1
For the Crank-Nicolson method, B(k) = I − k2 A I + k2 A is
Example
kB(k)k ≤ 1 is called strong stability, but Lax-Richtmyer stability is
also obtained if kB(k)k ≤ 1 + αk for some constant α, since then
1 i(j+1)hξ 1 ihξ
D0 Wj = e − ei(j−1)hξ = e − e−ihξ eijhξ
2h 2h
i i
= sin(hξ)eijhξ = sin(hξ)Wj ,
h h
that is, W is an eigenfunction with eigenvalue i
h sin(hξ)
Note that this agrees to rst order with the eigenvalue iξ of
the operator ∂x
Von Neumann Analysis
Consider a function Vj on
the grid xj = jh , with nite 2-norm
1/2
∞
X
kV k2 = h |Vj |2
j=−∞
kU n+1 k2 ≤ (1 + αk)kU n k2
Example (Crank-Nicolson)
For the Crank Nicolson method,
n+1
−rUi−1 n+1
+ (1 + 2r)Uin+1 − rUi+1 n
= rUi−1 + (1 − 2r)Uin + rUi+1
n
k
Uij∗ = Uijn + (Dx2 Uijn + Dx2 Uij∗ )
2
k
Uijn+1 = Uij∗ + (Dy2 Uij∗ + Dx2 Uijn+1 )
2
or, as in the alternating direction implicit (ADI) method,
k
Uij∗ = Uijn + (Dy2 Uijn + Dx2 Uij∗ )
2
k
Uijn+1 = Uij∗ + (Dx2 Uij∗ + Dy2 Uijn+1 )
2
Implicit scheme with only tridiagonal systems
Remains second order accurate
Finite Dierence Methods for Hyperbolic Problems
Advection
The scalar advection equation, with constant velocity a:
ut + aux = 0
FTCS scheme:
Ujn+1 − Ujn a n n
=− Uj+1 − Uj−1
k 2h
or
ak
Ujn+1 = Ujn − n n
Uj+1 − Uj−1
2h
Stability problems more later
The Lax-Friedrichs Method
1 ak
Ujn+1 = n n n n
Uj−1 + Uj+1 − Uj+1 − Uj−1
2 2h
Lax-Richtmyer stable if
ak
≤ 1,
h
upj = e2πipjh , p, j = 1, 2, . . . , m + 1
Forward Euler
Use Forward Euler in time =⇒ FTCS scheme:
ak
Ujn+1 = Ujn − n n
Uj+1 − Uj−1
2h
Stability region S : |1 + kλ| ≤ 1 =⇒ imaginary kλp will always
be outside S =⇒ unstable for xed k/h
However, if e.g. k = h2 , we have
2
2 ka Im(kλ)
|1 + kλp | ≤ 1 +
h
= 1 + a2 h2 = 1 + a2 k
−1 Re(kλ)
which gives Lax-Richtmyer stability
2 T /2
k(I + kA)n k2 ≤ (1 + a2 k)n/2 ≤ ea
Forward-Euler stability region
Ujn+1 − Ujn n − Un
Uj+1 h2 n − 2U n + U n
Uj−1
j−1 j j+1
+a = 2
k 2h 2k h
ut + aux = uxx
where = h2 /(2k).
Lax-Friedrichs
The Lax-Friedrichs method can then be written as
U 0 (t) = A U (t) with
0 1 −1
−1 0 1
a −1 0 1
A = − .. .. ..
2h . . .
−1 0 1
1 −1 0
−2 1 1
1
−2 1
1 −2 1
+ 2 .. .. ..
h . . .
1 −2 1
1 1 −2
where = h2 /(2k)
Lax-Friedrichs
ak
−2 < −2k/h2 < 0, or 0≤ ≤1
h
The three methods, Lax-Wendro, upwind, Lax-Friedrichs, can
all be written as advection-diusion with
a2 k ahν ah h2 ah
LW = = , up = , LF = =
2 2 2 2k 2ν
where ν = ak/h. Stable if 0 < ν < 1.
The Beam-Warming method
g(ξ) = (1 − ν) + νe−iξh
Example (Lax-Friedrichs)
stable if |ν| ≤ 1
Von Neumann analysis
Example (Lax-Wendro)
stable if |ν| ≤ 1
Example (Leapfrog)
h
tn+1
tn
xj−1 xj
ak
The CFL condition
For the advection equation, u(X, T ) depends only on the
initial data η(X − aT )
The domain of dependence is D(X, T ) = {X − aT }
Heat equation ut = uxx , D(X, T ) = (−∞, ∞)
Domain of dependence for 3-point explicit FD method: Each
value depends on neighbors at previous timestep
Rening the grid with xed k/h ≡ r gives same interval
This region must contain the true D for the PDE:
X − T /r ≤ X − aT ≤ X + T /r
=⇒ |a| ≤ 1/r or |ak/h| ≤ 1
The Courant-Friedrichs-Lewy (CFL) condition: Numerical
domain of dependence must contain the true D as k, h → 0
t2 t4
t0 t0
xj−2 xj xj+2 xj−4 xj xj+4
The CFL condition
Example (FTCS)
The centered-dierence scheme for the advection equation is
unstable for xed k/h even if |ak/h| ≤ 1
Example (Beam-Warming)
3-point one-sided stencil, CFL condition gives 0 ≤ ak/h ≤ 2 (for
left-sided, used when a > 0)
Example (Crank-Nicolson)
Any implicit scheme satises the CFL condition, since the
tridiagonal linear system couples all points.
Modied equations
Find a PDE vt = · · · that the numerical approximation Ujn
satises exactly, or at least better than the original PDE
Example (Upwind method)
To second order accuracy, the numerical solution satises
1 ak
vt + avx = ah 1 − vxx
2 h
Advection-diusion equation
Example (Lax-Wendro)
To third order accuracy,
2 !
1 2 ak
vt + avx + ah 1 − vxxx = 0
6 h
Dispersive behavior, leading to a phase error . To fourth order,
2 !
1 2 ak
vt + avx + ah 1 − vxxx = −vxxxx
6 h
where = O(k 3 + h3 ) =⇒ highest modes damped
Modied equations
Example (Beam-Warming)
To third order,
2 !
1 3ak ak
vt + avx = ah2 2− + vxxx
6 h h
Example (Leapfrog)
Modied equation
2 !
1 ak
vt + avx + ah2 1− vxxx = vxxxxx + · · ·
6 h
k k2
Ujn+1 = Ujn − n
A(Uj+1 n
− Uj−1 ) + 2 A2 (Uj−1
n
− 2Ujn + Uj+1
n
)
2h 2h
Second-order accurate, stable if ν = max1≤p≤s |λp k/h| ≤ 1
k
Ujn+1 = Ujn − A(Ujn − Uj−1
n
)
h
k
Ujn+1 n
= Ujn − A(Uj+1 − Ujn )
h
Only useful if all eigenvalues of A have same sign. Instead,
decompose into scalar equations and upwind each one separately
=⇒ Godunov's method
Initial boundary value problems