%Lecture 7, lead compensation on Spring Mass Damper
M=1;b=10;k=20;f=1;
%open loop
%_____________________________________________
%#1
num=1;den=[1 10 20];
[numc,denc]=cloop(num,den,-1);
sys=tf(numc,denc);
step(numc,denc);
%Next
%____________________only with Ki______________
%#2
kp=0;ki=10;
num=[kp ki];den=[1 10 20 0];
[numc,denc]=cloop(num,den);
sys=tf(numc,denc)
t=0:0.01:8;
step(numc,denc,t)   
%Next
%____________________only with Ki______________
%#3 raise the gain
kp=0;ki=100;
num=[kp ki];den=[1 10 20 0];
[numc,denc]=cloop(num,den);
sys=tf(numc,denc)
t=0:0.01:8;
step(numc,denc,t)   
%a separate additional attempt
numcom=[1 1]; dencom=[1 2];
nnum=conv(num,numcom);nden=conv(den,dencom);
[numc,denc]=cloop(nnum,nden);
step(numc,denc)
%____________________kp with Ki______________
%#4 raise the gain
%------------------------------------
%lead Compensator design stage 1
%try numcom=[1 1.4]/2 or [1 1]/2
%tweak around the given value below.
a=13.92;om=5.93; T=1/(sqrt(a)*om);
numcom=[a*T 1]; dencom=[T 1];
nnum=conv(num,numcom);nden=conv(den,dencom);
[numc,denc]=cloop(nnum,nden);
step(numc,denc)
%------------------------------------
%lead Compensator design stage 2 -4.7712
a=3;om2=8.50; T2=1/(sqrt(a)*om);
numcom=[a*T2 1]; dencom=[T2 1];
nnum2=conv(nnum,numcom);nden2=conv(nden,dencom);
[numc,denc]=cloop(nnum2,nden2);
step(numc,denc)


