home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21eb.zip / octave / SCRIPTS.ZIP / scripts.fat / polynom / poly.m < prev    next >
Text File  |  1999-04-29  |  2KB  |  62 lines

  1. ## Copyright (C) 1996, 1997 John W. Eaton
  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
  7. ## the Free Software Foundation; either version 2, or (at your option)
  8. ## any later version.
  9. ##
  10. ## Octave is distributed in the hope that it will be useful, but
  11. ## WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13. ## General Public License 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
  18. ## 02111-1307, USA.
  19.  
  20. ## usage: poly (x)
  21. ##
  22. ## If A is a square n-by-n matrix, poly (A) is the row vector of
  23. ## the coefficients of det (z * eye(n) - A), the characteristic
  24. ## polynom of A.
  25. ##
  26. ## If x is a vector, poly (x) is a vector of coefficients of the
  27. ## polynom whose roots are the elements of x.
  28.  
  29. ## Author: KH <Kurt.Hornik@neuro.tuwien.ac.at>
  30. ## Created: 24 December 1993
  31. ## Adapted-By: jwe
  32.  
  33. function y = poly (x)
  34.  
  35.   if (nargin != 1)
  36.     usage ("poly (x)");
  37.   endif
  38.  
  39.   m = min (size (x));
  40.   n = max (size (x));
  41.   if (m == 0)
  42.     y = 1;
  43.   elseif (m == 1)
  44.     v = x;
  45.   elseif (m == n)
  46.     v = eig (x);
  47.   else
  48.     usage ("poly (x), where x is a vector or a square matrix");
  49.   endif
  50.  
  51.   y = zeros (1, n+1);
  52.   y(1) = 1;
  53.   for j = 1:n;
  54.     y(2:(j+1)) = y(2:(j+1)) - v(j) .* y(1:j);
  55.   endfor
  56.  
  57.   if (all (all (imag (x) == 0)))
  58.     y = real (y);
  59.   endif
  60.  
  61. endfunction
  62.