function [mu,logd] = ssv(Z1,Z2,Z3,Z4,Z5,Z6,Z7)
% [MU,LOGD] = SSV(A,B,C,D,W) or SSV(A,B,C,D,W,K) or (A,B,C,D,W,K,OPTION)
% or [MU,LOGD] = SSV(SS_,...), etc. produces the row-vector MU
% containing an upper bound on the structured singular value (ssv) of 
%        G(jw) = C * inv(jwI-A) * B + D
% at each frequency in the real vector w. 
% Input: 
%     A,B,C,D  -- state-space matrices of pxq transfer function matrix G(s)
%     W        -- vector of frequencies at which MU is to be computed
% Optional inputs:
%     K  -- uncertainty block sizes--default is K=ones(q,2).  K is an
%           an nx1 or nx2 matrix whose rows are the uncertainty block
%           sizes for which the ssv is to be evaluated; K must satisfy 
%           sum(K) == [q,p].If i_th uncertainty is real, set K(i,:) = [-1 -1].
%           If only the first column of K is given then the
%           uncertainty blocks are taken to be square, as if K(:,2)=K(:,1).
%           
%     OPTION  -- method for computing MU, one of the following: 'psv' (by
%            default) or 'osborne' or 'perron' (if only "MU" output specified)
%            or 'muopt' (multiplier approach for real/complex uncertainties).
% Outputs:
%     MU      -- the bode plot of MU
%     LOGD    -- the optimal diagonal scaling D, (exp(LOGD) is the value)

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

nag1 = nargin;
inargs = '(a,b,c,d,w,k,option)';
eval(mkargs(inargs,nag1,'ss'))
nag1 = nargin;    % NARGIN may have been changed
nag2 = nargout;

if nag1 == 5 
   [rd,cd] = size(d);
   k = ones(cd,2);
   option = 'psv';
end

if isempty(k)
   [rd,cd] = size(d);
   k = ones(cd,2);
end;

fprintf('Excuting SSV.');
if nag1 == 6
   option = 'psv';
end

if nag2 == 1 & nag1 < 7
   option = 'perron';
end

if min(k(:,1)) < 0 
   option = 'muopt';
end

[mg] = freqrc(a,b,c,d,w);
[rmg,cmg] = size(mg);
[rd,cd] = size(d);
gg = ones(rd,cd);
[rk,ck] = size(k);
logd = zeros(rk,cmg);
%
for is = 1 : cmg
     fprintf('.');
     gg(:) = mg(:,is);
     if issame('psv',option),
        [mu(is),junk,logd(:,is)] = psv(gg,k);
     elseif issame('perron',option)
        mu(is) = perron(gg,k);
     else
        eval(['[mu(is),junk,d_db(:,is)] = ' option '(gg,k);']);
     end
end
fprintf('.Done SSV\n');
%
% ----- End of SSV.M ---- RYC/MGS 8/88 
