FTCS Soluton to the Heat Equaton ME 448/548 Notes Gerald Recktenwald Portland State Unversty Department of Mechancal Engneerng gerry@pdx.edu ME 448/548: FTCS Soluton to the Heat Equaton
Overvew 1. Use the forward fnte dfference approxmaton to u/ t. u t uk+1 u k t 2. Use the central dfference approxmaton to 2 u/ x 2 at tme t k. 2 u = uk 1 2uk + uk +1 x 2 x 2 x 3. Solve for u k+1. The computatonal formula s explct: each value of u k+1 updated ndependently. 4. FTCS s easy to mplement, but s only condtonally stable 5. Truncaton errors are O ( ( x) 2) and O( t). can be ME 448/548: FTCS Soluton to the Heat Equaton page 1
Fnte Dfference Operators Choose the forward dfference to evaluate the tme dervatve at t = t k. u t = uk+1 u k + O( t) (1) tk,x t Approxmate the spatal dervatve wth the central dfference operator and take all nodal values at tme t k. 2 u = uk 1 2uk + uk +1 + O( x 2 ) (2) x 2 x 2 x ME 448/548: FTCS Soluton to the Heat Equaton page 2
FTCS Approxmaton to the Heat Equaton Substtute Equaton (1) and Equaton (2) nto the heat equaton u k+1 u k t = α uk 1 2uk + uk +1 x 2 + O( t) + O( x 2 ) (3) Drop truncaton error terms to get u k+1 u k t = α uk 1 2uk + uk +1 x 2 (4) ME 448/548: FTCS Soluton to the Heat Equaton page 3
FTCS Computatonal Molecule t k+1 k k 1....... FTCS scheme enables explct calculaton of u at ths node Soluton s known for these nodes t=0, k=1 =1 1 +1 n x x=0 x=l ME 448/548: FTCS Soluton to the Heat Equaton page 4
FTCS Approxmaton to the Heat Equaton Solve Equaton (4) for u k+1 u k+1 = ru k +1 + (1 2r)uk + ruk 1 (5) where r = α t/ x 2. FTCS s an explct scheme because t provdes a smple formula to update u k+1 ndependently of the other nodal values at t k+1. t....... k+1 k k 1 t=0, k=1 =1 1 +1 n x x=0 x=l ME 448/548: FTCS Soluton to the Heat Equaton page 5
demoftcs Code functon errout = demoftcs(nx,nt) %... Comments and processng of optonal nputs skpped % --- Assgn physcal and mesh parameters alfa = 0.1; L = 1; tmax = 2; % Dffuson coeffcent, doman length and max tme dx = L/(nx-1); dt = tmax/(nt-1); r = alfa*dt/dx^2; r2 = 1-2*r; % --- Assgn IC and BC. u s ntalzed to a vector that ncludes BC x = lnspace(0,l,nx) ; u = sn(p*x/l); % --- Loop over tme steps for k=2:nt uold = u; % prepare for next step for =2:nx-1 u() = r*uold(-1) + r2*uold() + r*uold(+1); end end Remember that the formula for updatng u k+1 s u k+1 = ru k +1 + (1 2r)uk + ruk 1 ME 448/548: FTCS Soluton to the Heat Equaton page 6
Alternatve formulaton to the FTCS Algorthm Equaton (5) can be expressed as a matrx multplcaton. u (k+1) = Au (k) (6) where u (k+1) s the vector of u values at tme step k + 1, u (k) s the vector of u values at tme step k, and A s the trdagonal matrx 1 0 0 0 0 0 r (1 2r) r 0 0 0 0 r (1 2r) r 0 0 A = 0 0......... (7). 0 0 0 0 r (1 2r) r 0 0 0 0 0 1 The frst and last rows of A are set to enforce the Drchlet boundary condtons at x = 0 and x = L. ME 448/548: FTCS Soluton to the Heat Equaton page 7
Run the demoftcs code >> demoftcs Error n FTCS soluton = 0.002221 0.14 0.12 FTCS Exact 0.1 0.08 u 0.06 0.04 0.02 0 0 0.2 0.4 0.6 0.8 1 x ME 448/548: FTCS Soluton to the Heat Equaton page 8
Unstable FTCS Soluton moveftcs(15): 2 1.5 t = 0.41 Intal Condton FTCS soluton 2 1.5 t = 1.63 Intal Condton FTCS soluton 2 1.5 t = 1.96 Intal Condton FTCS soluton 1 1 1 0.5 0.5 0.5 0 0 0 0.5 0.5 0.5 1 0 0.2 0.4 0.6 0.8 1 1 0 0.2 0.4 0.6 0.8 1 1 0 0.2 0.4 0.6 0.8 1 2 1.5 t = 0.82 Intal Condton FTCS soluton 2 1.5 t = 1.88 Intal Condton FTCS soluton 2 1.5 t = 2.00 Intal Condton FTCS soluton 1 1 1 0.5 0.5 0.5 0 0 0 0.5 0.5 0.5 1 0 0.2 0.4 0.6 0.8 1 1 0 0.2 0.4 0.6 0.8 1 1 0 0.2 0.4 0.6 0.8 1 2 1.5 t = 1.22 Intal Condton FTCS soluton 2 1.5 t = 1.92 Intal Condton FTCS soluton 2 1.5 t = 2.04 Intal Condton FTCS soluton 1 1 1 0.5 0.5 0.5 0 0 0 0.5 0.5 0.5 1 0 0.2 0.4 0.6 0.8 1 1 0 0.2 0.4 0.6 0.8 1 1 0 0.2 0.4 0.6 0.8 1 ME 448/548: FTCS Soluton to the Heat Equaton page 9
Condtonally Stable FTCS Scheme Observatons: The FTCS soluton appears to be stable at frst Toward the end of the smulaton tme, oscllatons grow exponentally Instablty s not caused by truncaton error Instablty s fed by round-off errors, but not drectly caused by round-off We ll use a smplfed form of Fourer Stablty Analyss Suppose that the ntal condton s a small sne wave The correct soluton s a decay of the sne wave Under what condton does the soluton grow nstead of decay?
Stablty Analyss Suppose the ntal condton looks lke ths ( ) πx u 0 (x) = σ cos x = σ( 1) 1 where σ s a sutably small value The soluton at tme step k = 2 s u (2) = r ( u 0 (x +1 ) + u 0 (x 1 ) ) + (1 2r)u 0 (x ) = rσ ( ( 1) + ( 1) 2) + (1 2r)σ( 1) 1 = rσ ( ( 1) +1 + ( 1) 1) + (1 2r)σ( 1) 1 ME 448/548: FTCS Soluton to the Heat Equaton page 11
Stablty Analyss Snce ( 1) +1 = ( 1) 1 for any, the soluton at k = 2 can be further smplfed u (2) = 2rσ( 1) 1 + (1 2r)σ( 1) 1 = (1 4r)σ( 1) 1 = (1 4r)u 0 (x ) The pattern s u (2) = (1 4r)u 0 (x ) u (3) = (1 4r)u (2) (x ) = (1 4r) 2 u 0 (x ) u (4) = (1 4r)u (3) (x ) = (1 4r) 3 u 0 (x ) where u (2) s the numercal soluton at x and k = 2; and where (1 4r) 2 s the square of (1 4r) ME 448/548: FTCS Soluton to the Heat Equaton page 12
Stablty Analyss The general pattern s u k = (1 4r)k 1 u 0 (x ). where (1 4r) k 1 s (1 4r) rased to the k 1 power. Therefore, the soluton grows when 1 4r > 1. Therefore, stablty requres 1 4r < 1 and (1 4r) < 1. 1 4r < 1 = 4r < 2 = r > 1 2 true for any r > 0. The second case s (1 4r) < 1 = 4r < 2 = r < 1 2 ME 448/548: FTCS Soluton to the Heat Equaton page 13
Stablty Analyss The quck and drty stablty analyss shows that the FTCS scheme s stable only f r = α t x 2 < 1 2. (8) Recall: α s a parameter of the physcal problem. We must choose t and x so that Equaton (8) s satsfed. ME 448/548: FTCS Soluton to the Heat Equaton page 14
Workng wth the FTCS Stablty Crteron To ncrease accuracy, we want to decrease both x and t. For a gven x, the stablty lmt for FTCS mposes an upper lmt on t t < x2 2α (9) Choosng t and x so that r < 1 2 does not guarantee an accurate numercal soluton. r < 1 2 only guarantees that the FTCS soluton wll not blow up. ME 448/548: FTCS Soluton to the Heat Equaton page 15
Workng wth the FTCS Stablty Crteron When reducng x and t to mprove accuracy, follow a path lke A C not A B. Note that the axes have logarthmc scales. log( t) Unstable B C A Stable ( x) t = 2 2α log( x) ME 448/548: FTCS Soluton to the Heat Equaton page 16
Measurng the FTCS Truncaton Error The error reported by demoftcs s defned as E(n x, n t ) = 1 nx u k u(x, t k ) 2 (10) The factor of 1/ n x converts u k u(x, t k ) 2 to an average error. ME 448/548: FTCS Soluton to the Heat Equaton page 17
Measurng the FTCS Truncaton Error Desgnate the local error at x = x and t = t k as e k = uk u(x, t k ). (11) Defne ē k as an RMS average error per node at tme step t k ē k [ 1 n x nx =1 (e k )2 ] 1/2 (12) Algebrac substtuton shows that E(n x, n t ) = ē k. If the soluton to the heat equaton s smooth, then E(n x, n t ) = O( t) + O( x 2 ) ME 448/548: FTCS Soluton to the Heat Equaton page 18
Measurng the truncaton error Systematcally reduce x and t to determne whether truncaton error predcton s realzed by the code. We know the exact soluton, so we use Equaton(10).
Measurng the truncaton error Suppose we don t know the exponent of the truncaton error for our code. In other words, nstead of we have where p s uknown E(n x, n t ) = O( x 2 ) E(n x, n t ) = O( x p ) Snce x = L n x 1 we have ( E(n x, n t ) = O( x p L p ) ) = O (n x 1) p ME 448/548: FTCS Soluton to the Heat Equaton page 20
Measurng the truncaton error Smplfy the precedng expresson E(n x, n t ) = O( x p ) ( L p ) = O (n x 1) p ( ) 1 O gnore multplcatve constants (n x 1) p ( 1 O n p x ) leadng powers domnate Therefore, we expect ( 1 E(n x, n t ) = O n p x ) ME 448/548: FTCS Soluton to the Heat Equaton page 21
Measurng the truncaton error Measure E(n x, n t ) on two dfference meshes, n x,1 and n x,2. Wth we form the rato Solve for p ( 1 E(n x, n t ) = O E(n x,2, n t ) E(n x,1, n t ) = n p x,1 n p x,2 = n p x ) ( ) p nx,1 n x,2 p = log (E(n x,2, n t )/E(n x,1, n t )). log (n x,1 /n x,2 ) ME 448/548: FTCS Soluton to the Heat Equaton page 22
convftcs Code functon convftcs % convftcs Convergence of FTCS on a seres of fner spatal meshes % --- Set constants to be consstent wth demoftcs alfa = 0.1; L = 1; tmax=2; rsafe = 0.49999; % stable r<0.5 % --- Specfy nx and compute nt consstent wth stablty lmt nx = [8 16 32 64 128 256]; dx = L./(nx-1); nt = cel( 1 + alfa*tmax*((nx-1).^2)/(rsafe*l^2) ); % --- Loop over mesh szes, store error and compute order of scheme fprntf( \n nx nt error E(j)/E(j-1) p\n ); er = NaN; p = 0; for j=1:length(nx); e(j) = demoftcs(nx(j),nt(j)); f j>1 er = e(j)/e(j-1); p = log(er)/log(nx(j-1)/nx(j)); end fprntf( %5d %5d %11.3e %8.4f %8.4f\n,nx(j),nt(j),e(j),er,p); end % -- plottng code skpped... ME 448/548: FTCS Soluton to the Heat Equaton page 23
Runnng the convftcs Code >> convftcs 10 2 FTCS deal nx nt error E(j)/E(j-1) p 8 21 6.028e-03 NaN 0.0000 16 92 1.356e-03 0.2249 2.1524 32 386 3.262e-04 0.2406 2.0553 64 1589 7.972e-05 0.2444 2.0329 128 6453 1.970e-05 0.2471 2.0170 256 26012 4.895e-06 0.2485 2.0085 The last column of text output shows that demoftcs has the rght truncaton error behavor. E(n x,n y ) 10 3 10 4 10 5 10 6 10 3 10 2 10 1 10 0 x ME 448/548: FTCS Soluton to the Heat Equaton page 24
Summary for the FTCS Scheme FTCS s easy to mplement. The update formula s u k+1 = ru k +1 + (1 2r)uk + ruk 1 The FTCS scheme s condtonally stable when r = α t x 2 < 1/2 In two spatal dmensons wth y = x, the stablty condton s r < 1/4. In 3D wth y = z = x, the stablty condton s r < 1/8. There are much better schemes for solvng the heat equaton. FTCS s a toy used to ntroduce the numercal soluton of PDEs ME 448/548: FTCS Soluton to the Heat Equaton page 25