%
%  Solve 2D hyperbolic problem
%
%      u_tt - c^2*u_xx = 0, 0<x<L, c>0
%      u(0,t)=0, t>0           % initial cond.
%      u_t(L,t)+c*u_x(L,t)=0   % absorbing bound.cond.
%      u(x,0)=u0(x)            % Dirichlet bound.cond.
%
%      n = number of spatial grid points: h=L/(n-1)
%      m = number of time steps: dt=T/m
%
function hyp2(c,n,m)
L=6;
T=10;
dt=T/m;
x=L*(0:n-1)/(n-1);
h=L/(n-1);
c2 = dt*c/h;
c1 = c2*c2;
u=u0(x);
uold=u;
unew=zeros(n,1);
fprintf(1,'h=%.2f, dt=%.2f, Courant number = %.3f\n',h,dt,dt*c/h);   
for step=1:m
    t = step*dt; 
    for j=2:n-1
        unew(j) = 2*u(j) - uold(j) + c1*(u(j+1)-2*u(j)+u(j-1));
    end
    unew(n) = u(n) - c2*(u(n)-u(n-1));
    if mod(step,5)==0
        plot(x,unew,'LineWidth',2); axis([0 L -1 1]);
        str = sprintf('time = %f',t);
        title(str);
        pause(0.1);
    end
    uold = u;
    u=unew;
end

%
% Initial value
%
function u=u0(x)
%u=0.9*(x>=0.25 & x<=0.5);
u=(x>=1 & x<=2).*sin(pi*(x-1));
%u=0*x;
