function adv_LF(a) % 1D advection equation u_t + u_x = 0 with Dirichlet boundary conditions % Leap Frog N = 2^11; h = 1/N; x = (0:h:1)'; u0 = 10 * (1-x) .* exp(-200*(.5-x).^3) .* sin(30.*exp(-100*(x-.2).^2)); figure(1); clf; plot(x,u0); pause T = 1; dt = a/N; % choose a good time step r = dt/h; I = 2:N; U = u0; % 1st step: Runge Kutta 2 Up = U; Utemp = zeros(size(U)); Utemp(I) = U(I) - r/4*(U(I+1)-U(I-1)); % like U at time dt/2 U(I) = U(I) - r/2*(Utemp(I+1)-Utemp(I-1)); V = zeros(size(U)); for n = 1:400 V(I) = Up(I) - r*(U(I+1) - U(I-1)); Up = U; U = V; figure(1); clf; plot(x,U); axis([0 1 -3 5]); end