%% Tune PID Controller to Favor Reference Tracking or Disturbance Rejection (Command Line)
% This example shows how to use command-line PID tuning options to reduce
% overshoot in reference tracking or to improve rejection of a disturbance
% at the plant input.  Using the |pidtune| command, the example
% illustrates the tradeoff between reference tracking and
% disturbance-rejection performance in PI and PID control systems. 
%
%%
% Consider the control system of the following illustration.
%%
% 
% <<../pidtuner6.png>>
% 
%%
% Setpoint tracking is the response at _y_ to signals at _r_.  Input disturbance
% rejection is the suppression at _y_ of signals at _d_.
%
% Create a model of the plant, which for this example is given by:
%%
% 
% $$G = \frac{{0.3}}{{{s^2} + 0.1s}}.$$
% 
%%

% Copyright 2015 The MathWorks, Inc.

G = tf(0.3,[1 0.1 0]);
%%
% Design a PI controller for this plant, using a bandwidth of 0.03 rad/s.
wc = 0.03;
[C1,info] = pidtune(G,'PI',wc);
%%
% Examine the step-reference tracking and step-disturbance rejection of
% the control system using the default controller.  The disturbance
% response from _d_ to _y_ is equivalent to the response of a closed loop given by
% |feedback(G,C1)|.
T1 = feedback(G*C1,1);
GS1 = feedback(G,C1);

subplot(2,1,1);
stepplot(T1)
title('Reference Tracking')
subplot(2,1,2);
stepplot(GS1)
title('Disturbance Rejection')
%%
% By default, for a given bandwidth, |pidtune| tunes the controller to
% achieve a balance between reference tracking and disturbance rejection.
% In this case, the controller yields some overshoot in the
% reference-tracking response.  The controller also suppresses the input
% disturbance with a somewhat longer settling time than the reference tracking,
% after an initial peak.   
%
% Depending on your application, you might want to alter the balance
% between reference tracking and disturbance rejection to favor one or the
% other. For a PI controller, you can alter this balance by changing the
% phase margin of the tuned system.  The default controller returned by
% |pidtune| yields a phase margin of 60&#176;.
info.PhaseMargin
%%
% Design controllers for phase margins of 45&#176; and 70&#176; with the same
% bandwidth, and compare the resulting reference tracking and disturbance
% rejection.
opts2 = pidtuneOptions('PhaseMargin',45);
C2 = pidtune(G,'PI',wc,opts2);
T2 = feedback(G*C2,1);
GS2 = feedback(G,C2);

opts3 = pidtuneOptions('PhaseMargin',70);
C3 = pidtune(G,'PI',wc,opts3);
T3 = feedback(G*C3,1);
GS3 = feedback(G,C3);

subplot(2,1,1);
stepplot(T1,T2,T3)
legend('PM = 60','PM = 45','PM = 70')
title('Reference Tracking')
subplot(2,1,2);
stepplot(GS1,GS2,GS3)
title('Disturbance Rejection')
%% 
% Lowering the phase margin to 45&#176; speeds up disturbance rejection,
% but also increases overshoot in the reference tracking response.
% Increasing the phase margin to 70&#176; eliminates the overshoot
% completely, but results in extremely sluggish disturbance rejection.  You
% can try different phase margin values until you find one that 
% balances reference tracking and disturbance rejection suitably for your
% application.  The effect of the phase margin on this balance depends on the
% plant model.  For some plant models, the effect is not as large as
% shown in this example.
%%
% If you want to fix both the bandwidth and phase margin of your control
% system, you can still change the balance between reference tracking and
% disturbance rejection using the |DesignFocus| option of |pidtune|.  You
% can set |DesignFocus| to either |'disturbance-rejection'| or
% |'reference-tracking'| to tune a controller that favors one or the other.
%
% The |DesignFocus| option is more effective for a control system with more
% tunable parameters.  Therefore, it does not have much effect when used
% with a PI controller.  To see its effect, design a PIDF controller for
% the same bandwidth and the default phase margin (60&#176;) using each of
% the |DesignFocus| values.  Compare the results.
opts4 = pidtuneOptions('DesignFocus','balanced');   % default focus
C4 = pidtune(G,'PIDF',wc,opts4);
T4 = feedback(G*C4,1);
GS4 = feedback(G,C4);

opts5 = pidtuneOptions('DesignFocus','disturbance-rejection');
C5 = pidtune(G,'PIDF',wc,opts5);
T5 = feedback(G*C5,1);
GS5 = feedback(G,C5);

opts6 = pidtuneOptions('DesignFocus','reference-tracking');
C6 = pidtune(G,'PIDF',wc,opts6);
T6 = feedback(G*C6,1);
GS6 = feedback(G,C6);

subplot(2,1,1);
stepplot(T4,T5,T6)
legend('Balanced','Rejection','Tracking')
title('Reference Tracking')
subplot(2,1,2);
stepplot(GS4,GS5,GS6)
title('Disturbance Rejection')
%%
% When you use the |DesignFocus| option to favor reference tracking or
% disturbance rejection in the tuned control system, you can still adjust
% phase margin for further fine tuning of the balance between these two
% measures of performance.  Use |DesignFocus| and |PhaseMargin| together to
% achieve the performance balance that best meets your design requirements.
%
% The effect of both options on system performance depends strongly
% on the properties of your plant. For some plants, changing the
% |PhaseMargin| or |DesignFocus| options has little or no effect.