Finite difference method for heat equation Praveen. C praveen@math.tifrbng.res.in Tata Institute of Fundamental Research Center for Applicable Mathematics Bangalore 560065 http://math.tifrbng.res.in/~praveen January 25, 2013 1 / 46
Heat equation: Initial value problem Partial differential equation, µ > 0 Exact solution u t = µu xx, (x, t) R R + u(x, 0) = f(x), x R 1 + u(x, t) = e y2 /4µt f(x y)dy =: (E(t)f)(x) 4πµt Solution bounded in maximum norm u(t) C = E(t)f C f C = sup f(x) x R 2 / 46
Finite difference mesh in space and time: partition space R by a uniform mesh of size h and time R + by a uniform mesh of size t x = h, =..., 2, 1, 0, +1, +2,... t n = n t, n = 0, 1, 2,... t n t Let the numerical approximation be x x U n u(x, t n ) 3 / 46
Finite difference in space First derivative: D + x U n = U n +1 U n h Second derivative:, Dx U n = U n U 1 n, D 0 h xu n = U +1 n U 1 n 2h Finite difference in time D + x D x U n = U n 1 2U n + U n +1 h 2 D t + U n = U n+1 U n t 4 / 46
Finite difference scheme: forward time and central space (FTCS) i.e., D + t U n = µd + x D x U n, n = 0, 1, 2,... U n+1 U n t Update equation: Solve for U n+1 = µ U n 1 2U n + U n +1 h 2 U n+1 = (E h U n ) = λu n 1 + (1 2λ)U n + λu n +1 where Explicit scheme λ = µ t h 2 U n+1 = H(U n ) 5 / 46
Maximum principle If λ 1/2, then U n+1 is given by a convex linear combination of U 1 n, U n, U +1 n. Hence min(u n 1, U n, U n +1) U n+1 min Uk n U n+1 k max(u n 1, U n, U n +1) max Uk n, k Hence solution at any time level n is bounded by initial data Maximum norm min Uk 0 U n max Uk 0,, n k k U = sup U Maximum stability: If λ 1/2, then U n+1 = E h U n U n... U 0 and hence U n U 0 = f, n 6 / 46
Maximum principle We have E h V V Eh n V V λ 1/2 is necessary: Consider initial condition f = f(x ) = ( 1) ɛ, 0 < ɛ 1 so that f = ɛ After one time step U 1 = [λ( 1) 1 + (1 2λ)( 1) + λ( 1) +1 ]ɛ = (1 4λ)( 1) ɛ and after n time steps U n = (1 4λ) n ( 1) ɛ If λ > 1/2, then U n = 1 4λ n ɛ as n 7 / 46
Maximum principle Remark: The condition λ 1 2 depends on the space step h as t h2 2µ implies that the allowed time step t = t = O ( h 2) The scheme is said to be conditionally stable. 8 / 46
Truncation error and consistency If u(x, t) is exact solution then with u n = u(x, t n ), the local truncation error τ n := D t + u n µd x + Dx u n Using Taylor s formula, for some x (x 1, x +1 ) and t n I n = (t n, t n+1 ) τ n = 1 2 tu tt(x, t n ) 1 12 µh2 u xxxx ( x, t n ) Using the PDE, we get u tt = µ 2 u xxxx and from the exact solution, we have u xxxx C f xxxx C. The truncation error is bounded as τ n C t max t I n u tt (t) C + Ch 2 u xxxx (t n ) C Ch 2 max t I n u xxxx (t) C, since λ 1/2 Ch 2 f xxxx C 9 / 46
Error estimate Let U n and u n be the numerical and exact solutions, and let λ 1/2. Then there is a constant C such that Proof: Set e n = U n u n. Then U n u n Ct n h 2 f xxxx C, t n 0 D + t e n µd + x D x e n = τ n and hence Iterating over time e n+1 = (E h e n ) t τ n n 1 e n = (Eh n e 0 ) t n 1 = t l=0 l=0 (E n 1 l (E n 1 l h τ l ) h τ l ) since e 0 = U 0 u 0 = 0 10 / 46
Using the stability estimate and truncation error estimate n 1 e n t E n 1 l h τ l l=0 n 1 t τ l, l=0 C(n t)h 2 f xxxx C E n 1 l h τ l τ l Ch 2 f xxxx C = Ct n h 2 f xxxx C Remark: The FTCS scheme has first order accuracy in time and second order accuracy in space τ n = O ( t + h 2). Since t = O ( h 2) under the stability condition λ 1/2, the scheme has second order accuracy with respect to the mesh width h. If h is halved, then the error reduces by 1/4. But number of time steps increases by a factor of 4. 11 / 46
Symbol of E h FTCS scheme General finite difference scheme U n+1 = λu n 1 + (1 2λ)U n + λu n +1 U n+1 = p a p (λ)u n p How does scheme modify one Fourier mode: U n = gn e iξ g = amplitude g n+1 (ξ) = Ẽh(ξ)g n (ξ) ξ is called the wave number. Symbol or characteristic polynomial of E h Ẽ h (ξ) = p a p e ipξ, ξ R Useful in stability analysis: Fourier stability 12 / 46
Necessary condition for maximum stability A necessary condition for stability of the operator E h with respect to the discrete maximum norm is that Ẽh(ξ) 1, ξ R Proof: Assume that E h is stable in maximum norm and that Ẽh(ξ 0 ) > 1 for some ξ 0 R. Then with initial condition f = e iξ0 ɛ, the numerical solution after one time step is U 1 = p ( a p e i( p)ξ0 ɛ = a p e )e ipξ0 iξ0 ɛ = Ẽh(ξ 0 )f p By iterating in time, we get U n = [Ẽh(ξ 0 )] n f and U n = Ẽh(ξ 0 ) n ɛ as n Since f = ɛ, this contradicts the stability of the scheme and hence the theorem is proved. 13 / 46
FTCS scheme Symbol U n+1 = λu n 1 + (1 2λ)U n + λu n +1 Ẽ h (ξ) = λe iξ + (1 2λ)e 0 + λe iξ = 1 2λ + 2λ cos(ξ) Since cos(ξ) [ 1, +1], the maximum value is attained for cos(ξ) = 1 max Ẽh(ξ) = 1 4λ 1 = λ 1 ξ 2 which is the condition previously derived for maximum stability. 14 / 46
Discrete Fourier transform For any grid function V = {V } R define discrete l 2 norm V 2,h = h Space of bounded grid functions Discrete Fourier transform =+ = V 2 1/2 l 2,h = {V R : V 2,h < } V (ξ) = h + = V e iξ Inverse transform V = 1 +π V (ξ)e iξ dξ 2πh π 15 / 46
Discrete Fourier transform Parseval relation V 2 2,h = 1 2πh +π π V (ξ) 2 dξ = 1 2π +π/h π/h V (ξh) 2 dξ Definition: Stability in l 2,h The discrete solution operator E h is said to be stable in the norm 2,h if E h V 2,h V 2,h, V l 2,h 16 / 46
Von Neumann stability condition A necessary and sufficient condition for E h to be stable in l 2,h is given by Ẽh(ξ) 1, ξ R Proof: For any V l 2,h, Ê h V (ξ) = h (E h V ) e iξ = h = a p V p e iξ p = a p e ipξ h V p e i( p)ξ p = Ẽh(ξ) V (ξ) and iterating in time Ê n h V (ξ) = [Ẽh(ξ)] n V (ξ) 17 / 46
Using Parseval relation, stability of E h in l 2,h is equivalent to +π Ê n h V (ξ) 2 dξ = +π Ẽh(ξ) 2n V (ξ) 2 dξ +π π π π V (ξ) 2 dξ which holds if and only if Ẽh(ξ) n 1, n 0, ξ R Remark: If the problem is in the time interval (0, T ), then a less restrictive notion of stability is given by the condition E n h V C T V, 0 n T/ t If the finite difference scheme satisfies E h V (1 + K t) V, K 0 then this implies Eh n V (1 + K t) n V e Kn t V e KT V 18 / 46
Fourier transform in space Define numerical scheme at all x R, not ust at the mesh points U n+1 (x) = (E h U n )(x) = p a p (λ)u n (x x p ) Advantage: All the U n lie in same function space, L 2 (R) or C(R), independently of h. Define usual L 2 norm v = + v(x) 2 dx 1/2 Fourier transform + v(ξ) = v(x)e ixξ dx Then Parseval relation v 2 = 1 2π v 2 Ê h v(ξ) = p a p ( v( ph)(ξ) = p a p e iphξ ) v(ξ) = Ẽh(hξ) v(ξ) 19 / 46
For the numerical scheme, we thus find U n = 1 2π [ Ẽ h (hξ)] n Û 0 1 Stability in L 2 norm holds if 2π sup ξ R sup Ẽh(hξ) 1 ξ R Ẽh(hξ) n Û 0 = sup Ẽh(hξ) n U 0 ξ R Accuracy of order r The finite difference scheme E h is accurate of order r if Ẽ h (ξ) = e λξ2 + O ( ξ r+2), ξ 0 Example: FTCS scheme Ẽ h (ξ) = 1 2λ + 2λ cos(ξ) = 1 λξ 2 + 1 12 λξ4 + O ( ξ 6) ( 1 = e λξ2 + 12 λ 1 ) 2 λ2 ξ 4 + O ( ξ 6) FTCS is accurate of order 2; For the choice λ = 1 6 it is accurate of order 4. 20 / 46
Convergence in L 2 Assume that the scheme E h with λ = t h = constant and is accurate of order r 2 and stable in L 2. Then U n u n Ct n h r f r+2, t n 0 Proof: Since Ẽh(ξ) is bounded in R, we have from accuracy property Ẽh(ξ) e λξ2 C ξ r+2, ξ R By stability and a n b n = (a b)(a n 1 + a n 2 b +... + b n ) it follows that n 1 [Ẽh(ξ)] n e nλξ2 (Ẽh(ξ) e λξ 2 ) [Ẽh(ξ)] n 1 e λξ2 Cn ξ r+2 =0 Take Fourier transform of heat equation in x variable dû dt (ξ, t) = ξ2 û(ξ, t), t > 0, û(ξ, 0) = f(ξ) 21 / 46
so that û(ξ, t) = e ξ2 t f(ξ) Fourier transform of error in numerical solution is From Parseval relation But U n u n 2 = 1 2π and using the fact that (Û n û n )(ξ) = ([Ẽh(hξ)] n e n tξ2 ) f(ξ) Û n û n 2 = 1 2π ([Ẽh(hξ)] n e n tξ2 ) f(ξ) [Ẽh(hξ)] n e n tξ2 Cnh r+2 ξ r+2 2 df = iξ f(ξ), dx d r f dx = ( iξ)r f(ξ), λ = t h 2 = const 22 / 46
we get U n u n 1 Cnh r+2 ξ r+2 f(ξ) 2π C(n t)h r f (r+2), h 2 = O ( t) = Ct n h r f r+2 23 / 46
Multi-level scheme Higher order accuracy in time D 0 t U n = µd + x D x U n or U n+1 U n 1 = µ U n 1 2U n + U n +1 h 2 2 t Formally second order in t and x, but unstable Dufort-Frankel scheme: stable for any λ U n+1 U n 1 2 t ( U 1 n 2 = µ U n+1 +U n 1 2 h 2 ) + U n +1 Note: You need to get U 1 by some other scheme. Second order accurate (need t/h 0, ok if t/h 2 =constant) τh n = O ( t 2) + O ( h 2) ( ) + t2 t 4 h 2 u tt(x, t n ) + O h 2 24 / 46
Initial-boundary value problem u t = µu xx, (x, t) (0, 1) R + u(x, 0) = f(x) u(0, t) = 0 u(1, t) = 0 25 / 46
IBVP: Forward Euler scheme Consider a uniform grid of M + 1 points, indexed from 0 to M. D + t U n = µd + x D x U n, = 1,..., M 1 or or U n+1 U n t = U n 1 2U n + U n +1 h 2 U n+1 = λu n 1 + (1 2λ)U n + λu n +1, = 1,..., M 1 U n+1 0 = U n+1 M = 0 Maximum norm U,h = max U 0 M 26 / 46
IBVP: Forward Euler scheme Maximum stability The forward Euler scheme for the IBVP is stable in maximum norm iff λ 1/2. λ 1/2 is necessary: Consider initial condition U 0 = f = ( 1) sin(πh), = 0, 1,..., M so that f,h = 1. Then U n = [1 2λ 2λ cos(πh)] n f, = 0, 1,..., M If λ > 1/2, then for sufficiently small h 1 2λ 2λ cos(πh) γ > 1 which implies U n,h γ n f,h = γ n, h 0 27 / 46
IBVP: Forward Euler scheme Local truncation error: (for any fixed λ) τ n Ch 2 max t I n u xxxx (t) C, I n = (t n, t n+1 ) Error estimate If λ 1/2, then the FTCS scheme for IBVP has error estimate given by U n u n,h Ct n h 2 max t t n u xxxx (t) C 28 / 46
Numerical example: FTCS (forward Euler) PDE u t = µu xx x (0, 1) Initial condition u(x, 0) = f(x) = sin(πx) Exact solution u(x, t) = e µπ2t sin(πx) Try with λ = 0.5, 0.6 Matlab code: heat 1d fe.m 29 / 46
Implicit scheme for IBVP: Backward Euler Explicit scheme (Forward Euler/FTCS) D + t U n = µd + x D x U n Implicit scheme (Backward Euler/BTCS) D + t U n = µd + x D x U n+1 or U n+1 U n t = µ U n+1 1 n+1 2U + U n+1 +1 h 2 λu n+1 1 + (1 + 2λ)U n+1 λu n+1 +1 = U n, = 1,..., M 1 U n+1 0 = U n+1 M = 0 In matrix notation where U = [U 1, U 2,..., U M 1 ] and BU n+1 = U n 30 / 46
where Implicit Ū n+1 and scheme Ū n are now thought of as vectors with M 1components corresponding to the for interior IBVP: mesh-points Backward and B is the Euler diagonally dominant, symmetric, tridiagonal matrix 1+2λ λ 0... 0. λ 1+2λ λ... B =. 0........ 0..... λ 1+2λ λ 0... 0 λ 1+2λ Clearly the system (9.22) may easily be solved for Ū n+1. Maximum stability The backward Euler scheme is stable in maximum norm for any value of λ. The scheme is unconditionally stable. Proof: At time level n + 1, let the maximum be achieved at grid point = 0. Then U n+1 0 = 1 [ λu n+1 n+1 1 + 2λ 0 1 + λu + U n ] 0+1 0 31 / 46
Implicit scheme for IBVP: Backward Euler and U n+1,h = U n+1 0 1 1 + 2λ 2λ 1 + 2λ [ λ U n+1 n+1 0 1 + λ U + U n 0+1 0 ] U n+1,h + 1 1 + 2λ U n,h which implies that U n+1,h U n,h. Remark: If we write BU n+1 = U as U n+1 = B 1 U n =: E U n then maximum norm stability implies that E U,h U,h = E,h 1 32 / 46
Implicit scheme for IBVP: Backward Euler Error estimate The backward Euler scheme has the error estimate U n u n,h Ct n ( t + h 2 ) max t t n u xxxx (t) C Proof: Define the error e n = U n u n. U n+1 U n t u n t u n+1 = µ U n+1 1 The local truncation error can be bounded as n+1 2U + U n+1 +1 h 2 = µ un+1 1 2un+1 + u n+1 +1 h 2 + τ n τ n,h C( t + h 2 ) max t I n 1 u(t) C 4, I n 1 = (t n 1, t n ) 33 / 46
Implicit scheme for IBVP: Backward Euler Then the error satisfies e n+1 e n t = µ en+1 1 2en+1 + e n+1 +1 h 2 τ n In terms of matrix B and discrete evolution operator E Iterating over time Be n+1 = e n t τ n = e n+1 = E e n te τ n n 1 e n = E e n 0 t E n l τ l, n 1 l=0 n 1 = t E n l τ l since e 0 = 0 l=0 34 / 46
Implicit scheme for IBVP: Backward Euler Therefore n 1 e n,h t E n l τ l,h l=0 n 1 t τ l,h, l=0 (n t) max 0 l n 1 τ l,h t n C( t + h 2 ) max t t n u(t) C 4 from stability of E Remark: The Backward Euler scheme is first order accurate in time and second order accurate in space. 35 / 46
Implicit scheme for IBVP: Crank-Nicholson D + t U n = µd + x D x ( ) U n + U n+1 2 λ 2 U n+1 1 + (1 + λ)u n+1 λ 2 U n+1 +1 = λ 2 U n 1 + (1 λ)u n + λ 2 U n +1 U n+1 0 = U n+1 M = 0 In matrix notation where BU n+1 = AU n 36 / 46
BŪ n+1 = AŪ n, Implicit scheme for IBVP: Crank-Nicholson both A and B are symmetric tridiagonal matrices, with9.2 B diagoant: The Mixed Initial-Boundary Value 1+λ 1 2 λ 0 and... 0 1 λ 1 2 1 2 λ 1+λ 1 2 λ...... λ 0... 0 1 B =. 0........ 2 0, λ 1 λ 1 2 λ.... A =..... 1 2 λ 1+λ 1 2 λ 0........ 0..... 1 0... 0 1 2 λ 1+λ 2 λ 1 λ 1 2 λ 1 0... 0 2 λ 1 λ With obvious notation we also have U n+1 = B 1 AU n =: E U n B kh U n+1 = A kh U n, or Maximum stability U n+1 = B 1 kh A khu n = E k U n, The Crank-Nicholson scheme where, is stable similarly into maximum the above, norm for λ 1 Proof: Similar to backward Euler scheme B 1 kh V,h V,h. Remark: Backward Euler scheme The same wasapproach unconditionally to stability stable as forinthe maximum backward Euler norm. for λ 1, since the coefficients on the right are then non-neg (1 + λ) U n+1,h λ U n+1,h + U n,h, 37 / 46
Implicit scheme for IBVP: Crank-Nicholson Remark: If λ > 1, then U n+1,h (2λ 1) U n,h But since 2λ 1 > 1, this does not yield stability in maximum norm. Error estimate For λ 1, the Crank-Nicholson scheme has the error estimate U n u n,h = O ( t 2 + h 2) Remark: The condition λ 1 is too restrictive. This can be relaxed if we use a weaker norm for stability. 38 / 46
IBVP: Fourier stability Vector V = (V 0, V 1,..., V M ) R M+1 Inner product and corresponding norm Space of vectors (V, W ) h := h M V W =0 V 2,h = (V, V ) h l 0 2,h = {V R M+1 : V 2,h <, V 0 = V M = 0} Orthonormal basis vectors for l 0 2,h : φ p R M+1, p = 1,..., M 1 φ p, = 2 sin(πph), = 0, 1,..., M (φ p, φ q ) h = δ pq φ p are eigenfunctions of finite difference operator D + x D x D + x D x φ p, = 2 h 2 [1 cos(πph)]φ p,, = 1, 2,..., M 1 39 / 46
IBVP: Fourier stability Let f l2,h 0 be initial data. Then f = M 1 p=1 ˆf p φ p, ˆfp = (f, φ p ) h Parseval relation f 2,h = ˆf 2 Forward Euler method (FTCS): Consider one time step U 1 = f + µ td + x D x f = M 1 p=1 ˆf p [1 2λ(1 cos(πph))]φ p,, = 1, 2,..., M 1 and U 1 0 = U 1 M = 0 40 / 46
IBVP: Fourier stability or more generally U n = M 1 p=1 ˆf p [Ẽh(πph)] n φ p,, = 0, 1,..., M where Ẽh(ξ) is the symbol of the operator E h Ẽ h (ξ) = 1 2λ[1 cos(ξ)] By orthogonality of {φ p } and Parseval s relation U n 2,h = ( M 1 p=1 ) 1/2 ˆf p 2 [Ẽh(πph)] 2n max Ẽh(πph) n f p 2,h Check that Ẽh(ξ) 1 for ξ [0, π] iff λ 1/2. In that case, we have stability U n 2,h f 2,h, iff λ 1/2 41 / 46
IBVP: Fourier stability This condition is identical to the condition for maximum norm stability of FTCS scheme. Backward Euler method Ẽ h (ξ) = 1 1 + 2λ[1 cos(ξ)] and 0 Ẽh(ξ) 1 for all ξ [0, π] and any λ > 0. Hence we have unconditional stability. Crank-Nicholson method Ẽ h (ξ) = 1 λ[1 cos(ξ)] 1 + λ[1 cos(ξ)] and Ẽh(ξ) 1 for ξ [0, π] and for any λ > 0. Thus the CN scheme is unconditionally stable in l 0 2,h. 42 / 46
IBVP: Fourier stability Convergence of CN scheme in l 0 2,h Let U n and u n be numerical and exact solutions. Then for any λ > 0 U n u n 2,h Ct n (h 2 + t 2 ) max t t n u(t) C 6 Proof: The local truncation error ( ) u n τ n = D t + u n µd x + Dx + u n+1 2 = [ D + t u n u t (x, t n+ 1 2 ) ] µd + x D x [ ] µ D x + Dx u n+ 1 2 u xx (x, t n+ 1 ) 2 and using Taylor formula we get [ u n + u n+1 2 τ n 2,h C( t 2 + h 2 ) max t I n u(t) C 6 u n+ 1 2 ] 43 / 46
IBVP: Fourier stability The error e = U n u n satisfies the difference equation e n+1 = E e n t B 1 τ n or n 1 e n = t E n 1 l B 1 τ l l=0 The result follows from the stability of E and the boundedness of B 1, i.e., E V 2,h V 2,h and B 1 V 2,h V 2,h 44 / 46
θ-scheme Symbol of the scheme D + t U n = µd + x D x [(1 θ)u n + θu n+1 ] θ = 0 Forward Euler Explicit O ( t + h 2) θ = 1 Backward Euler Implicit O ( t + h 2) θ = 1/2 Crank-Nicholson Implicit O ( t 2 + h 2) Ẽ h (ξ) = 1 2(1 θ)λ(1 cos ξ) 1 + 2θλ(1 cos ξ) For 0 θ 1, we have Ẽh(ξ) 1 for all ξ. Stability requires that 1 4(1 θ)λ min Ẽ h (ξ) = 1 = (1 2θ)λ 1 ξ 1 + 4θλ 2 θ < 1/2: stable in l 0 2,h only if λ < 1 2(1 2θ) θ 1/2: unconditionally stable in l 0 2,h 45 / 46
Numerical example Initial and boundary condition same as in (29) Matlab codes: Backward euler scheme: heat 1d be.m Crank-Nicholson scheme: heat 1d cn.m 46 / 46