% Demonstration examples for using SIMC model reduction (see simc_reduce.m) and
% PID tuning (see simc_pidtuning.m) rules as described in Multivariable Feedback
% Control by Skogestad and Postlethwaite.

% Author(s): Bora Eryilmaz, The MathWorks, Inc.

%% Example 2.15, page 58.
% Original plant
Go = zpk(1/0.8, [-1/6 -1/2.5 -1/2.5 -2.5], -3/18.75, 'ioDelay', 1.2, 'DisplayFormat', 'time')

% First-order model approximation using SIMC model reduction rules.
G1 = simc_reduce(Go,1)
% PI controller design using SIMC tuning rules.
K1 = simc_pidtune(G1)

% Second-order model approximation using SIMC model reduction rules.
G2 = simc_reduce(Go,2)
% PID controller design using SIMC tuning rules.
K2 = simc_pidtune(G2)

% Closed-loop step responses.
figure(1); step( feedback(K1*ss(G1),1), feedback(K2*ss(G2),1) )

%% Example 2.16, page 59.
% Original plant
Go = zpk([], [-0.1 -20 -20], 8000,'DisplayFormat', 'time')

% First-order model approximation using SIMC model reduction rules.
G1 = simc_reduce(Go,1)
% PI controller design using SIMC tuning rules.
K1 = simc_pidtune(G1,0.1) % Desired closed-loop time constant: tauc = 0.1

% Second-order model approximation using SIMC model reduction rules.
G2 = simc_reduce(Go,2)
% PID controller design using SIMC tuning rules.
K2 = simc_pidtune(G2,0.1) % Desired closed-loop time constant: tauc = 0.1

% Closed-loop step response.
figure(2); step( feedback(K1*ss(G1),1), feedback(K2*ss(G2),1) )

%% Model with positive numerator time constants.
% Original plant
Go = zpk([-1/6 -1/3], [-0.1 -1/8 -1], 0.225, 'ioDelay', 0.3, 'DisplayFormat', 'time')
% First-order model approximation using SIMC model reduction rules.
[G1,k,th,t1] = simc_reduce(Go,1)
% PI controller design using SIMC tuning rules.
[K1,Kc,ti] = simc_pidtune(G1)

% Closed-loop step response.
figure(3); step( feedback(K1*ss(G1),1) )
