% function [s, c, u, v] = CompSingCond( Gjw, outp, inp )
% 
% Computation of singular value decomposition and condition 
% number for a given VARYING or Constant matrix.
% 
% Outputs: s    - singular values,
%          c    - condition number,
%          u    - output singular vectors,
%          v    - input singular vectors.
% G = u*s*v';
%
% Inputs:  Gjw  - varying matrix,
%          outp - vector which describes which rows to select,
%                 if omitted all rows are selected.
%          inp  - vector which describes which columns to select,
%                 if omitted all columns are selected.


function [sin, con, u, v] = CompSingCond( G_jw, outp, inp )
   [mt, rd, cd, n] = minfo( G_jw );
   if strcmp( mt, 'vary' ) == 1

      if nargin < 3
         inp = ':';
      end
      if nargin < 2
         outp = ':';
      end
      
      if nargin < 1
         disp( 'Usage: [s, c, u, v] = CompSingCond( Gjw, outp, inp )' )
      end
      
      [u, sin, v] = vsvd( sel(G_jw, outp, inp) );
      con = vcond( sel(G_jw, outp, inp) );

      % Plot the results if no return variables.
      if nargout < 1
         figure( 1 );
         vplot( 'liv,lm', sin ); grid;
	 v = axis;
	 iv = getiv( G_jw );
	 v(1) = iv(1);
	 v(2) = iv( max(size(iv)) );
	 axis( v );
	 title ( 'Singular values as function of frequency.');
         xlabel ('Frequency [rad/s].');
         ylabel ('Magnitude');

	 if max(size(outp)) > 1
  	    figure( 2 );
	    vplot( 'liv,m', con ); grid;
	    v = axis;
	    v(1) = iv(1);
	    v(2) = iv( max(size(iv)) );
	    axis( v );
	    title ( 'Condition number as function of frequency.');
	    xlabel ('Frequency [rad/s].');
	    ylabel ('Magnitude');
         end
      end
      
   end
end
