%Lecture on Cruise Control.
%case 1
m=1000;b=50;f=500;u=10;num=[1];den=[m b];
step(num*f,den)
%________________________________________
%Proportional Controller Kp=100
Kp=100;num=[Kp];den=[m b+Kp];Ki=0;
step(u*num,den)
%________________________________________
%Proportional Controller Kp=10000
Kp=600;num=[Kp];den=[m b+Kp];
step(u*num,den)

%________________________________________
%Proportional Integral Controller Kp=600,Ki=1;
Kp=600;Ki=5;num=[Kp Ki];den=[m b+Kp Ki];
t=0:0.01:200;
step(u*num,den,t)
%step(u*num,den)
%_________________________________________
Kp=600;Ki=40;num=[Kp Ki];den=[m b+Kp Ki];
t=0:0.01:200;
step(u*num,den,t)
%_______________________________________
Kp=600;Ki=200;num=[Kp Ki];den=[m b+Kp Ki];
t=0:0.01:200;
step(u*num,den,t)
%________________________________________
Kp=100;Ki=40;num=[Kp Ki];den=[m b+Kp Ki];
t=0:0.01:200;
step(u*num,den,t)
%over above
%________________________________________
Kp=200;Ki=100;num=[Kp Ki];den=[m b+Kp Ki];
t=0:0.01:200;
step(u*num,den,t)
%________________________________________
%For the purpose of showing derivative control
%Further raising Ki has reduced rise time.
Kp=800;Ki=40;num=[Kp Ki];den=[m b+Kp Ki];
t=0:0.01:20;
step(u*num,den,t)
%______________________________________________
%Derivative controller
Kp=600;Ki=200;Kd=400;num=[Kd Kp Ki];den=[m+Kd b+Kp Ki];
t=0:0.01:2;
step(u*num,den,t)
%______________________________________
%Derivative controller final Kp=1500 isOK
Kp=1500;Ki=100;Kd=400;num=[Kd Kp Ki];den=[m+Kd b+Kp Ki];
t=0:0.01:200;
step(u*num,den,t)
%Kp=1500, Ki=100, Kd=1 isOK
%Kp=3500, Ki=100, Kd=1 isOK


%________________________________________
%For root locus 
Kp=200;
numr=[1];denr=[m b+Kp 0];
sys=tf(numr,denr)
rlocus(sys)
%___________________________________
Kp=800;Ki=40;
numr=[1 0 0];denr=[m b+Kp Ki];
sys=tf(numr,denr)
rlocus(sys)

xi=-0.5;omn=100;t=0:0.01:1;
re=1+exp(-xi*omn*t)/sqrt(1-xi*xi).*sin(omn*sqrt(1-xi*xi)*t -atan2(sqrt(1-xi*xi),(-xi)));