clear all
close all
%------------------------------
% Solution of the 1d advection equation
% u_t+c*u_x=0
% c-velocity;
% Initial condition: u(x,0)=u_0;
% x in (0,a) with
% periodic boundary conditions: u(0,t)=u(a,t).
%-----------------------------
% Analytical solution:
% u(x,t)=u0(x-ct)
%-----------------------------
%-----------------------------
% 1. Upwind method-Backward for c>0
% u_ij+1=u_ij+c*h*(u_ij-u_i-1j);
% h=dt/dx;
%-----------------------------
c=0.5;
a=10;
dx=0.1;
dt=0.05;
T=200;
h=dt/dx;
n=a/dx;
tt=50;
c*h
%--------------
x=0:dx:a;
u0=exp(-(x-2).^2);%0.5*sin(2*pi*x/a);%exp(-(x-8).^2);%0.5*sin(2*pi*x/a);%exp(-(x-8).^2); % Initial condition
plot(x,u0,'b','LineWidth',2)
hold on
text(1.95,1.01,'t=0',...
     'HorizontalAlignment','left')
u(1,:)=u0;
%---------------
ah=(-c*h+1)*ones(n+1,1);
au=c*h*ones(n,1);
A=diag(ah)+diag(au,-1);
A(1,n+1)=c*h;
%----------------
for t=1:T
    um=A*u(t,:)'; % 
    u(t+1,:)=um';
    if mod(t,tt)==0
       plot(x,u(t+1,:),'r','LineWidth',2);
      % title(['t=' num2str(t)],'Color','b')
       axis([0 a 0 1.1])
       hold on
       pause(0.5)
       xlabel('x','FontName','Times New Roman','FontSize',16)
       ylabel('u(x)','FontName','Times New Roman','FontSize',16)
    end;
end;
  text(3,0.93,'t=50',...
     'HorizontalAlignment','left')
 text(4.3,0.87,'t=100',...
     'HorizontalAlignment','left')
 text(5.5,0.82,'t=150',...
     'HorizontalAlignment','left')
 text(6.8,0.78,'t=200',...
     'HorizontalAlignment','left')   

