home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21fb.zip / octave / SCRIPTS.ZIP / scripts / control / zp2ssg2.m < prev    next >
Encoding:
Text File  |  1999-12-15  |  2.1 KB  |  68 lines

  1. ## Copyright (C) 1996,1998 Auburn University.  All Rights Reserved
  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. ## 
  10. ## Octave is distributed in the hope that it will be useful, but WITHOUT 
  11. ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
  12. ## FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License 
  13. ## for more details.
  14. ## 
  15. ## You should have received a copy of the GNU General Public License 
  16. ## along with Octave; see the file COPYING.  If not, write to the Free 
  17. ## Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. 
  18.  
  19. ## -*- texinfo -*-
  20. ## @deftypefn {Function File } { [@var{poly}, @var{rvals}] =} zp2ssg2 (@var{rvals})
  21. ## Used internally in @code{zp2ss}
  22. ## Extract 2 values from @var{rvals} (if possible) and construct
  23. ##  a polynomial with those roots.
  24. ## @end deftypefn
  25.  
  26. function [poly, rvals] = zp2ssg2 (rvals)
  27.  
  28.   ## A. S. Hodel Aug 1996
  29.  
  30.   ## locate imaginary roots (if any)
  31.   cidx = find(imag(rvals));
  32.  
  33.   if(!isempty(cidx))
  34.     ## select first complex root, omit from cidx
  35.     r1i = cidx(1);      r1 = rvals(r1i);     cidx = complement(r1i,cidx);
  36.  
  37.     ## locate conjugate root (must be in cidx list, just in case there's
  38.     ## roundoff)
  39.     err = abs(rvals(cidx) - r1');
  40.     minerr = min(err);
  41.     c2i = find(err == minerr);
  42.     r2i = cidx(c2i);
  43.     r2 = rvals(r2i);
  44.     cidx = complement(r2i,cidx);
  45.  
  46.     ## don't check for divide by zero, since 0 is not complex.
  47.     if(abs(r2 - r1')/abs(r1) > 1e-12)
  48.       error(sprintf("r1=(%f,%f); r2=(%f,%f), not conjugates.", ...
  49.     real(r1),imag(r1),real(r2),imag(r2)));
  50.     endif
  51.  
  52.     ## complex conjugate pair
  53.     poly = [1, -2*real(r1), real(r1)^2+imag(r1)^2];
  54.   else
  55.     ## select two roots (they're all real)
  56.     r1 = rvals(1);
  57.     r2 = rvals(2);
  58.     poly = [1, -(r1+r2), (r1*r2)];
  59.     r1i = 1;  r2i = 2;
  60.   endif
  61.  
  62.   ## remove roots used
  63.   idx = complement([r1i, r2i],1:length(rvals));
  64.   rvals = rvals(idx);
  65.  
  66. endfunction
  67.  
  68.