% RGA = CompRGA( Gjw, outp, inp )
%           or
% CompRGA( Gjw, outp, inp )
% 
% Computation of frequency dependent relative gain array.
% 
% Outputs: RGA  - relative gain array.
% If no return arguments are supplied (equivalent to second calling
% syntax) plots of RGA elements in each row are given.
%
% 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 RGA = CompRGA( m, outp, inp )
   [mt, rd, cd, n] = minfo( m );

   if (strcmp( mt, 'vary' ) == 1) & ( max(size(outp)) > 1 )
   
      if nargin < 3
         inp = ':';
      end
      if nargin < 2
         outp = ':';
      end
      
      if nargin < 1
         disp( 'Usage: RGA = CompRGA( m, outp, inp )' )
      end

      ms = sel(m, outp, inp);
      ms_inv_trsp = vinv( ms );
      ms_inv_trsp = transp( ms_inv_trsp );
      RGA = veval( '.*', ms, ms_inv_trsp );

      % Plot results.
      if nargout < 1
         for i=1:max(size(outp))
            % plot some of the results.
            figure( i );
   	    iv = getiv( ms );
	    vplot( 'liv,m', sel(RGA, i, ':') );	grid; 
	    title( [ 'Frequency dependent RGA for output ' int2str(i) '.' ] );
            xlabel ('Frequency [rad/s].');
            ylabel ('Magnitude');
            v = axis;
	    v(1) = iv(1);
	    v(2) = iv(max(size(iv)));
	    axis( v );
	 end
      end
      
   end
   
end
