% code to solve the equation w_t + WW_x=0 using
% method of lines and Method of characteristics

function burgers
global N dx

set(0,                           ...
   'defaultaxesfontsize', 20,   ...
   'defaultaxeslinewidth', 1.2, ...
   'defaultlinelinewidth', 2.0, ...
   'defaultpatchlinewidth', 0.7); 

L = 4; % length of the interval
 
N = 201;  % number of grid points
dx = 2*L/(N-1);
x =[-L:dx:L];
 
% first find the numerical solution with MOL

w = Wofz(x);  % initial data

tstep = 0.25;
t_end =  1.75;
tspan = [0:tstep:t_end];
    
[T,S] = ode23s(@deRHS,tspan,w);
figure(1)
plot(x,w,'--','linewidth',2)
hold on

plot(x,S(2:end,:)','linewidth',2)
xlabel('x','fontsize',20)
ylabel('u(x,t)','fontsize',20)
  axis([-4 4 0 1.2])
  set(gca,'Xtick',-4:1:4)
%  find the exact solution using method of characteristics
for j = 1:length(T)
    t = T(j);
z = 2*Wofz(x)*t+x;
 
 if (j==1)
figure(2)
plot(z,w,'--','linewidth',2)
hold on
 else
     figure(2)
plot(z,w,'linewidth',2)

xlabel('x','fontsize',20)
ylabel('u(x,t)','fontsize',20)
axis([-4 4 0 1.2])
set(gca,'Xtick',-4:1:4)
 end
end
end
 
function s_prime=deRHS(t,w)
global N dx
W = w(2:N-1); 
Fw = -[0;W.*(w(3:N)+W)-w(1:N-2).*(W+w(1:N-2));0]/(2*dx);
 
s_prime = [Fw];

end 

function Wz = Wofz(x)

Wz = exp(-x.^2);
 
end

 
 