function [] = onewaywaveStability()
% Solve the one-way wave equation
%   u_t + u_x = 0,   for 0<x<10, 0<t
% with initial condition
%   u(0,x) = 1 - cos (2*pi*x)   for 0<x<1
%            0                  elsewhere
% and periodic boundary condition
% by several different discretizations

% Set discretization grid
M = 500;
h = 10/500;
x = linspace(0,10, M+1);
N = 500;
k = h;
u = zeros(N+1, M+1);
% Set initial condition
u(1, 1:51) = 1-cos(2*pi*x(1:51));

% forward-time forward-space scheme (unstable)
for n=2:(N+1)
    u(n, 1:M) = -u(n-1, 2:(M+1)) + 2*u(n-1, 1:M);
    u(n, M+1) = u(n, 1);
end
% draw animation
figure(1); clf;
F(50) = struct('cdata',[],'colormap',[]);
for l = 1:50
    plot(x, u(1+10*l,:));
    drawnow;
    F(l) = getframe;
end
movie(F);

% forward-time forward-space scheme (stable)
for n=2:(N+1)
    u(n, 2:(M+1)) = u(n-1, 1:M);
    u(n, 1) = u(n, M+1);
end
% draw animation
figure(2); clf;
for l = 1:50
    plot(x, u(1+10*l,:));
    drawnow;
    F(l) = getframe;
end
movie(F);

end