%PID COntroller on Spring Mass Damper
%M=1;b=10;k=20;f=1;
%M=1;b=10;k=20;f=1;
%open loop
%_____________________________________________
%#1
%num=1;den=[1 10 20];
num=1;den=[1 10 20];
sys=tf(num,den);
step(num,den);
%______________________________________________
%Kp=300 #2
kp=300;num=kp;den=[1 10 20+kp];
sys=tf(num,den);t=0:0.01:1.2;
step(num,den,t)
%-------------RLOCUS for kp---------------
%#3
numr=[1];denr=[1 10 20];
sysr=tf(numr,denr);
rlocus(sysr);
%________________________________________________


%Next #4 try 40 and 280
kp=300;kd=20;
num=[kd kp];den=[1 10+kd 20+kp];
sys=tf(num,den);t=0:0.01:1.2
step(num,den,t)
%---------------RLOCUS for kd----------------
%#5
numr=[1 0];denr=[1 10 20+kp];
sysr=tf(numr,denr);
rlocus(sysr);

%Next
%____________________Kp with Ki______________
%#6 TRY kp=600 also and see it does not help
kp=300;ki=1000;
num=[kp ki];den=[1 10 20+kp ki];
sys=tf(num,den);t=0:0.01:1.2;
step(num,den,t)   
%------------------RLOCUS for  ki-------------------
%#7
numr=[1];denr=[1 10 20+kp 0];
sysr=tf(numr,denr);
rlocus(sysr)
%check
%#8
kp=30;ki=200;
num=[kp ki];den=[1 10 20+kp ki];
sys=tf(num,den);
step(num,den)   
%we may be at a higher frequency beyond the dip
%#9 rlocus forkp=300
kp=300;
numr=[1];denr=[1 10 20+kp 0];
sysr=tf(numr,denr);
rlocus(sysr)
%_______________________________
%#10 
kp=300;ki=200;
num=[kp ki];den=[1 10 20+kp ki];
sys=tf(num,den);
step(num,den)   
%_________________________________________

%Next
%#11 Rlocus for kd
kp=300;ki=1000;
numr=[1 0 0];denr=[1 10 20+kp ki];
sysr=tf(numr,denr);
rlocus(sysr)
%__________________________
%#12 try 200, 300, TRY kd=20,50 also
%Here between kp 300 and 600, latter gives
%better rise time.
kp=300;ki=1000;kd=40;t=0:0.01:1.8;
num=[kd kp ki];den=[1 10+kd 20+kp ki];
sys=tf(num,den);
step(num,den,t)   


kp=3750;kd=.05*kp;ki=25*kp;
num=[kd kp ki];den=[1 10+kd 20+kp ki];
sys=tf(num,den);
step(num,den,[0:.01:2])


%_________one more ziegler nichols_________
kp=3600;kd=.05*kp;ki=25*kp;
num=[kd kp ki];den=[1 10+kd 20+kp ki];
sys=tf(num,den);
step(num,den,[0:.01:2])
   
   
   
   
   
   
