home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21eb.zip / octave / SCRIPTS.ZIP / scripts.fat / control / zp2ssg2.m < prev    next >
Text File  |  1999-04-29  |  2KB  |  66 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 [poly,rvals] = zp2ssg2(rvals)
  18. # [poly,rvals] = zp2ssg2(rvals)
  19. #
  20. #  used internally in zp2ss
  21. # extract 2 values from rvals (if possible) and construct
  22. # a polynom with those roots.
  23.  
  24. # A. S. Hodel Aug 1996
  25.  
  26. # locate imaginary roots (if any)
  27. cidx = find(imag(rvals));
  28.  
  29. if(!isempty(cidx))
  30.   # select first complex root, omit from cidx
  31.   r1i = cidx(1);      r1 = rvals(r1i);     cidx = complmnt(r1i,cidx);
  32.  
  33.   # locate conjugate root (must be in cidx list, just in case there's
  34.   # roundoff)
  35.   err = abs(rvals(cidx) - r1');
  36.   minerr = min(err);
  37.   c2i = find(err == minerr);
  38.   r2i = cidx(c2i);
  39.   r2 = rvals(r2i);
  40.   cidx = complmnt(r2i,cidx);
  41.  
  42.   # don't check for divide by zero, since 0 is not complex.
  43.   if(abs(r2 - r1')/abs(r1) > 1e-12)
  44.     error(sprintf("r1=(%f,%f); r2=(%f,%f), not conjugates.", ...
  45.       real(r1),imag(r1),real(r2),imag(r2)));
  46.   endif
  47.  
  48.   # complex conjugate pair
  49.   poly = [1, -2*real(r1), real(r1)^2+imag(r1)^2];
  50. else
  51.   # select two roots (they're all real)
  52.   r1 = rvals(1);
  53.   r2 = rvals(2);
  54.   poly = [1, -(r1+r2), (r1*r2)];
  55.   r1i = 1;  r2i = 2;
  56. endif
  57.  
  58. # remove roots used
  59. idx = complmnt([r1i, r2i],1:length(rvals));
  60. rvals = rvals(idx);
  61.  
  62. endfunction
  63.  
  64.