function[gc]=leadbode(g,K,PM,GM) %This function using the Bode diagram make the phase lead compensation, %input is tf, Kp(or Kv, Ka) (if have no K, Kc=1),phase margin and gain %margin which expect. %-------------------------------------------------------|% %|(C)2005 Bui Trung Hieu |% %|Website: www.khvt.com |% %|Email: hieu@khvt.com |% %-------------------------------------------------------|% if (nargin==3) GM=PM; PM=K; Kc=1; elseif (nargin==4) Kc=Kc_error(g,K); end %Now, find margin g1=Kc*g; [gm1,Pm,wcg1,wcp1]=margin(g1); clear gm1 wcp1 wcg1 %Step 4 in course, loop at here... %Phase angle adder %Step 5 theta=2; max_ang=30; while(theta89*pi/180) Result=sprintf('Bai toan vo nghiem') gc=nan; break; end %Step 6 anpha=(1+sin(phimax))/(1-sin(phimax)); %Step 7 [mag phase w]=bode(g1); clear phase mag=mag(:); w=w(:); %Find freq value which satisfies relation: Abs(G1(jw'c))=1/sqrt(anpha) temp=1/sqrt(anpha); t=find((mag>temp-7)&(mag=GM)&(pmc>=PM)) clf; hold on bode(gc,g); bode(gkq,'r') legend('Phase lead compensation','Org Tranfer Function','Compensated Tranfer Function'); margin(g*gc); pause(); clf; hold on; step(feedback(gkq,1),feedback(g,1)); grid on legend('Compensated Tranfer Function','Org Tranfer Function'); pause(); close(); break else clear gc gmc pmc wgc wpc gkq theta=theta+1; if (theta==max_ang) Result=sprintf('Bai toan vo nghiem') gc=nan; end end end %End