home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21eb.zip / octave / SCRIPTS.ZIP / scripts / control / tf2ss.m < prev    next >
Text File  |  1999-03-05  |  3KB  |  91 lines

  1. # Copyright (C) 1996,1998 A. Scottedward Hodel 
  2. #
  3. # This file is part of Octave. 
  4. #
  5. # Octave is free software; you can redistribute it and/or modify it 
  6. # under the terms of the GNU General Public License as published by the 
  7. # Free Software Foundation; either version 2, or (at your option) any 
  8. # later version. 
  9. # Octave is distributed in the hope that it will be useful, but WITHOUT 
  10. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
  11. # FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License 
  12. # for more details.
  13. # You should have received a copy of the GNU General Public License 
  14. # along with Octave; see the file COPYING.  If not, write to the Free 
  15. # Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
  16.  
  17. function [a,b,c,d] = tf2ss(num,den)
  18.   # Conversion from tranfer function to state-space.
  19.   # The state space system
  20.   #      .
  21.   #      x = Ax + Bu
  22.   #      y = Cx + Du
  23.   #
  24.   # is obtained from a transfer function
  25.   #
  26.   #                num(s)
  27.   #          G(s)=-------
  28.   #                den(s)
  29.   #
  30.   # via the function call [a,b,c,d] = tf2ss(num,den).
  31.   # The vector 'den' must contain only one row, whereas the vector 'num'
  32.   # may contain as many rows as there are outputs of the system 'y'.
  33.   # The state space system matrices obtained from this function will be
  34.   # in controllable canonical form as described in "Modern Control Theory",
  35.   # [Brogan, 1991].
  36.  
  37.  
  38.   # Written by R. Bruce Tenison (June 22, 1994) btenison@eng.auburn.edu
  39.   # mod A S Hodel July, Aug  1995
  40.  
  41.   if(nargin != 2)        error("tf2ss: wrong number of input arguments")
  42.   elseif(isempty(num))   error("tf2ss: empty numerator");
  43.   elseif(isempty(den))   error("tf2ss: empy denominator");
  44.   elseif(!is_vector(num)) 
  45.     error(sprintf("num(%dx%d) must be a vector",rows(num),columns(num)));
  46.   elseif(!is_vector(den)) 
  47.     error(sprintf("den(%dx%d) must be a vector",rows(den),columns(den)));
  48.   endif
  49.  
  50.   # strip leading zeros from num, den
  51.   nz = find(num != 0);
  52.   if(isempty(nz)) num = 0;
  53.   else num = num(nz(1):length(num));         endif
  54.   nz = find(den != 0);
  55.   if(isempty(nz)) error("denominator is 0.");
  56.   else den = den(nz(1):length(den));         endif
  57.  
  58.   # force num, den to be row vectors
  59.   num = vec(num)';        den = vec(den)';
  60.   nn = length(num);       nd = length(den);
  61.   if(nn > nd) error(sprintf("deg(num)=%d > deg(den)= %d",nn,nd)); endif
  62.  
  63.    # Check sizes
  64.    if (nd == 1)      a = []; b = []; c = []; d = num(:,1) / den(1); 
  65.    else
  66.     # Pad num so that length(num) = length(den)
  67.     if (nd-nn > 0) num = [zeros(1,nd-nn), num]; endif
  68.  
  69.     # Normalize the numerator and denominator vector w.r.t. the leading 
  70.     # coefficient
  71.     d1 = den(1);    num = num / d1;    den = den(2:nd)/d1;
  72.     sw = nd-1:-1:1;
  73.  
  74.     # Form the A matrix
  75.     if(nd > 2)      a = [zeros(nd-2,1),eye(nd-2,nd-2);-den(sw)];
  76.     else            a = -den(sw);                                endif
  77.  
  78.     # Form the B matrix
  79.     b = zeros(nd-1,1);           b(nd-1,1) = 1;
  80.  
  81.     # Form the C matrix
  82.     c = num(:,2:nd)-num(:,1)*den;        c = c(:,sw);
  83.  
  84.     # Form the D matrix
  85.     d = num(:,1);
  86.   endif
  87.  
  88. endfunction
  89.