% Plant
%
 h = 0.1;                   % loop delay
 P = tf(41.085,[.71 1 0],'InputDelay',h);
%
% Requirements
%
 wc = 5;                    % required crossover
 PM = 0.3*180/pi;           % requuired phase margin (in deg)
%
% Reaching required crossover
%
 [mPc,phPc] = bode(P,wc);   % calculate magn & phase at wc
 C = 1/mPc;                 % proportional controller for which |P(jwc)|=1
%
% Reaching required phase margin
%
 phi = 5.7+PM-180-phPc;     % required phase lead
 if phi > 0,
    N = ceil(phi/60);       % number of lead controllers
    C = C*Clead(wc,phi,N);  % adding lead controller(s)
 end,
%
% Reaching required low-frequency gain
%
 C = C*Clag(wc,inf);        % adding lag controller
 C = C;
%%
% Some tricks to return the crossover to 3
%
%  wc = 3;                    % required crossover
%  [mPc,phPc] = bode(P*C,wc);   % calculate magn & phase at wc
%  C = C/mPc;                   % proportional controller for which |P(jwc)|=1
%
%
% Results & simulations
%
 nichols(P*C/3.5,logspace(-2,1.8,1000)), axis([-400 -90 -30 50]), grid on,
%  npo = nyquistoptions; npo.ShowFullContour = 'off';
%  nyquist(P*C,logspace(-2,3,10000),npo), axis([-2 1 -4 1.5]), axis('equal'), grid,
%  [Lgm,Lpm,Lwphi,Lwc] = margin(P*C);
%  [Lgm,Lwphi;Lpm,Lwc],                % display obtained margins
%  step(feedback(pade(P,10)*C,1))      % closed-loop step response (reference)
%  bodemag(feedback(P*C/3.5,1)), grid on, axis([.1 100 -40 10]),  % magnitude of T(jw)
%  step(feedback(P*C,1))        % closed-loop step response (disturbance)
%  bodemag(feedback(P,C)), grid on     % magnitude of Td(jw)
