function [AD,BD1,BD2,CCD1,CCD2,DD11,DD12,DD21,DD22] = augd(Z1,Z2,Z3,Z4,Z5,Z6,Z7,Z8,Z9,Z10,Z11,Z12,Z13)
%
% [TSS_D] = AUGD(TSS_,SS_D) or
% [AD,BD1,BD2,CCD1,CCD2,DD11,DD12,DD21,DD22] = AUGD(A,B1,B2,C1,C2,..
% D11,D12,D21,D22,AD,BD,CCD,DD) produces a two-port augmented plant
%  TSS_D that absorbs the scaling matrices D(s) and inv(D(s))
%  such that
%                                -1
%        (TSS_D) := D(s) P(s) D(s)    
%                 = MKSYS(AD,BD1,BD2,CCD1,CCD2,DD11,DD12,DD21,DD22,'tss');
%
%
%  Input Data: P = TSS_ = SYSTEM(A,B1,B2,C1,C2,D11,D12,D21,D22,'tss')
%              D = SS_D = SYSTEM(AD,BD,CCD,DD);

% R. Y. Chiang & M. G. Safonov 
% Copyright (c) 1988 by the MathWorks, Inc.
% All Rights Reserved.

inargs = '(a,b1,b2,c1,c2,d11,d12,d21,d22,ad,bd,ccd,dd)';
eval(mkargs(inargs,nargin,'tss,ss'));

if exist('ad')==2,ad=[];end  % This takes care empty matrix bug in eval

if min(size(ad))==0
  di=inv(dd);
  AD = a;
  BD1 = b1*di;
  BD2 = b2;
  CCD1 = dd*c1;
  CCD2 = c2;
  DD11 = dd*d11*di;
  DD12 = dd*d12;
  DD21 = d21*di;
  DD22 = d22;
else
  [ai,bi,ci,di] = ssinv(ad,bd,ccd,dd);

  %
  xd = size(ad)*[1;0];
  xp = size(a)*[1;0];
  u1 = size(b1)*[0;1];
  u2 = size(b2)*[0;1];
  y1 = size(c1)*[1;0];
  y2 = size(c2)*[1;0];

  AD = [ai        zeros(xd,xp) zeros(xd,xd);
        b1*ci     a                   zeros(xp,xd);
        bd*d11*ci bd*c1               ad];
  BD1 = [bi;b1*di;bd*d11*di];
  BD2 = [zeros(xd,u2);b2;bd*d12];
  CCD1 = [dd*d11*ci dd*c1 ccd];
  CCD2 = [d21*ci c2 zeros(y2,xd)];
  DD11 = dd*d11*di;
  DD12 = dd*d12;
  DD21 = d21*di;
  DD22 = d22;
end
%
if xsflag
   AD = mksys(AD,BD1,BD2,CCD1,CCD2,DD11,DD12,DD21,DD22,'tss');
end
%
% ------ End of AUGD.M % RYC/MGS %