% leapfrog with simple outflow boundary condition (equivalent to using forward
% Euler for the last point) this illustrate spurious modes that propagate
% backwards!  See "Finite Difference Methods for Ordinary and Partial
% Differential Equations", R.  LeVeque.
n=100;
x=linspace(0,1,n)';
h=1/n;
a=1;
k = 0.9*h/abs(a);


utrue = @(x) max(1-abs(6*(mod(x,1)-1/2)),0);
u = utrue(x);

e = ones(n,1); h = 1/n;
A = -(a/2/h)*spdiags([-e,e],[-1,1],n,n);

% Leapfrog
uol=u;
u = uol + k*A*uol;
for i=1:1000,
 unew = uol + 2*k*A*u;
 uol=u;
 u=unew;
 plot(x,u,x,utrue(x-a*k*i)); axis([0 1 -0.5 1.5]);
 title(sprintf('t=%d\n',k*(i-1)));
 pause(0.1);
end;
