function Wave()
L = 3;   % length of the space interval
n = 450; % number of interior grid points
r = 0.8; % ratio to compute time step
%r = 1.001; % ratio to compute time step
%r = 1.0001
T = 5;   % final time

function r = iv(x);  % initial displacement
  r = min([2*x,2-2*x]')';  r = max([r,0*x]')';
end%function

dx = L/(n+1);
dt = r*dx;
x  = linspace(0,L,n+2)';

y0 = iv(x);  y0(1) = 0; y0(n+2) = 0;
y1 = y0;   % use zero initial speed
y2 = y0;
for t = 0:dt:T+dt;
  plot(x,y0); axis([0,L,-1,1]); grid on; drawnow()
  for k = 2:n+1
    y2(k) = (2-2*r)*y1(k)+r*(y1(k-1)+y1(k+1))-y0(k);
  end%for
  y0 = y1; y1 = y2;
end%for

end%function
