home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l460 / 2.ddi / POLYFUN.DI$ / ROOTS.M < prev    next >
Encoding:
Text File  |  1993-03-07  |  909 b   |  37 lines

  1. function r = roots(c)
  2. %ROOTS    Find polynomial roots.
  3. %    ROOTS(C) computes the roots of the polynomial whose coefficients
  4. %    are the elements of the vector C. If C has N+1 components,
  5. %    the polynomial is C(1)*X^N + ... + C(N)*X + C(N+1).
  6. %
  7. %    See also POLY.
  8.  
  9. %    J.N. Little 3-17-86
  10. %    Copyright (c) 1984-93 by The MathWorks, Inc.
  11.  
  12. % ROOTS finds the eigenvalues of the associated companion matrix.
  13.  
  14. n = size(c);
  15. if ~sum(n <= 1)
  16.     error('Must be a vector.')
  17. end
  18. n = max(n);
  19. c = c(:).';    % Make sure it's a row vector
  20.  
  21. % Strip leading zeros and throw away.  Strip trailing zeros,
  22. % but remember them as roots at zero.
  23. inz = find(abs(c));
  24. nnz = max(size(inz));
  25. if nnz ~= 0
  26.     c = c(inz(1):inz(nnz));
  27.     r = zeros(n-inz(nnz),1);
  28. end
  29.  
  30. % Polynomial roots via a companion matrix
  31. n = max(size(c));
  32. a = diag(ones(1,n-2),-1);
  33. if n > 1
  34.     a(1,:) = -c(2:n) ./ c(1);
  35. end
  36. r = [r;eig(a)];
  37.