Itroductory lecture otes o Partial Differetial Equatios - c Athoy Peirce. Not to be copied, used, or revised without explicit writte permissio from the copyright ower. 1 Lecture 8: Solvig the Heat, Laplace ad Wave equatios usig fiite differece methods (Compiled 6 Jauary 018) I this lecture we itroduce the fiite differece method that is widely used for approximatig PDEs usig the computer. We use the defiitio of the derivative ad Taylor series to derive fiite differece approximatios to the first ad secod derivatives of a fuctio. We the use these fiite differece quotiets to approximate the derivatives i the heat equatio ad to derive a fiite differece approximatio to the heat equatio. Similarly, the techique is applied to the wave equatio ad Laplace s Equatio. The techique is illustrated usig EXCEL spreadsheets. Key Cocepts: Fiite Differece Approximatios to derivatives, The Fiite Differece Method, The Heat Equatio, The Wave Equatio, Laplace s Equatio. 8 Fiite Differece Methods 8.1 Approximatig the Derivatives of a Fuctio by Fiite Differeces Recall that the derivative of a fuctio was defied by taig the limit of a differece quotiet: f f(x + ) f(x) (x) = lim 0 Now to use the computer to solve differetial equatios we go i the opposite directio - we replace derivatives by appropriate differece quotiets. If we assume that the fuctio ca be differetiated may times the Taylor s Theorem is a very useful device to determie the appropriate differece quotiet to use. For example, cosider f(x + ) = f(x) + f (x) +! Re-arragig terms i (8.) ad dividig by we obtai f(x + ) f(x) f (x) + 3 3! = f (x) + f (x) + f (3) (x) +.... 3! (8.1) f (3) (x) + 4 f (4) (x) +... (8.) 4! If we tae the limit 0 the we recover (8.1). But for our purposes it is more useful to retai the approximatio f(x + ) f(x) = f (x) + f (ξ) (8.3) = f (x) + O(). (8.4) We retai the term f (ξ) i (8.4) as a measure of the error ivolved whe we approximate f (x) by the differece quotiet ( f(x + ) f(x) ) /. Notice that this error depeds o how large f is i the iterval [x, x + ] (i.e. o the smoothess of f) ad o the size of. Sice we lie to focus o that part of the error we ca cotrol we say that the error term is of the order deoted by O(). Techically a term or fuctio E() is O() if E() 0 cost.
Now the differece quotiet (8.4) is ot the oly oe that ca be used to approximate f (x). Ideed, if we cosider the expasio of f(x ): f(x ) = f(x) f (x) +! f (x) 3 3! ad we subtract (8.5) from (8.), ad divide by () we obtai: f(x + ) f(x ) f (3) (x) + 4 f (4) (x) +... (8.5) 4! = f (x) + f (3) (ξ) (8.6) 3! We otice that the error term associated with this form of differece approximatio is O( ), which coverges more rapidly to zero as 0. I order to obtai a approximatio to f (x) we add (8.5) to (8.), which upo re-arragemet ad dividig by leads to: f(x + ) f(x) + f(x ) = f (x) + 1 1 f (4) (ξ) (8.7) Due to the symmetry of the differece approximatios (8.6) ad (8.7) about the expasio poit x these are called cetral differece approximatios. The differece approximatio (8.4) is ow as a forward differece approximatio. We ote that the cetral differece schemes (8.6) ad (8.7) are secod order accurate while the forward differece scheme (8.4) is oly accurate to O(). 8. Solvig the heat equatios usig the Method of Fiite Differeces Cosider the followig iitial-boudary value problem for the heat equatio u t = α u x 0 < x < 1, t > 0 (8.8) BC: u(0, t) = 0 u(1, t) = 0 (8.9) IC: u(x, 0) = f(x). (8.10) The basic idea is to replace the derivatives i the heat equatio by differece quotiets. We cosider the relatioships betwee u at (x, t) ad its eighbours a distace apart ad at a time t later. Correspodig to the differece quotiet approximatios itroduced i Sectio 1, we cosider the followig partial differece approximatios. Forward Differece i Time: After re-arragemet ad divisio by t: Cetral Differeces i Space: u(x, t + t) = u(x, t) + t u t u (x, t) + (x, t) +. t! t u(x, t + t) u(x, t) t u(x +, t) = u(x, t) + u (x, t) + x! u(x, t) = u(x, t) u (x, t) + x! = u (u, t) + O( t) (8.11) t u 3 3 u 4 4 u (u, t) + (x, t) + (x, t) + x 3! x3 4! x u 3 3 u 4 4 u (x, t) (x, t) + (x, t) +. x 3! x3 4! x4
Addig ad re-arragig: Fiite Differece Methods 3 u(x +, t) u(x, t) + u(x, t) Substitutig (8.11) ad (8.1) ito (8.8) we obtai Re-arragig: u(x, t + t) u(x, t) t = α ( u(x +, t) u(x, t) + u(x, t) = u x (x, t) + O( ) (8.1) ) + O( t, ). ( ) t u(x, t + t) = u(x, t) + α {u(x +, t) u(x, t) + u(x, t)}. (8.13) We subdivide the spatial iterval [0, 1] ito N + 1 equally spaced sample poits x =. The time iterval [0, T ] is subdivided ito M + 1 equal time levels t = t. At each of these space-time sample poits we itroduce approximatios: t +1 t 0 u(x, t ) u. N t u 0 u 1 u u +1 u N 1 u N ( ) t (u = u + α +1 u + u ) 1 This is implemeted i the spread sheets Heat0 ad Heat. Implemetig Derivative Boudary Coditios: Assume that the boudary coditios (8.9) are chaged to BC: u(0, t) = 0, u (1, t) = 0. x Cosider a cetral differece approximatio to u x (1, t), where x N = N = 1, Re-arragig we obtai: u(x N +, t) u(x N, t) = 0. u(x N +, t) = u(x N, t) ( ) Sice x N = 1 we observe that x N + is outside the domai. To accomodate this we itroduce a extra colum u N+1 ito which we copy the values u N 1. I the colum x N we implemet the same differece approximatio for the Heat Equatio, amely: N = u N + α ( t )(u N+1 u N + u N 1) ( ) This is implemeted i the spread sheet Heat0f.
4 while u N+1 = u N 1 (see (*) ) sice colum u N 1 is copied to colum u N+1. Note that this BC could be implemeted aother way without itroducig the additioal colum, by elimiatig u N+1 from ( ) ad ( ): N = u N + α ( t ) (u N 1 u N). If this latter equatio is implemeted at x N there is o eed to itroduce a extra colum u N+1 or to implemet the differece equatio give i (**) as the the derivative boudary coditio is tae care of automatically. Stability of the Fiite Differece Scheme for the heat equatio Cosider the followig fiite differece approximatio to the 1D heat equatio: Let u = ϕ e iθ the Therefore u = t ( u +1 u + u ) 1 where u u(x, t ) (ϕ +1 ϕ )e iθ = t ( e iθ + e iθ) ϕ e iθ ϕ +1 = ϕ t ( ) θ 4 si [ = 1 4 t ( θ si = t [ cos(θ) ] ϕ e iθ )] ϕ ( ) θ ϕ sice cos(θ) 1 = si Now for stability we require that ϕ +1 ϕ so that 1 4 t ( ) θ si 1 4 t ( ) θ si 0 The right iequality is satisfied automatically, while the left iequality ca be re-writte i the form: 4 t si ( ) θ Sice si ( ) 1 this coditio is satisfied for all θ provided t (8.14)
Exercises o fiite Differeces applied to the Heat Equatio Fiite Differece Methods 5 Exercise 1 Numerical Istability: (a) Chage the t i cell D1 from 0.001 to 0.05 ad you will observe what is ow as a umerical istability. Now chage t to 0.0065, which is ow as the stability boudary predicted by (8.14) ad observe what happes. Now let t = 0.006 ad observe the abrupt chage i the solutio - it is much closer to what we would expect. (b) Derive the stability coditio for the fiite differece approximatio of the 1D heat equatio whe α 1. i.e. u = α t ( u +1 u + u ) 1 Exercise Trucatio Error: The istability oted i 1 above is ot the oly source of error i the umerical approximatio. Although umerical istability is evidet for a parameter choice that is ustable, the other type of error is preset i almost every type of umerical approximatio scheme. This class of error results from discardig the O( ) ad O( t) terms i (8.6) ad (8.4) whe we replace derivatives i (8.8) by differece quotiets. This error is ow as the trucatio error. To estimate the magitude of the trucatio error, chage the spread sheet to implemet the iitial coditio f(x) = { x 0 < x < 1/ (1 x) 1/ x < 1. Now code up the Fourier Series (i aother spread sheet) that is derived i Lecture 10, Exercise 10.1 ad compare the umerical solutio to the exact Fourier Series solutio with 50 terms. The differece betwee the two is maily due to the trucatio error sice the roud-off error is about 10 1 ad does ot grow if stable parameters are used. Exercise 3 Derivative Boudary Coditios: Implemet a derivative boudary coditio the left edpoit x = 0. Chec the umerical solutio agaist the problem solved i Lecture 11.
6 8.3 Fiite Differece scheme for the 1D Wave Equatio Cosider the followig iitial boudary value problem for the Wave Equatio: u tt = c u xx 0 < x < L (8.15) BC: u(0, t) = 0 u(l, t) = 0 (8.16) IC: u(x, 0) = f(x) (8.17) u (x, 0) = g(x) t (8.18) We itroduce a fiite differece mesh x = t, t = t ad let the correspodig odal values be deoted by u u(x, t ). t t +1 t t, + 1 t 1 1,, + 1,, 1 x Now approximatig derivatives by cetral differeces both i space ad time we obtai u + u 1 ( u t = c +1 u + u 1 Therefore }{{} time level + 1 = u u 1 + ( c t ) + O(, t ). (8.19) ) ( u +1 u + u ) 1 = r u +1 + (1 r )u + r u 1 }{{} time level u 1 }{{} time level 1 (8.0) Here r = (c t/) is ow as the Courat Number. We observe that the Discrete Equatio (8.0) ivolves three distict levels i which ow data is trasferred from steps 1 ad to step + 1. Iitial Coditios - Startig the Solutio The 3-level scheme poses some challeges whe imposig the iitial coditios. If we imagie a row of false mesh poits at time t = t = t 1, the the iitial velocity coditio (8.18) ca be approximated usig cetral differeces as: therefore u 1 u 1 t = g(x ) (8.1) u 1 = u 1 tg(x ) (8.) Now we assume that the Discrete Wave Equatio (8.0) also holds at t = 0 so that u 1 = r u 0 +1 + (1 r )u 0 + r u 0 1 u 1 (8.3)
Fiite Differece Methods 7 Substitutig (8.) ito (8.3) ad re-arragig we obtai: u 1 = 1 (r u 0 +1 + (1 r )u 0 + r u 0 1) + tg(x ) (8.4) Sice u 0 = f(x ) ad g(x ) are ow, we are ow i a positio to specify the first two rows of odes. This is sufficiet to start the recursio (8.0) for all subsequet steps. u (use 8.0) t = t u (use 8.4) t 1 = t f 0 f 1 f 1 f f +1 t 0 = 0 x 0 x 1 x 1 x x +1 x N = odes = false mesh poits used to derive (8.4) but ot actually used i the computatio. Stability of the Fiite Differece Scheme for the wave equatio: Cosider the followig fiite differece approximatio to the 1D wave equatio: ad, as i the case of the heat equatio, substitute u = ϕ e iθ ito (8.5) = r u +1 + (1 r )u + r u 1 u 1 (8.5) e iθ ϕ +1 = ( r e iθ + ( 1 r ) + r e iθ) e iθ ϕ e iθ ϕ 1 Cacelig terms ad usig the double agle formulae ϕ +1 = ( 1 + r (cos θ 1) ) ϕ ϕ 1 (8.6) ( = 1 r si θ ) ϕ ϕ 1 (8.7) If we ow assume that ϕ has the followig expoetial form ϕ = G the (8.7) reduces to the followig quadratic equatio: G γg + 1 = 0 (8.8) where γ = ( 1 r si ) θ. The solutios of this quadratic equatio are give by Now sice G 1 ad G are the roots of this quadratic we may coclude that G 1, = γ ± γ 1 (8.9) (G G 1 )(G G ) = G (G 1 + G )G + G 1 G = 0 (8.30)
8 Comparig the last terms i these two quadratic equatios (8.8) ad (8.30) we coclude that G 1 G = 1. (8.31) However, for stability of solutios for the form ϕ = G, we require that G 1 1 ad G 1. Give the costrait (8.31), the oly possibility, if the solutios are to be stable, is that G 1 = G = 1. Thus G must fall o the uit dis, which implies that γ 1 Thus or so that 1 r si θ 1 1 1 r si θ 1 r si θ 0 (8.3) The secod iequality i (8.3) is satisfied automatically, while the first leads to the coditio r si θ Sice the maximum value that si ( θ ) ca achieve is 1, we coclude that the coditio for stability is 1 or r = (c t/) 1 t c (8.33) The coditio (8.33), which imposes a upper boud o the time step that ca be used, is ow as the Courat- Friedrichs-Lewy or CFL coditio.
Fiite Differece Methods 9 8.4 Solvig Laplace s Equatio usig fiite differeces Cosider the boudary value problem u x + u = 0 y 0 < x, y < 1 (8.34) BC: u(0, y) = 0; u(1, y) = 0; u(x, 0) = f(x); u(x, 1) = 0. (8.35) y u(x, 1) = 0 1 1 = y M u(0, y) = 0 u xx + u yy = 0 u(1, y) = 0 y u(x, 0) = f(x) y 1 y 0 1 x x 0 x 1 x x N = 1 As before we replace the secod derivatives i (8.34) by cetral differece quotiets that are secod order accurate: u(x +, y) u(x, y) + u(x, y) u(x, y + y) u(x, y) + u(x, y y) y = u x (x, y) + O( ) (8.36) = u y (x, y) + O( y ) (8.37) We partitio the iterval 0 x 1 ito (N + 1) equally spaced odes x = ad the iterval 0 y 1 ito (M + 1) equally spaced odes y m = m y. Replacig the derivatives i (8.34) by the differece quotiets i (8.36) ad (8.37), ad represetig the mesh values at (x, y m ) by u m u(x, y m ) we obtai: u +1m u m + u 1m If we choose = y the we obtai + u m+1 u m + u m 1 y = (u xx + u yy ) (x,x m ) + O(, y ). u +1m + u 1m + u m+1 + u m 1 4u m = 0 1, m (N 1), (M 1) (8.38) u m+1 1 u 1m u m u +1m 1-4 1 1 u m 1 This is ow as the fiite differece Stecil that relates u m to its 4 earest eighbours. This is a system of (N 1) (M 1) uows for the values of u m iterior to the domai - recall the boudary values are already specified!
10 8.4.1 Solvig the System of Equatios by Jacobi Iteratio This is a procedure to solve the system of Equatios (8.38) by loopig through each of the mesh poits ad updatig u m accordig to (8.38) assumig that the earest eighbours already have values close to the exact solutio. This procedure is repeated util the chages that are made i each iteratio falls below a certai tolerace. To implemet this iterative procedure we observe that the discrete Laplace Equatio (8.38) ca be re-writte i the form: m = u +1m + u 1m + u m+1 + u m 1 4 (8.39) average Thus u m is the average value of its earest eighbours. Note that a ew superscript idex has bee itroduced to represet the odal values at the th iteratio. Thus iteratio ca be viewed as taig successive eighbour averages util there is o chage, at which poit the value of u m equals the average of the values at its mesh eighbours. This mea value property is a discrete form of a fudametal property of ay solutio to Laplace s Equatio, which also implies that maxima or miima of solutios to Laplace s equatio caot occur iside the domai but must be restricted to the boudary - a very useful poperty of Laplace s equatio ow as The Maximum Priciple. To implemet the iterative procedure (8.39) i a spread sheet, go to the Tools Meu at the top of the scree ad clic o the Optios Tab. The select the Calculatio Tab. Chec the Iteratio box. If you set the umber of iteratios to 5 say, the if you start with zero values throughout the iterior of the domai (as you should if you cut ad paste as demostrated i class), you will see the values percolate 5 cells ito the domai from the o zero boudary coditio f(x) = si(πx). You ca choose a surface plot to visualize the solutio. Now hold dow the F9 ey ad watch the solutio move to equilibrium. This iterative process essetially uses diffusio o a pseudo time scale to tae the solutio to equilibrium. Exercises for Laplace s Equatio: Exercise 4 Implemet a 0 derivative BC alog the lies x = 0 ad x = 1. Plot a cross sectio of the results alog y = 1/. To esure that u u (0, y) = 0 = (1, y). x x Exercise 5 Implemet a ihomogeeous term for Poisso s Equatio: u x + u = f(x, y) 0 < x, y < 1. y Itroduce fiite differece quotiets, assume = y to arrive at the iterative formula: ( u +1m + u 1m + u m+1 + u m 1 f(x, y m ) ) m = 4. ( ) It may be useful to calculate the values of f m o a separate sheet i which the same cell values as those for u m are maitaied. The the values of f m ca be refereced i the calculatio of u m accordig to ( ).