Example Advection Harmonic Wave

You might also like

Download as pdf
Download as pdf
You are on page 1of 15
‘1130722, 718 PM Advection Advection close al1 Parameters grid spacing Delta x * % Left boundary % right boundary % % % advection speed wave Length final tine lanbda = 0.53 tend Mesh (xlide:xR-dx)';® spatial grid jength(x);% number of grid points 1st order upwind discretization in space @ = ones(N,1); % auxiliary vector DLL = spdiags([-e,e]/dx, [-1,0], N,N); % left-sided finite diff. Periodic boundary condition D1l(a,end) = -1/de; Forward difference in time T= speye(Wn); % Identity matrix Cs = (0.8, 2, 1.215 % Courant numbers for 4 = aslength(cs) T = sin(2*pi*x/lanbda); % initial condition c= C5C4)5 % Courant number dt =C* dx / us % tine-step size R= I~ urdteDil; % RHS matrix figure(i); % separate figure for each C for t = dt:dt:t_end % time-stepping loop T= RT; % compute next step T_a = sin(2*pit(x-u*t)/lanbda); % analytical solution plot (x,T_a); % plot analytical solution hold ons plot (x,1); % plot numerical solution hold off; title(sprint(‘Upwind, C = %e" sC))s drawnow; end end hitpssituweltuwien ac aupluginie.php/8899984/mod_resourcelcontent/fadvection ml 1s. ‘1130722, 718 PM Advection hitps:ituweltuwien ac alpluginie.php/8839984/mod_resourcelcontent/fadvection inl ‘1130722, 718 PM Advection Leapfrog scheme Centered fie diference in space Dic = spdiags([-e,e]/2/dx, [-1,1], NjN); % centered FD Periodic boundary condition bic(a,end) = =1/2/dxs Dic(end,1) = 1/2/dx; for 4 Aslength(Cs) Figure(i); © = cs(i)s dt = * dx /.u 5 % time-step size T Tm sin(2*pitx/lanbda); % initial condition sin(2*pit(x+urdt) /Lambda) ; for t = dt:dt:t_end Tpl = Tw - 2turdt*o1e*T; T_a = sin(2*pi*(x-u*t)/Lambda); % analytical solution plot (x,T_a); % plot analytical solution hold ons plot (x,T); % plot numerical solution hold off; ylim({-2,1])5 title(sprintf( ‘Leapfrog, C = %g',C))5 hitps:ituweltuwien ac alpluginie.php/8899984/mod_resourcelcontent/fadvection hint 36 ‘1130722, 718 PME Advection drawnow; end end Leapfrog, C = 0.8 hitps:ituweltuwien ac aupluginie.php/8839984/mod_resourcelcontent/fadvection hint ‘1130722, 718 PM Advection Leapfrog, C = 1.2 am i 0.6 oa 02 “0.2 i “04 06 “0.8 41st order upwind for 2 = ().25 0.25; % wave length 8; % Courant number sin(2*pi*x/lambda); % initial condition C* dx /u 5 % time-step size R=I- utdtp1l; % RHS matrix Figure(1); for t = dtidt:t_end RTs % compute next step Ta = sin(2*pi*(x-u*t)/lanbda); % analytical solution plot(x,T_a); % plot analytical solution hhold ons plot(x,T); % plot numerical solution hold off; yhim((-1,2])5 title(sprintf(‘Upwind, C = Xe" 4C))5 drawnow; end hitps:ituweltuwien ac alpluginie.php/8839984/mod_resourcelcontent/fadvection inl 515 ‘1130722, 718 PM Advection Upwind, C = 0.8 Leapfrog for 1 = 0.25 T = sin(2*pitx/lanbda); % initial condition ‘Tm = sin(2*pi*(x+utdt)/Lambda) ; Figure(2); for t = dtidt:t_end Tpi = Ted - 2*utdto1erT; Tm = T5 T = 1piy Ta = sin(2*pit(x-u*t)/lanbda); % analytical solution plot(x,T_a); % plot analytical solution hold ons plot(x,1)5 % plot numerical solution hold off} ylin({-1,1])3 title(sprintf( ‘Leapfrog, C = %g'.C)); drannows end hitps:ituweltuwien ac alpluginie.php/8839984/mod_resourcelcontent/fadvection inl 65 ‘1130722, 718 PM Advection Backward-time centered-space larbda xs = err = for i ba br pa at T = 0.5; [0.005,0.01,0.02,0.05]; nan(size(dxs))5 = Anlength(dxs) si) (xLidxtxt-dx) 5 & spatial grid = length(x); | number oF grid points = Speye(NN)3 -X Identity matrix = ones(Ni); _% auxiliary vector 1c = spdiags([-e,e]/2/dx, [-1,2], NN); % centered FD je(Ayend) = -1/2/dx; ie(end,1) = 1/2/dx3 “dx / uj % time-step size sin(2*pitx/lambda); % initial condition L2 T+ utdtoic; % LHS matrix fi fo igure(i); nt = at: Ts t\t T_a = sin(2*pi*(x-u*t)/Lanbda); % analytical solution plot (x,T_a); % plot analytical solution hold ong plot (x,T); % plot numerical solution hold off; ylim({-2,1])5 title(sprintf('BTCS, dx = %g',dx)); drawnow; hitps:ituweltuwien ac aupluginie.php/8839984/mod_resourcelcontent/fadvection hint m6 ‘1130722, 718 PME end err(i) = max(T-T_a); end BTCS, dx = 0.005 Advection BTCS, dx = 0.01 i \ I~ 0 0.2 0.4 Oe hitps:ituweltuwien ac alpluginie.php/8839984/mod_resourcelcontent/fadvection inl ‘1130722, 718 PM Advection BTCS, dx = 0.02 aff 0.6 oa 0.2 0.2 “04 06 “0.8 0 0.2 0.4 Oe BTCS, dx = 0.05 og figure(t) 5 Loglog(dxs,err); hold ons Loglog(dxs, 1e1*dxs) ; Loglog(dxs ,1e2*dxs.*2)5 hold off; xlabel('$\Delta x$","Interpreter*, “latex”); ylabel( error’); title('Brcs"); Legend( ‘numerical error’, "$\belta xs", "$\belta x*2$", "Interpreter", “Latex”, "Location’ hitps:ituweltuwien ac aupluginie.php/8839984/mod_resourcelcontent/fadvection hint southeast"); ons ‘1130722, 718 PM Advection 10° a 10? - Tanceioal rar az Art 10? 107 Ar Lax-Wendroff for i = a:length(dxs) dx = dxs(i); x = (xLidx:xR-dx)'5 % spatial grid N= Length(x); % number of grid points I = speye(N,N); Identity matrix e = ones(N,1); % auxiliary vector Dic = spdiags({-e, e] /2/dx, [-1, 1], NyN)s % centered FD Dac = spdiags([1,-2,1].*e/ax"2, [-1,0,1], NN); dt=c*dx/ uj % time-step size T = sin(2*pi*x/lanbda); X initial condition Ra T- utderone su*2*de2/2%02¢5 % RNS matrix R(a,end) = R(2,1)5 R(end,1) = R(end-1,end) 5 Figure(i)s for t = dt:dt:t_end T= RT; Ta = sin(2*pit(x-u*t)/Lanbda); % analytical solution plot (x,1_a); % plot analytical solution hold ons plot (x,1); % plot numerical solution hold off ylim({-2,1])5 title(sprintf('Lax-wendroft, dx = %g',dx)); drawnow; end hitps:ituweltuwien ac alpluginie.php/8839984/mod_resourcelcontent/ advection himl 1015 ‘1130722, 718 PME Advection err(i) = max(T-1_a); end Lax-Wendroff, d Lax-Wendroff, dx = 0.01 oa 0.6 oa 02 o 0.2 0.8 0.6 0.8 \ 1 02 04 6 08 hitps:ituweltuwien ac alpluginie.php/8839984/mod_resourcelcontent/ advection himl ns 1 ‘1130722, 718 PM Advection 0.2 “04 06 \ “| \f A Oe Lax-Wendroff, dx = 0.05 Figure(1); loglog(axs,err); hold on loglog(axs, 1e1*dxs); loglog(dxs, 1e2*dxs.*2)5 hold off; xlabel('$\0elta x$*, "Interpreter", latex"); ylabel(‘error'); ‘title( ‘Lax-Wendroff"); Legend(‘nunerical error’, "$\belta x$", ‘$\Delta x*2$", "Interpreter", "latex", "Location" ,"southeast"); hitps:ituweltuwien ac aupluginie.php/8839984/mod_resourcelcontent/fadvection hint 126 ‘1130722, 718 PM Advection Lax-Wendroff 10° error aaa ra —r, ar 10 Ar Lax-Friedrichs ax = 0.0; % grid spacing c= 0.85 % Courant number (xt :dx:xR-dx)'5 spatial grid % % N= Lengtn(x)5 rnunber of grid points T = speye(NsN)5 Tdentity matrix fe = ones(N,1); auxiliary vector Dic = spdiags({-e, e] /2/dx, [-1, 2], NN); % centered FD bac = spdiags([1,-2,1].te/dx2, [-1,0,1], NN); A = spdiags((@.5,0.5]."e , [-1, 1], NN); % averaging matrix de Ct dx / uj % tine-step size R= A- urdttDic; % RHS matrix R(Lyend) = R(2,1)5 X Periodic BC at x0 R(end,1) = R(end-1,end); % Periodic BC at x=1 lanbdas = [0.25,0.5,2]5 err = nan(size(lambdas)); for 4 length (lanbdas) Janbda = Lanbdas(i); T = sin(2*pitx/lambda); % initial condition Figureti); fon t = dtsdt:t_end Te RT Ta = sin(2*pit(x-urt)/lanbda); % analytical solution plot (x,T_a); % plot analytical solution hold ons plot (x,1); % plot numerical solution hitps:ituweltuwien ac aupluginie.php/8839984/mod_resourcelcontent/fadvection hint 1316 ‘1130722, 718 PME Advection hold off; ylin([-2,1])5 title(sprintf('Lax-Friedrichs, lambda = %g" lambda); drawnow; end err(i) = max(T-T_a); Lax-Friedrichs, lambda = 0.25 hitps:ituweltuwien ac alpluginie.php/8839984/mod_resourcelcontent/fadvection inl 1416 ‘1130722, 718 PM Advection Lax-Friedrichs, lambda = 1 os 0.6 oa 02 0.2 “04 0.6 “0.8 Figure(1); plot (lanbdas err) ; xlabel('$\lanbda$" ,"Interpreter™, “latex"); ylabel( ‘error’ )3 title('Lax-Friedrichs'); Lax-Friedrichs 0.9 08 or 06 05 = oa 03 02 02 03 OF O05 O06 O07 O08 09 2 d hitps:ituweltuwien ac alpluginie.php/8839984/mod_resourcelcontent/ advection himl 19115

You might also like