% Solution of the 1d Burgers' equation
% u_t+u*u_x=0 or u_t+0.5*(u^2)_x=0
% Initial condition: u(x,0)= ;
% x in (0,a) with
% periodic boundary conditions: u(0,t)=u(a,t).
%-----------------------------
% The Riemann Problem: Shocks 
%-----------------------------
% Upwind method with F=u^2/2
%
clear all
close all
a=10;
dx=0.05;
dt=0.05;
T=80; % Tc=23
h=dt/(dx);
n=a/dx;
tt=10;
ul=0.8;
ur=0.2;
%---------------------------------
x=0:dx:a;
%u0=exp(-(x-3).^2);%0.5*sin(2*pi*x/a);%exp(-(x-2).^2); % Initial condition

 u0(1:101)=ul;
u0(101:n+1)=ur;
 plot(x,u0,'r','LineWidth',2)
hold on
axis([0 a 0 1.1])
u(1,:)=u0;

 for t=1:T
     u(t+1,1)=ul;
     u(t+1,n+1)=ur;
%     %u(t+1,1)=u(t,1)-h*(u(t,1)^2-u(t,n+1)^2);
     for i=2:n
         u(t+1,i)=u(t,i)-h*(u(t,i)^2-u(t,i-1)^2);
     end;
     if mod(t,tt)==0
        plot(x,u(t+1,:),'b','LineWidth',2);
       title(['t=' num2str(t)],'Color','b')
       axis([0 a 0 1.1])
      hold on
       pause(0.5)
       %plot(x,u(t+1,:),'r','LineWidth',2);
       xlabel('x')
      ylabel('u(x,t)')
    end;
end;
 plot(x,u(t,:),'r','LineWidth',2);

% %---------------------
s=(ul+ur)/2;
%---------------------
x0(1)=0;
y0(1)=6;
for i=1:5
% u0(i)=exp(-(x0(i)-1).^2) 
figure(2)
plot(x,(x-x0(i))/ul,'b','LineWidth',2)
xlabel('x')
ylabel('t')
hold on
plot(x,(x-y0(i))/ur,'b','LineWidth',2)
x0(i+1)=x0(i)+1;
y0(i+1)=y0(i)+1;
end;
plot(x,(x-(a/2))/s,'r','LineWidth',3)
 axis([0 a 0 10])