% Staggered Leapfrog Script Template clear;close; % build a cell-centered grid with N=200 and L=1; . . . % define the initial displacement and velocity vs. x y = exp(-(x-L/2).^2*160/L^2)-exp(-(0-L/2).^2*160/L^2); v = 0*x; % multiplying x by zero makes an array of zeroes % of the same length as x subplot(2,1,1) plot(x,y) % plot the initial conditions xlabel('x');ylabel('y(x,0)');title('Initial Displacement') subplot(2,1,2) plot(x,vy) % plot the initial conditions xlabel('x');ylabel('v_y(x,0)');title('Initial Velocity') pause; % Set the wave speed; c=2; % wave speed % Suggest to the user that a time step no larger than taulim be used taulim=h/c; fprintf(' Courant time step limit %g \n',taulim) tau=input(' Enter the time step - ') % Get the initial value of yold from the initial y and vy . . . % load the ghost point boundary conditions in yold(1) and yold(N+2) . . . tfinal=input(' Enter tfinal - ') skip=input(' Enter # of steps to skip between plots (runs faster) - ') nsteps=tfinal/tau; % here is the loop that steps the solution along figure % open a new frame for the animation for n=1:nsteps time=n*tau; % compute the time % Use leapfrog and the boundary conditions to load ynew with y at % the next time step using y and yold, i.e., ynew(2:N+1)=... % Be sure to use colon commands so it will run fast. . . . %update yold and y yold=y;y=ynew; % make plots every skip time steps if mod(n,skip)==0 plot(x,y,'b-') xlabel('x');ylabel('y'); % Print a top label with the time included topline=sprintf('Staggered Leapfrog Wave: time= %g',time) title(topline) axis([min(x) max(x) -2 2]); pause(.1) end end