%%%% Phase lead example
%%%% See section 11.2 in the notes
%%%% Also has the phase lag compensator of section 11.3
num_G = 1;
den_G = [1 1 0];
G = tf(num_G,den_G);
figure(1)
margin(G)
%%%% PM > 50, but cross over frequency is 0.786. Too low
figure(2)
margin(10*G)
%%%% Cross over frequency > 3, but PM = 18

%%% We want 50-18 = 32 degrees phase advance, at w = 3
w_a = 3;
alpha = (1 - sin(32*pi/180)) / (1 + sin(32*pi/180));
w_L = w_a * sqrt(alpha);
C_lead = tf([1 w_L],[alpha w_L]);

figure(3)
bode(C_lead*G)
grid on
text(3.5,-14,'gain at \omega =3 is -14dB')

k = 10^(14/20);
C_lead = C_lead * k;

figure(4)
margin(C_lead*G)

%%% Finally include the lag design for better steady state performance

num_c_lag = [1 0.05];
den_c_lag = [1 0.005];
C_lag = tf(num_c_lag,den_c_lag);
figure(5)
margin(C_lag*C_lead*G)

