%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Exercise 3.8 Combined input and output unc. for inverse-based control
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%Plant (3.82): distilation column, G(s)=1/(75s+1)[87 -86.4;108.2 -109.6]
G0 = [87.8 -86.4; 108.2 -109.6];
dyn = nd2sys(1,[75 1]); Dyn = daug(dyn,dyn); G = mmult(Dyn,G0);

% Inverse-based controller (3.84)
dynk = nd2sys([75 1],[1 1.e-6],0.7); Dynk = daug(dynk,dynk);
Kinv = mmult(Dynk,minv(G0)); K = Kinv;

% TIME  simulation
% Nominal
GK = mmult(G,K); I2=eye(2); S = minv(madd(I2,GK)); T = msub(I2,S);
kr=nd2sys(1,[5 1]); Kr=daug(kr,kr); Tr = mmult(T,Kr);
y = trsp(Tr,[1;0],100,.1);
u = trsp(mmult(K,S,Kr),[1;0],100,.1);
% With relative uncertainty del both at the input and output
del=0.12;  % Inverse-controller: Unstable for del larger than 0.12
Unci = [1+del 0; 0 1-del]; Unco = [1-del 0; 0 1+del];
GKu = mmult(Unco,G,Unci,K);
Su = minv(madd(I2,GKu)); Tu = msub(I2,Su); Tru = mmult(Tu,Kr);
Su=sysbal(Su);spoles(Su)
yu = trsp(Tru,[1;0],100,.1);
uu = trsp(mmult(K,Su,Kr),[1;0],100,.1);
subplot(211);vplot(y,yu,'--');title('OUTPUTS')
subplot(212);vplot(u,uu,'--');title('INPUTS');
xlabel('TIME (min)');


