Finite Difference Metods Assignments Anders Söberg and Aay Saxena, Micael Tuné, and Maria Westermarck Revised: Jarmo Rantakokko June 6, 1999 Teknisk databeandling
Assignment 1: A one-dimensional eat equation Subtask 1 a) Use te forward Euler sceme to solve te one-dimensional eat problem u t = λu xx 0 <x<1, t > 0 u(x, 0) = f(x) (1) u(0,t) = u(1,t)=0 were Compute and plot te values u n f(x) =sinπx +0.3sin2πx 0.1sin3πx λ =0.1, =0.1, k =0.05 for n =0, 10, 20, 30, 40, 50. b) Te same problem as in a) but wit k =0.1. Make comments on te results. c) Solve te same problem as in a) but wit λ =0.05. Coose k as large as possible but keep λk/ 2 1/2. Plot te results and compare tem wit a). How does reduction in te value of te parameter λ affect te results? Make experiments wit different values of λ. d) Solve te same problem as in a) wit te backward Euler metod. Tis gives a system of equations wit N 1 unknowns at every time step. Te resulting system is a tridiagonal system and can be solved wit MATLAB routines. Begin by considering te coefficient matrix. Verify by experiments te unconditional stability of te sceme. e) Solve te same problem as in d) but wit te Crank Nicolson metod. Subtask 2 Our simple model problem (1) can be generalized in different ways. a) Cange te boundary values to u(0,t)=g 1 (t) and u(1,t)=g 2 (t) were g 1 and g 2 are cosen in a proper way and compute te solution wit te forward Euler sceme. 1 Institutionen för teknisk databeandling
b) Let λ be a function of x and/ or t. Try wit λ = e x, λ = e t, λ = e x+t and oters values in combination wit backward Euler. Are tere any computational complications in comparison wit te case wen λ constant? c) Let λ =2/(2+u 2 (x, t)) i.e. let λ depend on te temperature u and compute tesolutionwitteforwardeulermetod. d) Try wit λ = 1. Wat appens? e) Try wit u t = λu xx + au x + bu + f and replace u x wit (u n +1 un 1 )/2 in forward Euler and similarly for backward Euler and Crank Nicolson. Assignment 2: A two-dimensional eat equation Nowweaveteequationu t = λ 1 u xx + λ 2 u yy in te region 0 <x,y<1, t>0. Te constants λ 1 and λ 2 are bot positive. Suppose we ave te initial temperature u(x, y, 0) = f(x, y), e.g. = sin πx sin πy and tat u(x, y, t) = 0 on te boundary. a) Define forward Euler, implement it and compute te solution for some combination of x, y, and k. Try to find a stability condition for λ 1 = λ 2. b) Implement te backward Euler metod. How many unknowns do we get on eac time level? c) Try te ADI-metod (ADI=Alternating Direction Implicit) given by /2 i i u n i k/2 /2 i k/2 /2 i+1, = λ 1 /2 i+1, = λ 1 2/2 i 2 x 2/2 i 2 x + /2 i 1, + /2 i 1, + λ 2 u n i,+1 2u n i + u n i, 1 2 y i,+1 + λ 2un+1 i + i, 1 2 2 y Here we ave used an extra time level wit time index n +1/2. Instead of solving one very large linear system we solve a number of smaller systems. 2 Institutionen för teknisk databeandling
Assignment 3: A yperbolic problem Subtask 1 We study te PDE-problem u t = u x 0 <x<1, 0 <t u(x, 0) = f(x) =sin2πx u(x, t) =u(x +1,t) wic as te solution u(x, t) =f(x + t) a) Use te leap-frog sceme u n 1 2k = un +1 un 1 2 and try te following combinations of and k 1) =0.1, k =0.049 2) =0.1, k =0.098 3) =0.05, k =0.0245 4) =0.05, k =0.049 5) =0.1, k =0.1 Use te exact solution for t = k. Compare te computed solution wit te correct solution. Try to find ow te quality depends on te value of λ = k/. In cases of instability, on wic time level is it first observed? For wic x? Make observations of te propagation of perturbations. Subtask 2 a) We cange te boundary condition to u(1, t) = 0. Te PDE-problem is still well posed and as te solution { f(x + t) for x + t 1, u(x, t) = 0 oterwise. But te leap-frog sceme needs a boundary condition also on te left. Coose simple extrapolation 0 =2u n 1 u n 1 2. Wic effects does tis numerical boundary condition ave? 3 Institutionen för teknisk databeandling
b) To reduce te perturbations we cange te difference sceme to = u n 1 +2k un +1 u n 1 2 δ(u n 1 +2 4un 1 +1 +6un 1 4u n 1 1 + un 1 2 ) Tis is leap-frog wit a dissipative term and it can be applied in x 2,x 3,..., x N 2. In x 1 and x N 1 te simple leap-frog sceme is used. Use =0.05 and k =0.04 and coose different values of δ between 0 and 0.2 and try to find an optimal value of δ. Assignment 4: A gas flow problem One dimensional gas flow can be described by te yperbolic system ρ m e t + m ρu 2 + p (e + p)u x = 0 0 0. (2) Here ρ is te density of te gas, m te momentum, and e te total energy. Te velocity is u = m/ρ. Under te assumption tat we ave an ideal gas, te pressure is given by p =0.4(e 1 2 m2 /ρ). Te system can be written in te form U t + F (U) x =0, were te vectors U and F (U) can be identified in (2). Te initial state is 0.445 U(0,x)= 0.311 for x<0 and U(0,x)= 8.928 0.5 0 1.4275 for x>0 Subtask 1 Implement and solve te problem in te interval 2 <x<2uptot =0.6. Use te leap-frog wit a dissipative term and approximately 400 grid points 4 Institutionen för teknisk databeandling
in space. Try different values of δ to find a solution wit as few oscillations as possible. To obtain stability t sould be cosen suc tat t 0.2 x. In Figure 1 te solution, at a time t>0(nott =0.6), is sown. Note tat suc a good resolution cannot be obtained wit te metods used in tis assignment. 1.4 Density 4 Pressure 1.6 Velocity 1.2 3.5 1.4 3 1.2 1 1 2.5 0.8 0.8 2 0.6 0.6 1.5 0.4 0.4 1 0.2 0.2-1 -0.8-0.6-0.4-0.2 0 0.2 0.4 0.6 0.8 1 0.5-1 -0.8-0.6-0.4-0.2 0 0.2 0.4 0.6 0.8 1 0-1 -0.8-0.6-0.4-0.2 0 0.2 0.4 0.6 0.8 1 Figure 1: Te solution of te gas flow example (2) Subtask 2 Explain ow te upper limit of t is found. Te limit as someting to do wit te spectral radius of te Jacobian, F/ U, wic is around 4.7. Assignment 5: An elliptic problem Subtask 1 A simple elliptic PDE-problem is { uxx + u yy = f(x, y) 0 <x,y<1 u(x, y) = g(x, y) on te boundary Tis is te Diriclet problem for Poisson s equation. Te step lengts are x = y = 1/N and te most natural difference approximation is te five-point formula u i+1, 2u i, + u i 1, x 2 + u i,+1 2u i, + u i, 1 y 2 = f(x i,y ) 5 Institutionen för teknisk databeandling
Wen tis is applied we get a linear system wic is sparse, i.e. most of te elements of te coefficient matrix are zeros. a) First, find te linear system for N = 4 and ten do te same for a general value of N. How is te coefficient matrix canged if x y? b) Compute te solution and plot it for N =10, 20, 30. Make own coices of f(x, y) andg(x, y). Assignment 6: Air Quality Modeling and te Advection Diffusion Equation I Introduction An important area in modern Environmental Engineering is te study of various air pollutants. Te concentrations of tese pollutants are described by air quality models wic are often formulated as partial differential equations. Wit te use of models te ope is to predict ow peak concentrations will cange in response to predefined canges in te source of pollution. Consider now te concentration u(x, y, z, t) of a gaseous compound. If we ave knowledge of te concentration at time t =0,wewouldliketobeabletopredict future concentrations. Let, for instance, u be te concentration of a noxious gas formed at an industrial plant. At t = 0 a concentrated cloud of te pollutant is released into te air surrounding te plant. Of great importance is te knowledge of ow concentrated te gas will be wen it reaces te nearby residential areas. To model te air quality (i.e. equation, te concentration of u) we use te continuity t = J (3) were is te nabla operator, =(,, ). Equation (3) states tat te x y z time development of u is related to te flux J (amount per area and time) of te gas. Te total flux is made up of two terms. First we ave te dispersive effect of diffusion, given by Fick s first law, J diffusion = D u, were D denotes te diffusion constant. In addition to diffusion te gas is transported by te wind troug a process called advection. Tis leads to a flux, J advection = v u, werev 6 Institutionen för teknisk databeandling
is te wind vector. If we now combine te expressions for te flux wit equation (3) we get te advection diffusion equation, t were is te Laplace operator, u = 2 u x 2 = D u (v u) (4) + 2 u y 2 + 2 u z 2. In some special cases equation (4) can be solved analytically. However, in most cases, we ave to rely on numerical metods. Task 1 To start off gently we can simplify equation (4) in te following way. First we leave out te diffusion term (i.e. D = 0). If in addition to tis we only let te wind blow in te x-direction (v =(v, 0, 0)) we get te following equation: wic can be expanded into t = (v u) x t = v x u v x Finally, if v is te same for all x, tenequation(5)turnsinto t = v x To solve equation (6) numerically te following explicit finite difference sceme 1 can be used: u n k = v un un 1 Use te sceme above and te initial condition { 5 x < 1 u 0 (x) = 0 oterwise to solve (6) for te area defined by { x 15 0 <t 4 1 Standard notation is used in all te scemes. Tis means tat and n are indices for space and time, respectively, and k are step lengts for space and time, respectively. (5) (6) 7 Institutionen för teknisk databeandling
Let v = 1 and coose =0.1 andk =0.05 for te step lengts. Plot te concentration profile at t = 4 togeter (in te same figure as sown in figure (2 B)) wit te profiles you obtain in Task 2 and 3. Explain te concentration profile at t = 4. Is tis wat you would expect wit only advection wit a constant wind velocity? Te exact solution of 6 is u(x, t) = u 0 (x vt) (sown in figure(2 A). Does your numerical solution differ from it? Note tat te problem above (equation (6) togeter wit te initial condition) is not a well-posed problem. To make it a well-posed problem we ave to add boundary conditions. Here, and in te oter tasks we will use natural constraints assuming tat te solution is constant outside a predefined interval. If we solve te problem using a large enoug region, like x 15, tis won t lead to any complications. Figure 2: A Te exact solution to equation (6). B Numerical solutions to Task 1, 2 and 3. Task 2 If we now complicate te situation given in Task 1 somewat by letting te wind (still ust blowing in te x-direction) be a function of x but still ignoring diffusion, ten our concentration u can be described by equation (5), given above. An explicit sceme for tis problem is: u n k = v u n u n 1 u n v v 1 (7) a) Deduce te order of accuracy for te sceme (7). 8 Institutionen för teknisk databeandling
b) Use te same step lengts, initial conditionandregionasintask1tosolve equation (5) wit te sceme (7). Let te wind velocity be defined as: { x v(x) = 2 /(1 + x 2 ) x>0 0 oterwise Plot te concentration profile at t = 4 togeter (in te same figure) wit te profiles of Task 1 and 3. Task 3 If we enter diffusion into equation (6), we obtain te following equation t = D 2 u x v 2 x wic can be solved using te sceme, (8) u n k = D un +1 2u n + u n 1 2 v un u n 1 (9) a) Solve equation (8), using te same initial condition, region and te same wind as in Task 1. Use =0.1, k =0.004 and D =1. Plot te concentration profile at t = 4 togeter (in te same figure) wit te profiles you obtained in Task 2 and 3. Explain te differences between te tree profiles. b) Te stability conditions for te finite difference scemes can be examined using, for example, te Fourier metod. Te stability condition for te sceme (9) is k (2D + v) 1 2 Te coice of step lengts in Task 3a terefore gives a stable difference sceme. Now, solve te same problem as in Task 3a but use te step lengts ( =0.1, k =1/210) and ( =0.1, k =1/209). Wit tese coices of step-lengts it is not possible to find te solution at exactly t = 4, owever, you can still study te penomenon of unstable difference scemes. Explain te results. 9 Institutionen för teknisk databeandling
Assignment 7: Air Quality Modeling and te Advection Diffusion Equation II Task 1 In assignment 6 we ave only looked at cases were te concentration depends on one space variable and time, u = u(x, t). Let s now study wat appens if te wind vector as two components, v =(v 1,v 2, 0). If we set D =0tegeneral advection diffusion equation (4) turns into: t = v 1 x v 2 y u v 1 x u v 2 y (10) Equation (10) can be solved using a generalization of te sceme (7): (i, ) u n (i, ) k = v 1 (i, ) un (i, ) u n (i 1,) v 2 (i, ) un (i, ) u n (i, 1) u n (i, ) v 1(i, ) v 1 (i 1,) u n (i, ) v 2(i, ) v 2 (i, 1) (11) Use tis sceme to solve (10) wit te following specifications. Solve for x 30, y 30, t 15, step lengts =0.5, k =0.1 and te initial condition: { 50(1 + cos πr u 0 (x, y) = ) r 4 4 0 oterwise were { r 2 =(x x 0 ) 2 +(y y 0 ) 2 (x 0,y 0 )=(0, 20) Te wind is defined by { R = x2 + y 2 v =( (y +0.5x)/R, (x 0.5y)/R) Make plots of te solution at t =0,t =5,t = 10, and t = 15. Make a velocity plot of te wind, i.e. plot arrows indicating te wind direction (use quiver in matlab). Try to explain your results. Cange te step size to k = 0.01. Wat appens? 10 Institutionen för teknisk databeandling
Task 2 Add te dissipation term wit D = 1.0. Approximate te Laplace operator wit second order centered differences. Re-do te calculations wit = 0.5 and k =0.01. Compare wit task 1 and explain bot te pysical and te numerical beavior. Task 3 Suggest a better numerical metod for tis problem and motivate your coice, (possibly by sowing tat it works better). 11 Institutionen för teknisk databeandling