%EX_4_10 simulates the perfromance of the min-max controller
%connected to the vehicle model.  The script
%uses the VEH_MMX5 file

%   last modified by S. H. Zak on November 20, 2002

clear
clc
disp('WARNING!')
disp('It takes time to simulate a system with the min-max controller  because of the presence of the sign function.')
disp('To accelerate the simulations, an approximation of the sign function is used.')
disp('The accuracy of the approximation is controlled by the parameter mu---see the file VEH_MMX5.')
disp('The smaller the parameter mu, the better the approximation.')
disp('Press ENTER to start simulations and wait...')
pause
tspan=[0 4];
x_0=[0 0 0 0 0 0 0 0]';
[t,x]=ode23s('VEH_MMX5',tspan,x_0);
subplot(2,1,1)
plot(t,x(:,4))
axis([0 4 0 5])
set(gca,'Fontsize',12)
grid
xlabel('Time (sec)','Fontsize',12)
title('Actual output in Example 4.10.','Fontsize',12)
hold on
plot(t,4*(t>=1))
subplot(2,1,2)
plot(t,x(:,8))
axis([0 4 0 5])
set(gca,'Fontsize',12)
grid
xlabel('Time (sec)','Fontsize',12)
title('Desired output in Example 4.10.','Fontsize',12)
hold on
plot(t,4*(t>=1))
eta=-0.1653;
a=.5;
r=4*(t>=1);
rho=abs(0.0329*x(:,1)+2.2854*x(:,2)+0.2429*x(:,3)-0.1653*x(:,4))+abs(r*eta);
f=0.1571*(x(:,1)-x(:,5))+26.9931*(x(:,2)-x(:,6))+6.3324*(x(:,3)-x(:,7))-3.025*(x(:,4)-x(:,8));
%u=-rho.*(f./(abs(f)+.00001));
u=-rho.*sign(f);
figure
plot(t,u)
set(gca,'Fontsize',12)
grid
xlabel('Time (sec)','Fontsize',12)
ylabel('{\it u}','Fontsize',12)
title('Controller action in Example 4.10.','Fontsize',12)