% 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)=u_0;
% x in (0,a) with
% periodic boundary conditions: u(0,t)=u(a,t).
%-----------------------------
% Analytical solution:
% u(x,t)=u0(x-u0(x0)*t,t)
%-----------------------------
%-----------------------------
% Upwind method with F=u^2/2
%
clear all
close all
a=10;
dx=0.05;
dt=0.05;
T=40; % Tc=23
h=dt/(2*dx);
n=a/dx;
tt=5;
%---------------------------------
x=0:dx:a;
u0=exp(-(x-3).^2);%0.5*sin(2*pi*x/a);%exp(-(x-2).^2); % Initial condition
% T=1.16
plot(x,u0,'r','LineWidth',2)
%hold on
axis([0 a 0 1.1])
% text(1.8,1.01,'t=0',...
%      'HorizontalAlignment','left')
u(1,:)=u0;

for t=1:T
    u(t+1,1)=u(t,1)-h*(u(t,1)^2-u(t,n+1)^2);
    for i=2:n+1
        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);