7017
|
1 ## Copyright (C) 1994, 1995, 1996, 1997, 1999, 2000, 2005, 2006, 2007 |
|
2 ## John W. Eaton |
2313
|
3 ## |
|
4 ## This file is part of Octave. |
|
5 ## |
|
6 ## Octave is free software; you can redistribute it and/or modify it |
|
7 ## under the terms of the GNU General Public License as published by |
7016
|
8 ## the Free Software Foundation; either version 3 of the License, or (at |
|
9 ## your option) any later version. |
2313
|
10 ## |
|
11 ## Octave is distributed in the hope that it will be useful, but |
|
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
14 ## General Public License for more details. |
|
15 ## |
|
16 ## You should have received a copy of the GNU General Public License |
7016
|
17 ## along with Octave; see the file COPYING. If not, see |
|
18 ## <http://www.gnu.org/licenses/>. |
1025
|
19 |
3368
|
20 ## -*- texinfo -*- |
|
21 ## @deftypefn {Function File} {} poly (@var{a}) |
3499
|
22 ## If @var{a} is a square @math{N}-by-@math{N} matrix, @code{poly (@var{a})} |
3368
|
23 ## is the row vector of the coefficients of @code{det (z * eye (N) - a)}, |
6850
|
24 ## the characteristic polynomial of @var{a}. As an example we can use |
|
25 ## this to find the eigenvalues of @var{a} as the roots of @code{poly (@var{a})}. |
|
26 ## @example |
|
27 ## roots(poly(eye(3))) |
|
28 ## @result{} 1.00000 + 0.00000i |
|
29 ## @result{} 1.00000 - 0.00000i |
|
30 ## @result{} 1.00000 + 0.00000i |
|
31 ## @end example |
|
32 ## In real-life examples you should, however, use the @code{eig} function |
|
33 ## for computing eigenvalues. |
|
34 ## |
|
35 ## If @var{x} is a vector, @code{poly (@var{x})} is a vector of coefficients |
|
36 ## of the polynomial whose roots are the elements of @var{x}. That is, |
|
37 ## of @var{c} is a polynomial, then the elements of |
|
38 ## @code{@var{d} = roots (poly (@var{c}))} are contained in @var{c}. |
|
39 ## The vectors @var{c} and @var{d} are, however, not equal due to sorting |
|
40 ## and numerical errors. |
|
41 ## @seealso{eig, roots} |
3368
|
42 ## @end deftypefn |
787
|
43 |
5428
|
44 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
2312
|
45 ## Created: 24 December 1993 |
|
46 ## Adapted-By: jwe |
904
|
47 |
2312
|
48 function y = poly (x) |
1025
|
49 |
|
50 if (nargin != 1) |
6046
|
51 print_usage (); |
1025
|
52 endif |
787
|
53 |
|
54 m = min (size (x)); |
|
55 n = max (size (x)); |
|
56 if (m == 0) |
|
57 y = 1; |
5158
|
58 return; |
787
|
59 elseif (m == 1) |
|
60 v = x; |
|
61 elseif (m == n) |
|
62 v = eig (x); |
|
63 else |
6046
|
64 print_usage (); |
787
|
65 endif |
2325
|
66 |
1336
|
67 y = zeros (1, n+1); |
|
68 y(1) = 1; |
787
|
69 for j = 1:n; |
|
70 y(2:(j+1)) = y(2:(j+1)) - v(j) .* y(1:j); |
|
71 endfor |
2325
|
72 |
787
|
73 if (all (all (imag (x) == 0))) |
|
74 y = real (y); |
|
75 endif |
2325
|
76 |
787
|
77 endfunction |