% Road traffic model
close all
clear all
% parametres
vm=130;
rhom=20;
rhom2=rhom^2;
% VITESSES 
subplot(3,2,1)
hold on 
% pour plots
drho=rhom/500;
rho=0:drho:rhom;
xlabel('\rho','FontSize',20)
ylabel(' c,v,q','FontSize',20,'Color','k')
% Preparation of the plot
rmin=0; 
rmax=rhom;
%axis([rmin rmax])
grid on
v=vm*(1-rho.^2/rhom2);
c=vm*(1-3*rho.^2/rhom2);
q=vm*(rho-rho.^3/rhom2);
zero=0*rho;
plot(rho,v,'-g','LineWidth',2)
plot(rho,c,'-b','LineWidth',2)
plot(rho,q,'-m','LineWidth',2)
plot(rho,zero,'-.k','LineWidth',2)
% caracteritisque
% LINEAIRE  
subplot(3,2,2)
hold on 
xlabel('x','FontSize',20)
ylabel(' t,\rho','FontSize',20,'Color','k')
xmax=50;
xmin=-xmax;
dx=.1;
k=1/5;
x=xmin:dx:xmax;
rhoim=15;
rhoa=.5;
rhoi=rhoim+rhoa*1./cosh(k*x);
cxc=vm*(1-3*rhoim^2/rhom2);
axis([xmin xmax 0 1])
for t=0:.1:1
	rhoimct=rhoa*1./cosh(k*(x-cxc*t));
	plot(x,t+0.1*rhoimct,'k','LineWidth',1)
end
for xc=xmin:5:xmax
	tau=1/cxc*(x-xc);
	plot(x,tau)
end
% DETENTES ET CHOCS
% Figure (8,2)
subplot(3,2,3)
hold on 
xlabel('x','FontSize',20)
ylabel(' t,\rho','FontSize',20,'Color','k')
xmax=50;
xmin=-xmax;
dx=.1;
k=1/5;
x=xmin:dx:2*xmax;
rhoimin=8;
rhoimax=2;
rhoi= (rhoimax-rhoimin)/2*tanh(k*x)+(rhoimin+rhoimax)/2;
axis([xmin 2*xmax 0 1])
plot(x,0.01*rhoi,'k','LineWidth',2)
for xc=xmin:5:xmax
	rhoc=(rhoimax-rhoimin)/2*tanh(k*xc)+(rhoimin+rhoimax)/2;
	cxc=vm*(1-3*rhoc^2/rhom2);
	tau=1/cxc*(x-xc);
	plot(x,tau)
end
% Figure (2,8)
subplot(3,2,4)
hold on
xlabel('x','FontSize',20)
ylabel(' t,\rho','FontSize',20,'Color','k')
xmax=50;
xmin=-xmax;
dx=.1;
k=1/5;
x=xmin:dx:2*xmax;
rhoimin=2;
rhoimax=8;
rhoi= (rhoimax-rhoimin)/2*tanh(k*x)+(rhoimin+rhoimax)/2;
axis([xmin 2*xmax 0 1])
plot(x,0.01*rhoi,'k','LineWidth',2)
for xc=xmin:5:xmax
	rhoc=(rhoimax-rhoimin)/2*tanh(k*xc)+(rhoimin+rhoimax)/2;
	cxc=vm*(1-3*rhoc^2/rhom2);
	tau=1/cxc*(x-xc);
	plot(x,tau)
end
q2=vm*(rhoimax-rhoimax^3/rhom2);
q1=vm*(rhoimin-rhoimin^3/rhom2);
w=(q2-q1)/(rhoimax-rhoimin)
y=30:dx:2*xmax;
plot(y,1/w*y,'-r','LineWidth',2)
% Figure (18,15)
subplot(3,2,5)
hold on 
xlabel('x','FontSize',20)
ylabel(' t,\rho','FontSize',20,'Color','k')
xmax=50;
xmin=-xmax;
dx=.1;
k=1/5;
x=2*xmin:dx:xmax;
rhoimin=18;
rhoimax=15;
rhoi= (rhoimax-rhoimin)/2*tanh(k*x)+(rhoimin+rhoimax)/2;
axis([2*xmin xmax 0 1])
plot(x,0.01*rhoi,'k','LineWidth',2)
for xc=xmin:5:xmax
	rhoc=(rhoimax-rhoimin)/2*tanh(k*xc)+(rhoimin+rhoimax)/2;
	cxc=vm*(1-3*rhoc^2/rhom2);
	tau=1/cxc*(x-xc);
	plot(x,tau)
end
% Figure (15,18)
subplot(3,2,6)
hold on
xlabel('x','FontSize',20)
ylabel(' t,\rho','FontSize',20,'Color','k')
xmax=50;
xmin=-xmax;
dx=.1;
k=1/5;
x=2*xmin:dx:xmax;
rhoimin=15;
rhoimax=18;
rhoi= (rhoimax-rhoimin)/2*tanh(k*x)+(rhoimin+rhoimax)/2;
axis([2*xmin xmax 0 1])
plot(x,0.01*rhoi,'k','LineWidth',2)
for xc=xmin:5:xmax
	rhoc=(rhoimax-rhoimin)/2*tanh(k*xc)+(rhoimin+rhoimax)/2;
	cxc=vm*(1-3*rhoc^2/rhom2);
	tau=1/cxc*(x-xc);
	plot(x,tau)
end
q2=vm*(rhoimax-rhoimax^3/rhom2);
q1=vm*(rhoimin-rhoimin^3/rhom2);
w=(q2-q1)/(rhoimax-rhoimin)
y=2*xmin:dx:-30;
plot(y,1/w*y,'-r','LineWidth',2)
