%                        ForwardtCenteredx.m
%
% A sample Matlab script that implements 
% the forward t/centered x finite difference method for 
% the PDE 
%
% u_t+au_x = 
%
%
%   
clear all
a=1;
tInitial    = 0.0;                      % Initial time
tFinal      = .2;                      % Final time
xleft=-2;
xright=2;
method='Forwardt-Centerx'; %'Lax-Friedrichs  '; 
%method='Lax-Friedrichs  '; 
      
h=.01;
lambda=.5;
k=h*lambda;
%nx=200;% Number of spacesteps
%nt=400;% Number of timesteps


nx=round((xright-xleft)/h);
nt=round((tFinal-tInitial)/k);
%lambda=k/h
nx1=nx+1; %number of space points counting the endpoints

%initialization
xvec(1:nx1,1)=(1:nx1)*h+(xleft-h);
tvec(1:nt,1) =(1:nt)*k+ tInitial;
for m=1:nx1
    if abs(xvec(m,1))<1
        u0(m,1)=1-abs(xvec(m,1));
    else
        u0(m,1)=0;
    end
end
%////////////////////////////////////////////////////////
%     Computing the solution     
%////////////////////////////////////////////////////////
%
uold=u0;
for(n = 1:nt)
    u(1,n)=0;u(nx1,n)=0; % boundary conditions
for(m = 2:nx)
    if method==    'Forwardt-Centerx';
        u(m,n)=uold(m,1)-.5*lambda*a*(uold(m+1,1)-uold(m-1,1));
    elseif method=='Lax-Friedrichs  '; 
        u(m,n)=.5*(uold(m+1,1)+uold(m-1,1))-.5*lambda*a*(uold(m+1,1)-uold(m-1,1));
    end
end
uold(1:nx1,1)=u(1:nx1,n);
end     
%
%    Set plot limits 
%
xPlotMin =     xleft;
xPlotMax =     xright;

% plot, then scale by calling axis command 
nplot=nt;
uplot(1:nx1,1)=u(1:nx1,nplot);
tplot=tvec(nplot);
figure(1)
plot(xvec,uplot);
xlabel('x')
ylabel('u')
title(['Solution of 1-way wave eqtn at t=' num2str(tplot) ' using ' method ' with \lambda=' num2str(lambda) ', \Delta x= h=' num2str(h)])




