%Example 6.9
%Triangular plant
G = [1 100; 0 1];
COND=cond(G)  %10^4
RGA=vrga(G)   %identity
CONDMIN=condmin(G) %1
CONDMINI=condmini(G) %200

%Inverse based controller
c=1; dynk = nd2sys(1,[1 1.e-6],c); Dynk = daug(dynk,dynk);
Kinv = mmult(Dynk,minv(G));

% Worst case computed with skrpmu.
Wi = daug(0.2,0.2);
systemnames = 'G Kinv Wi';
inputvar = '[di(2); r(2)]';
outputvar = '[Wi; r-G]';
input_to_G = '[Kinv+di]';
input_to_Kinv = '[r-G]';
input_to_Wi = '[Kinv]';
sysoutname = 'Pmsku';
cleanupsysic = 'yes';
sysic;

omega=logspace(-1,1,31);
Mjw = frsp(Pmsku, omega);
blk = [1 1;1 1; 2 2]; 
[skm, bnds, dvec, sens, pvec, gvec] = skrpmu(Mjw,blk);
skm_diag=pkvnorm(skm)  %||S'||=20.43
%Alternatively using wcperf in mu-tool (v2.0):
[delworst,mulow,muup] = wcperf(Mjw,blk,1);
wcp_diag=vunpck(vinterp(muup,[1 1;1 Inf],1)); %||S'||=20.43

% Check with full-block uncertainty
blk = [2 2; 2 2]; 
[skm, bnds, dvec, sens, pvec, gvec] = skrpmu(Mjw, blk);
skm_full=pkvnorm(skm)  %||S'||=1020.6
%Alternatively using wcperf in mu-tool (v2.0):
[delworst,mulow,muup] = wcperf(Mjw,blk,1);
wcp_full=vunpck(vinterp(muup,[1 1;1 Inf],1)); %||S'||=1020.6


%Results:
clc
disp(sprintf('Condition number of G: %7.1f',COND)); %10^4
disp(sprintf('RGA(G)=[%5.1f%5.1f]\n       [%5.1f%5.1f]',RGA')); %identity
disp(sprintf('Minimized condition number: %5.1f',CONDMIN)); %1
disp(sprintf('Input-minimized condition number: %6.1f',CONDMINI)); %200
disp('Worst-case performance, diagonal uncertainty:');
disp(sprintf('        Using skrpmu: %8.2f',skm_diag)); %20.43
disp(sprintf('        Using wcperf: %8.2f',wcp_diag)); %20.43
disp('Worst-case performance, full-block uncertainty:');
disp(sprintf('        Using skrpmu: %8.2f',skm_full)); %1020.6
disp(sprintf('        Using wcperf: %8.2f',wcp_full)); %1020.6

