Using the series lag compensation method, the system speed error is set to 5, with a phase margin greater than 50 degrees and a gain margin greater than 10 dB.
%chuanlianzhihoujiaozhengnum1=5*1;%expected speed error xishuden1=conv(conv([1,0],[0.2,1]),[0.8,1]);%denominator polynomial [Gn1,Ph1,Wg1,Wp1]=margin(num1,den1);%desired phase margin PH_1=50;%expected gain margin GN_1=10;%expected value w1=logspace(-3,1,1000);%frequency variable [mag1,pha1]=bode(num1,den1,w1);for ang1=5:1:15%angle offset ph_i=PH_1+ang1-180 ; [min1,id1]=min(abs(pha1-ph_i)) ;%cross frequency w_c1=w1(id1) ; a1=mag1(id1) ; T1=5/w_c1 ; %time constant num_c=[T1,1]; den_c=[a1*T1,1]; [num2,den2]=series(num1,den1,num_c,den_c) ;%series compensation [Gn2,Ph2,Wg2,Wp2]=margin(num2,den2);%desired phase margin if Ph2>PH_1 break; endend[mag2,pha2]=bode(num2,den2,w1);subplot(2,1,1);semilogx(w1,20*log10(mag1),’b’,w1,20*log10(mag2),’r’) ;grid;ylabel(‘Magnitude (dB)’);title(‘Blue Line – Before Compensation, Red Line – After Compensation’);subplot(2,1,2);semilogx(w1,pha1,’b’,w1,pha2,’r’) ;grid;ylabel(‘Phase (°)’);xlabel(‘Frequency (rad/s)’);disp([‘Before Compensation: Gain Margin=’,num2str(20*log10(Gn1)),’dB’,’,’,’Phase Margin=’,num2str(Ph1),’°’]);disp([‘After Compensation: Gain Margin=’,num2str(20*log10(Gn2)),’dB’,’,’,’Phase Margin=’,num2str(Ph2),’°’]);