% Wave equation with forward Euler central differences
xmax=10
nx=1000
dx=xmax/(nx)
tmax=2
lambda=.9
dt=dx*lambda
nt=0;
ntplot=1;
x=[1:nx]*dx;
y=cos(2*pi*x);
 
t=0
 
clf
fig1=figure(1)
lin1=line(x,y,'EraseMode','background');
axis([0 xmax -2 2]);
xlabel('Space x');
ylabel('Amplitude u');
drawnow;

while t < tmax

  yp=[y(nx) y(1:nx-1) ];
  ym=[y(2:nx) y(1) ];
  y=y-lambda*(yp-ym);

  if rem(nt,ntplot) == 0
    delete(lin1);
    lin1=line(x,y,'EraseMode','background');
    drawnow;
  end
  nt=nt+1;
  t=t+dt;

end

