% 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.


%% Example 1.
% Original plant
Go = zpk([-1/0.338], [-1/10.7643 -1/3.3344 ], -0.00188/106.1908, 'ioDelay', 0, '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)

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

%% Example 2.
% Original plant
Go = zpk([1/97.4], [-1/17.4825 -1/17.4825], 1.1313e-004, 'ioDelay', 0, '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,50) % Desired closed-loop time constant: tauc = 50

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

%% Example 3.
% Original plant
Go = zpk([1/3.79], [-1/50.5051 -1/3.202], 5.9667e-005, 'ioDelay', 0, '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) )
%% Example 4.
% Original plant
Go = zpk([1/1.7132 -1/78.125], [-1/3.7495 -1/22.5734 -1/38.3142], 2.6456e-005, 'ioDelay', 0, '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) % Desired closed-loop time constant: tauc = 0.1

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