Finite difference approximations

advertisement
LECTURE 19: THE FINITE DIFFERENCE METHODS
MINGFENG ZHAO
August 06, 2015
Finite difference approximations
Definition 1. We say that a function g(x) is said to be nth-order accurate, denoted by O((∆x)n ), if
g(x)
is finite.
(∆x)n
lim
∆x→0
Recall the Taylor’s formula:
f (x + ∆x) = f (x) + f 0 (x) · ∆x +
f 00 (x)
f 3 (x)
· (∆x)2 +
· (∆x)3 + · · · .
2!
3!
Then we have
(1)
f (x + ∆x)
(2)
f (x − ∆x)
f 00 (x)
f (3) (x)
· (∆x)2 +
(∆x)3 + O((∆x)4 )
2
6
f (3) (x)
f 00 (x)
· (∆x)2 −
(∆x)3 + O((∆x)4 ),
= f (x) − f 0 (x) · ∆x +
2
6
= f (x) + f 0 (x) · ∆x +
Then
• Forward difference approximation of the first derivative: By (1), we have
f (x + ∆x) − f (x)
= f 0 (x) + O(∆x).
∆x
That is, when we take ∆x very small, we can think
f 0 (x) ≈
f (x + ∆x) − f (x)
.
∆x
• Backward difference approximation of the first derivative: By (2), we have
f (x) − f (x − ∆x)
= f 0 (x) + O(∆x).
∆x
That is, when we take ∆x very small, we can think
f 0 (x) ≈
f (x) − f (x − ∆x)
.
∆x
1
2
MINGFENG ZHAO
• Centeral difference approximation of the first derivative: By (1)−(2), we have
f (x + ∆x) − f (x − ∆x)
= f 0 (x) + O((∆x)2 ).
2∆x
That is, when we take ∆x very small, we can think
f 0 (x) ≈
f (x + ∆x) − f (x − ∆x)
.
2∆x
• Centeral difference approximation of the second derivative: By (1)+(2), we have
f (x + ∆x) + f (x − ∆x) − 2f (x)
= f 00 (x) + O((∆x)2 ).
2∆x
That is, when we take ∆x very small, we can think
f 00 (x) ≈
f (x + ∆x) + f (x − ∆x) − 2f (x)
.
2∆x
Remark 1. The forward and backward difference approximations of the first derivative are first order accurate, but
the central difference approximations of the first and second derivatives are second accurate.
Finite difference method for an ODE: Euler’s method
Consider the following ODE with initial condition:


u0 (t) = g(t),
(3)
 IC : u(0) = a.
t ≥ 0,
Forward difference in time t:
u(t + ∆t) = u(t) + ut (t)∆t + O(∆t),
then
(4)
u(t + ∆t) − u(t)
= ut (t) + O(∆t).
∆t
Plug (4) into (3), we get
u(t + ∆t) − u(t)
= g(t) + O(∆t).
∆t
Think O(∆t) to be 0, then we get
u(t + ∆t) = u(t) + g(t)∆t.
LECTURE 19: THE FINITE DIFFERENCE METHODS
3
For any T > 0, we divide the time interval [0, T ] to M subintervals equally with M + 1 timed sample points tk = k∆t,
T
k = 0, 1, · · · , M , that is, the step ∆t =
, let uk = u(tk ), then we get the following difference equation:
M
uk+1 = uk + g(tk )∆t.
(5)
By (3), we have
u0 = a.
So we have
By (5)
By (5)
By (5)
By (5)
By (5)
a = u0 −−−−→ u1 −−−−→ u2 −−−−→ u3 −−−−→ u4 −−−−→ · · · .
Finite difference method for the heat equation
Consider the heat equation with initial condition:



ut = c2 uxx + σ(x, t), 0 ≤ x ≤ L, t ≥ 0,
(6)

 IC : u(x, 0) = f (x), 0 ≤ x ≤ L.
Forward difference in time t:
u(x, t + ∆t) = u(x, t) + ut (x, t)∆t + O(∆t),
then
u(x, t + ∆t) − u(x, t)
= ut (x, t) + O(∆t).
∆t
(7)
Central difference in space x:
u(x + ∆t, t)
u(x − ∆t, t)
uxx (x, t)
uxxx (x, t)
(∆x)2 +
(∆x)3 + O((∆x)4 )
2
3!
uxx (x, t)
uxxx (x, t)
= u(x, t) − ux (x, t)∆x +
(∆x)2 −
(∆x)3 + O((∆x)4 )
2
3!
= u(x, t) + ux (x, t)∆x +
then
(8)
u(x + ∆x, t) − 2u(x, t) + u(x − ∆x, t)
= uxx (x, t) + O((∆x)2 ).
(∆x)2
Plug (7) and (8) into (6), then
u(x + ∆x, t) − 2u(x, t) + u(x − ∆x, t)
u(x, t + ∆t) − u(x, t)
= c2
+ σ(x, t) + O(∆t, (∆x)2 ),
∆t
(∆x)2
which is second order accurate in space and first order accurate in time.
4
MINGFENG ZHAO
Now think O(∆t, (∆)2 ) to be 0, then
(9)
u(x, t + ∆t) = u(x, t) + c2 ·
∆t
· [u(x + ∆x, t) − 2u(x, t) + u(x − ∆x, t)] + σ(x, t)∆t.
(∆x)2
Remark 2. The use of the forward difference means the method is explicit, because (9) gives an explicit formula for
u(x, t + ∆t) depending only on the values of u at time t.
For any real number T > 0 and two natural numbers N, M ∈ N, we divide the spatial interval [0, L] to N subintervals
L
equally with N + 1 spaced sample points xk = k∆x, k = 0, 1, · · · , N , that is, the step ∆x = ; and divide the time
N
interval [0, T ] to M subintervals equally with M + 1 timed sample points tk = k∆t, k = 0, 1, · · · , M , that is, the step
T
. Then we are going to approximate the solution to (6) by finding the discrete value:
∆t =
M
ukn = u(xn , tk ) = u(k∆x, k∆t),
0 ≤ n ≤ N, 0 ≤ k ≤ M.
Plug x = xn and t = tk into (9), we have the following difference equation:
(10)
uk+1
= ukn + c2 ·
n
∆t
· [ukn+1 − 2ukn + ukn−1 ] + σ(xn , tk )∆t.
(∆x)2
For the initial condition, we know that
(11)
For the boundary condition:
u0n = u(xn , t0 ) = u(xn , 0) = f (xn ),
0 ≤ n ≤ N.
LECTURE 19: THE FINITE DIFFERENCE METHODS
5
• Dirichlet boundary condition: Assume that
(12)
u(0, t) = g(t),
u(L, t) = h(t),
t > 0.
Then we have
uk0 = u(x0 , tk ) = u(0, tk ) = g(tk ),
(13)
ukN = u(xN , tk ) = u(L, tk ) = h(tk ),
0 ≤ k ≤ M.
Hence, we have
– By (11), we know the values u0n for 0 ≤ n ≤ N , plug the values u0n ’s (0 ≤ n ≤ N ) into (10), we can get u1n
for 1 ≤ n ≤ N − 1. By (13), we know the values u10 and u1N . So we know the values u1n ’s (0 ≤ n ≤ N ).
– Plug the values u1n ’s (0 ≤ n ≤ N ) into (10), we can get u2n for 1 ≤ n ≤ N − 1. By (13), we know the values
u20 and u2N . So we know the values u2n ’s (0 ≤ n ≤ N ).
– Plug the values u2n ’s (0 ≤ n ≤ N ) into (10), we can get u3n for 1 ≤ n ≤ N − 1. By (13), we know the values
u30 and u3N . So we know the values u3n ’s (0 ≤ n ≤ N ).
– Keep going.
• Neumann boundary condition: Assume that
(14)
ux (0, t) = g(t),
ux (L, t) = h(t),
t ≥ 0.
Use the central difference in space x:
u(x + ∆t, t)
u(x − ∆t, t)
uxx (x, t)
uxxx (x, t)
(∆x)2 +
(∆x)3 + O((∆x)4 )
2
3!
uxxx (x, t)
uxx (x, t)
(∆x)2 −
(∆x)3 + O((∆x)4 )
= u(x, t) − ux (x, t)∆x +
2
3!
= u(x, t) + ux (x, t)∆x +
then
u(x + ∆x, t) − u(x − ∆x, t)
= ux (x, t) + O((∆x))2 .
2∆x
Now think O((∆x)2 ) to be 0, then
u(x + ∆x, t) = u(x − ∆x, t) + 2ux (x, t)∆x.
(15)
Plug x = x0 = 0 and t = tk into (15), then
uk−1 = uk1 − 2g(tk )∆x,
(16)
Hence, we have
and
ukN +1 = ukN −1 + 2h(tk )∆x.
6
MINGFENG ZHAO
– By (11), we know the values u0n for 0 ≤ n ≤ N , plug the values u01 and u0N −1 into (16), we can get u0−1 and
u0N +1 . So we know the values u0n ’s for −1 ≤ n ≤ N + 1.
– Plug the values u0n ’s (−1 ≤ n ≤ N + 1) into (10), we can get the values u1n for 0 ≤ n ≤ N . Plug the values
u11 and u1N −1 into (16), we can get u1−1 and u1N +1 . So we know the values u1n ’s for −1 ≤ n ≤ N + 1.
– Plug the values u1n ’s (−1 ≤ n ≤ N + 1) into (10), we can get the values u1n for 0 ≤ n ≤ N . Plug the values
u21 and u2N −1 into (16), we can get u2−1 and u2N +1 . So we know the values u2n ’s for −1 ≤ n ≤ N + 1.
– Keep going.
• Robin boundary condition I: Assume that
(17)
ux (0, t) = g(t),
u(L, t) = h(t),
t ≥ 0.
Then we have
ukN = u(xN , tk ) = u(L, tk ) = h(tk ),
(18)
0 ≤ k ≤ M.
Use the central difference in space x, then
uk−1 = uk1 − 2g(tk )∆x.
(19)
Hence, we have
– By (11), we know the values u0n for 0 ≤ n ≤ N , plug the value u01 into (19), we get u0−1 . So we know the
values u0n ’s for −1 ≤ n ≤ N .
– Plug the values u0n ’s (−1 ≤ n ≤ N ) into (10), we get u1n for 0 ≤ n ≤ N − 1. Plug u11 into (19), we get u1−1 .
By (18), we know the value u1N . So we know the values u1n ’s for −1 ≤ n ≤ N .
– Plug the values u1n ’s (−1 ≤ n ≤ N ) into (10), we get u2n for 0 ≤ n ≤ N − 1. Plug u21 into (19), we get u2−1 .
By (18), we know the value u2N . So we know the values u2n ’s for −1 ≤ n ≤ N .
– Keep going.
• Robin boundary condition II: Assume that
(20)
u(0, t) = g(t),
ux (L, t) = h(t),
t > 0.
Then we have
(21)
uk0 = u(x0 , tk ) = u(0, tk ) = g(tk ),
0 ≤ k ≤ M.
LECTURE 19: THE FINITE DIFFERENCE METHODS
7
Use the central difference in space x, then
ukN +1 = ukN −1 + 2h(tk )∆x.
(22)
Hence, we have
– By (11), we know the values u0n for 0 ≤ n ≤ N , plug the value u0N −1 into (19), we get u0N +1 . So we know
the values u0n ’s for 0 ≤ n ≤ N + 1.
– Plug the values u0n ’s (0 ≤ n ≤ N + 1) into (10), we get u1n ’s for 1 ≤ n ≤ N . Plug the value u1N −1 into (19),
we get u1N +1 . By (21), we get u10 . So we know the values u1n ’s for 0 ≤ n ≤ N + 1.
– Plug the values u1n ’s (0 ≤ n ≤ N + 1) into (10), we get u2n ’s for 1 ≤ n ≤ N . Plug the value u2N −1 into (19),
we get u2N +1 . By (21), we get u20 . So we know the values u2n ’s for 0 ≤ n ≤ N + 1.
– Keep going.
• Peoridic boundary condition: Assume that
(23)
u(0, t) = u(L, t),
ux (0, t) = ux (L, t),
t ≥ 0.
Then we have
uk0 = u(x0 , tk ) = u(0, tk ) = u(L, tk ) = u(xN , tk ) = ukN ,
0 ≤ k ≤ M.
Use the central difference in space x, then
uk1 − uk−1 = ukN +1 − ukN −1 ,
0 ≤ k ≤ M.
By (10), we have
(24)
uk+1
0
= uk0 + c2 ·
∆t
· [uk1 − 2uk0 + uk−1 ] + σ(x0 , tk )∆t
(∆x)2
uk+1
N
= ukN + c2 ·
∆t
· [ukN +1 − 2ukN + ukN −1 ] + σ(xN , tk )∆t.
(∆x)2
So we get


uk+1
− uk+1
=0

0
N






k
k
k
k

 u−1 + uN +1 = u1 + uN −1
∆t
∆t

−c2 ·
· uk−1 + uk+1
= uk0 + c2 ·
· [uk1 − 2uk0 ] + σ(x0 , tk )∆t

0

2

(∆x)
(∆x)2




∆t
∆t

 uk+1
− c2 ·
· uk
= ukN + c2 ·
· [−2ukN + ukN −1 ] + σ(xN , tk )∆t.
N
(∆x)2 N +1
(∆x)2
8
MINGFENG ZHAO
That is, we get

0



1


 −c2 · ∆t

(∆x)2

0
1 −1
0
0
0
1
1
0
0
0
1
−c2 ·
 
k
0
u
  −1  
  k+1  
 
 u
uk1 + ukN −1
 
 0
=
 

  uk+1  
∆t
k
k
uk0 + c2 · (∆x)
 
 N
2 · [u1 − 2u0 ] + σ(x0 , tk )∆t
 

∆t
k
k
ukN +1
ukN + c2 · (∆x)
2 · [−2uN + uN −1 ] + σ(xN , tk )∆t

∆t
(∆x)2





.



Notice that

0



1

det 
 −c2 · ∆t

(∆x)2

0
1 −1

0
0
0
1
1
0
0
0
1
−c2 ·
∆t
(∆x)2



∆t

6= 0.
 = 2c2 ·

(∆x)2


So uk−1 , uk+1
, uk+1
and ukN +1 can be solve by (24) in terms of uk0 , uk1 , ukN −1 and ukN .
0
N
Hence, we have
– By (11), we know the values u0n for 0 ≤ n ≤ N . Plug the values u00 , u01 , u0N −1 and u0N into (24), we get u0−1
and u0N +1 . So we know the values u0n ’s for −1 ≤ n ≤ N + 1.
– Plug the values u0n ’s (−1 ≤ n ≤ N + 1) into (10), we get u1n ’s for 0 ≤ n ≤ N . Plug the values u10 , u11 , u1N −1
and u1N into (24), we get u1−1 and u1N +1 . So we know the values u1n ’s for −1 ≤ n ≤ N + 1.
– Plug the values u1n ’s (−1 ≤ n ≤ N + 1) into (10), we get u2n ’s for 0 ≤ n ≤ N . Plug the values u20 , u21 , u2N −1
and u2N into (24), we get u2−1 and u1N +1 . So we know the values u2n ’s for −1 ≤ n ≤ N + 1.
– Keep going.
Finite difference method for the wave equation
Consider the wave equation with initial condition:
(25)



utt = c2 uxx + σ(x, t),
0 ≤ x ≤ L, t > 0,

 IC : u(x, 0) = φ(x), u (x, 0) = ψ(x), 0 ≤ x ≤ L.
t
Central difference in time t:
(26)
u(x, t + ∆t) − 2u(x, t) + u(x, t − ∆t)
= utt (x, t) + O((∆t)2 ).
(∆t)2
LECTURE 19: THE FINITE DIFFERENCE METHODS
Central difference in space x:
u(x + ∆x, t) − 2u(x, t) + u(x − ∆x, t)
= uxx (x, t) + O((∆x)2 ).
(∆x)2
(27)
Plug (26) and (27) into (25), then
u(x, t + ∆t) − 2u(x, t) + u(x, t − ∆t)
u(x + ∆x, t) − 2u(x, t) + u(x − ∆x, t)
= c2 ·
+ σ(x, t) + O((∆t)2 , (∆x)2 ).
(∆t)2
(∆x)2
So we have
k
k
k
uk+1
− 2ukn + uk−1
n
n
2 un+1 − 2un + un−1
=
c
·
+ σ(xn , tk ).
(∆t)2
(∆x)2
That is, we have the following difference equation:
(28)
uk+1
= 2ukn − uk−1
+ c2 ·
n
n
(∆t)2 k
[u
− 2ukn + ukn−1 ] + σ(xn , tk )(∆t)2 .
(∆x)2 n+1
For the initial condition that u(x, 0) = φ(x), we know that
(29)
u0n = u(xn , t0 ) = u(xn , 0) = φ(xn ),
0 ≤ n ≤ N.
For the initial condition that ut (x, 0) = ψ(x), use the central difference in time t, then
u(x, t + ∆t) − u(x, t − ∆t)
= ut (x, t) + O((∆t)2 ).
2∆t
Then we have
u1n − u−1
n = 2ψ(xn )∆t.
9
10
MINGFENG ZHAO
That is, we have
1
u−1
n = un − 2ψ(xn )∆t.
(30)
Assume the wave equation holds for t ≥ 0, by (28), then
2
u1n = 2u0n − u−1
n +c ·
(31)
(∆t)2 0
[u
− 2u0n + u0n−1 ] + σ(xn , t0 )(∆t)2 .
(∆x)2 n+1
Plug (30) into (31), then
u1n = 2u0n − u1n + 2ψ(xn )∆t + c2 ·
(∆t)2 0
[u
− 2u0n + u0n−1 ] + σ(xn , t0 )(∆t)2 ,
(∆x)2 n+1
That is, we have
(32)
(∆t)2 0
1
1
[u
− 2u0n + u0n−1 ] + σ(xn , t0 )(∆t)2 ,
u1n = u0n + ψ(xn )∆t + c2 ·
2
(∆x)2 n+1
2
which implies that essentially we do not need the values u−1
n ’s.
Finite difference method for the Laplace equation
Consider the Laplace equation with Dirichlet boundary condition:



uxx + uyy = 0, 0 < x < a, 0 < x < b,



(33)
BC : u(x, 0) = f1 (x), u(x, b) = f2 (x), 0 ≤ x ≤ a,





u(a, y) = g1 (y), u(0, y) = g2 (y), 0 ≤ y ≤ b.
Central difference in space x:
(34)
u(x + ∆x, y) − 2u(x, y) + u(x − ∆x, y)
= uxx (x, y) + O((∆x)2 ).
(∆x)2
Central difference in spce y:
(35)
u(x, y + ∆y) − 2u(x, y) + u(x, y − ∆y)
= uyy (x, y) + O((∆y)2 ).
(∆y)2
Plug (34) and (35) into (33), we get
(36)
u(x + ∆x, y) − 2u(x, y) + u(x − ∆x, y) u(x, y + ∆y) − 2u(x, y) + u(x, y − ∆y)
+
= O((∆x)2 , (∆y)2 ).
(∆x)2
(∆y)2
For any two natural numbers N, M ∈ N, we divide the interval [0, a] to N subintervals equally with N + 1 spaced
L
sample points xk = k∆x, k = 0, 1, · · · , N , that is, the step ∆x =
; and divide the interval [0, b] to M subintervals
N
LECTURE 19: THE FINITE DIFFERENCE METHODS
equally with M + 1 timed sample points ym = m∆y, m = 0, 1, · · · , M , that is, the step ∆y =
11
b
. Then we are going
M
to approximate the solution to (33) by finding the discrete value:
unm = u(xn , tm ) = u(n∆x, m∆t),
0 ≤ n ≤ N, 0 ≤ m ≤ M.
By (36), we get the following difference equation:
(37)
unm+1 − 2unm + unm−1
un+1m − 2unm + un−1m
+
= 0,
(∆x)2
(∆y)2
1 ≤ n ≤ N1 , 1 ≤ m ≤ M 1 .
For the boundary condition, we know that
(38)
un0 = f1 (xn ), unM = f2 (xn ),
(39)
uN m = g1 (ym ), u0m = g2 (ym ),
0 ≤ n ≤ N,
0 ≤ m ≤ M.
So we must solve (37) which form a linear system of (N − 1) × (M − 1) unknowns for the values unm with 1 ≤ n ≤ N − 1
and 1 ≤ m ≤ M − 1 when we plug boundary values (38) and (39).
Stability of the finite difference method for the heat equation
Recall the difference equation for the heat equation:
(40)
uk+1
= ukn + r[ukn+1 − 2ukn + ukn−1 ],
n
where
r = c2 ·
∆t
.
(∆x)2
12
MINGFENG ZHAO
Let ukn = M k e−in∆xθ be a solution to (40), then
M k+1 e−in∆xθ
=
M k e−in∆xθ + r[M k e−i(n+1)∆xθ − 2M k e−in∆xθ + M k e−i(n−1)∆xθ ]
=
M k e−in∆xθ 1 + re−∆xθ − 2r + re∆xθ
= M k e−n∆xθ [1 − 2r + 2r cos(∆xθ)]
∆xθ
= M k e−n∆xθ 1 − 4r sin2
.
2
So weg get
M = 1 − 4r sin2
∆xθ
2
.
So ukn is bounded if |M | ≤ 1, that is,
1 − 4r sin2 ∆xθ ≤ 1.
2
Hence we get
r≤
It’s easy to see that r ≤
1
2
2 sin
∆xθ
2
.
∆t
1
1
for all θ. Hence we have c2 ·
≤ , that is,
2
(∆x)2
2
∆t ≤
(∆x)2
.
2c2
Stability of the finite difference method for the wave equation
Recall the difference equation for the wave equation:
uk+1
= 2ukn − uk−1
+ r[ukn+1 − 2ukn + ukn−1 ],
n
n
(41)
where
r = c2 ·
(∆t)2
.
(∆x)2
Let ukn = M k e−in∆xθ be a solution to (41), then
M k+1 e−in∆xθ
=
2M k e−in∆xθ − M k−1 e−in∆xθ + r[M k e−i(n+1)∆xθ − 2M k e−in∆xθ + M k e−i(n−1)∆xθ ]
=
M k−1 e−in∆xθ [2M − 1 + rM e−i∆xθ − 2rM + rM ei∆xθ ]
=
M k−1 e−in∆xθ [2M − 1 − 2rM + 2rM cos(∆xθ)]
∆xθ
2
k−1 −in∆xθ
M
e
2M − 1 − 4rM sin
.
2
=
LECTURE 19: THE FINITE DIFFERENCE METHODS
13
So we get
2
2
M = 2M − 1 − 4rM sin
∆xθ
2
.
That is, we get
∆xθ
M 2 + 4r sin2
− 2 M + 1 = 0.
2
∆xθ
2
k
If we want un to be bounded, we must need |M | = 1. Let γ = 1 − 2r sin
, then M 2 − 2γM + 1 = 0, which
2
implies that
p
M = γ ± γ 2 − 1.
If M ’s are real, we must need γ = 1; if M ’s are complex, we must need γ 2 ≤ 1 and |M |2 = γ 2 + 1 − γ 2 = 1, that is,
1 − 2r sin2 ∆xθ ≤ 1.
2
So we get
r≤
1
sin2
It’s easy to see that r ≤ 1 for all θ. Hence we have c2 ·
∆xθ
2
2
.
(∆t)
≤ 1, that is,
(∆x)2
∆t ≤
∆x
,
c
which is called the Courant-Friedrichs-Lewy or CFL condition.
Department of Mathematics, The University of British Columbia, Room 121, 1984 Mathematics Road, Vancouver, B.C.
Canada V6T 1Z2
E-mail address: mingfeng@math.ubc.ca
Download