% MATLAB script demonstrating the use of phlead
%% Problem Statement
% Imagine we have an unstable plant, and we want to design a control
% system for it that gives an approximate bandwidth of 10 rad/s and a phase
% margin of 60 degrees.
P = zpk(, [0 0], 1);
wc = 10;
Pm = 60;
%% Gain Calculation
% Firstly, let's work out how much we need to scale it to get crossover at
% the desired frequency.
crossoverGain = abs(freqresp(P, wc));
Kp = 1/crossoverGain;
Pw = P*Kp;
%% Phase Calculation
% Next, work out the phase lead we need to add at crossover. (We could have
% got this from the freqresp earlier, but this syntax is more general.)
% Note that we can safely ignore warnings about stability: we expect the
% stability properties to be poor until we add a phase lead compensator.
warning off Control:analysis:MarginUnstable
[~,ThisPm] = margin(Pw);
warning on Control:analysis:MarginUnstable
PhaseLead = Pm - ThisPm;
Clead = phlead(wc, PhaseLead);
%% Combine the gain and phase lead to get a single compensator
% We combine the compensator, then calculate the closed-loop properties of
% our system.
C = Clead * Kp;
L = P * Clead * Kp;
T = feedback(L, 1);
S = 1 - T;
Y = feedback(C, P); % actuator gain, a.k.a. the Youla parameter
%% Plot the results
bodemag(S, T, Y,'m--', ss(1),'k:')
bottomLeft = 3;
legend('Sensitivity', 'Complementary sensitivity', ...
'Actuator gain', bottomLeft);