function [Ap,Bp,Cp,Dp] = sectf(Z1,Z2,Z3,Z4,Z5,Z6,Z7,Z8)
% [SS_P] = SECTF(SS_,d11,a,b,Type) or
% [Ap,Bp,Cp,Dp] = SECTF(A,B,C,D,d11,a,b,Type) performs multivariable
%   sector bilinear transformation on input-output channel 1 of a plant
%   P(s) such that the original input-output pairs (U1,Y1) in sector[a,b]
%   is mapped to another sector[x,y], or vice versa. 
%
%                           |A  B1  B2 |
%           P(s) := |A B| = |C1 D11 D12| 
%                   |C D|   |C2 D21 D22|
%
%           d11 = any matrix of same size as D11
%
%   Four options are provided: (must have 0 < b <= inf, a < b)
%
%       Type = 1, sector(a,b) -----> sector(0,inf) = positive real problem
%       Type = 2, sector(a,b) -----> sector(-1,1)  = small gain problem
%       Type = 3, sector(0,inf) ---> sector(a,b) (inverse map of type 1)
%       Type = 4, sector(-1,1) ----> sector(a,b) (inverse map of type 2)
%

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

inargs = '(A,B,C,D,d11,a,b,Type)';
eval(mkargs(inargs,nargin,'ss'))

[rd11,cd11] = size(d11);
[ra,ca] = size(A);
[rb,cb] = size(B);
[rc,cc] = size(C);
m = rd11; cb2 = cb-m; rc2 = rc-m;
%
if rd11 ~= cd11
   disp('    D11 NONSQUARE !! NO SECTOR TRANSFORMATION EXISTS !!')
   return
end
% ---------------------------------------------
if Type == 1
   q = a; r = 1/b;
end
%
if Type== 2
   q1 = a; r1 = 1/b;
   q2 = 1; r2 = -1;
end
%
if Type== 3
   q = -a; r = -1/b;
end
%
if Type== 4
   q1 = -1; r1 = 1;
   q2 = -a; r2 = -1/b;
end
% ---------------------------------------------
B = [B zeros(rb,2)];
C = [C;zeros(2,cc)];
M = zeros(m+cb2+m+m,m+cb2);
N = zeros(m+rc2,m+rc2+m+m);
F = zeros(m+cb2+m+m,m+rc2+m+m);
F(1:m,m+rc2+m+1:m+rc2+m+m) = eye(m);
F(m+cb2+m+1:m+cb2+m+m) = eye(m);
% ---------------------------------------------
if cb2 ~= 0 & rc2 ~= 0
   M(m+1:m+cb2,m+1:m+cb2) = eye(cb2);
   M(m+cb2+1:m+cb2+m,1:m) = eye(m);
   N(1:m,m+rc2+1:m+rc2+m) = eye(m);
   N(m+1:m+rc2,m+1:m+rc2) = eye(rc2);
else
   M(m+cb2+1:m+cb2+m,1:m) = eye(m);
   N(1:m,m+rc2+1:m+rc2+m) = eye(m);
end
% ----------------------------------------------
if Type == 1 | Type == 3
   K = [-q 1-q*r;1 r];
   D = [D zeros(rc,2);zeros(2,cb) K];
   [Ap,Bp,Cp,Dp] = interc(A,B,C,D,M,N,F);
end
%
if Type == 2 | Type == 4
   K1 = [-q1 1-q1*r1;1 r1];
   D = [D zeros(rc,2);zeros(2,cb) K1];
   [A,B,C,D] = interc(A,B,C,D,M,N,F);
   K2 = [-q2 1-q2*r2;1 r2];
   D = [D zeros(rc,2);zeros(2,cb) K2];
   [Ap,Bp,Cp,Dp] = interc(A,B,C,D,M,N,F);
end
%
if xsflag
   ss_p = mksys(Ap,Bp,Cp,Dp);
   Ap = ss_p;
end
%

% ---------- End of SECTF88.M --- RYC/MGS 1988 (Rev. 03/06/92)
