% 6.4.5 Leapfrog with various CFL numbers.

N=1000;
dx = 2 * pi/(N-1);
x = -pi:dx:pi;
r = [0.8,0.9,1.0,1.1];
dt = r*dx;
steps = round(pi./dt);

SW = zeros(N,1);
SW(:,1) = pi/4 * square(3*pi/2 + x);

for j = 1:4
	u = zeros(N,steps(:,j)+2);
	u(:,1) = pi/4 * square(pi/2 + x);
	u(:,2) = u(:,1);
	C = toeplitz(sparse([2 -1 zeros(1,N-3) -1]));
	for k = 3:steps(:,j)+2
		u(:,k) = 2*u(:,k-1) - u(:,k-2) - r(:,j)^2 * C*u(:,k-1);
	end
	subplot(2,2,j)
	plot(x,u(:,end),'b.-',x,SW(:,1),'r-')
	xlabel('x')
	ylabel('u')
	title(sprintf('Leapfrog, CFL=%1.1f',r(:,j)))
end
