Mercurial > hg > octave-lyh
annotate scripts/polynomial/poly.m @ 9245:16f53d29049f
update copyright notices
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Fri, 22 May 2009 10:46:00 -0400 |
parents | 1bf0ce0930be |
children | a8ce6bdecce5 |
rev | line source |
---|---|
9245 | 1 ## Copyright (C) 1994, 1995, 1996, 1997, 1999, 2000, 2005, 2006, 2007, |
2 ## 2008, 2009 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 | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
27 ## @group |
6850 | 28 ## roots(poly(eye(3))) |
29 ## @result{} 1.00000 + 0.00000i | |
30 ## @result{} 1.00000 - 0.00000i | |
31 ## @result{} 1.00000 + 0.00000i | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
32 ## @end group |
6850 | 33 ## @end example |
34 ## In real-life examples you should, however, use the @code{eig} function | |
35 ## for computing eigenvalues. | |
36 ## | |
37 ## If @var{x} is a vector, @code{poly (@var{x})} is a vector of coefficients | |
38 ## of the polynomial whose roots are the elements of @var{x}. That is, | |
39 ## of @var{c} is a polynomial, then the elements of | |
40 ## @code{@var{d} = roots (poly (@var{c}))} are contained in @var{c}. | |
41 ## The vectors @var{c} and @var{d} are, however, not equal due to sorting | |
42 ## and numerical errors. | |
43 ## @seealso{eig, roots} | |
3368 | 44 ## @end deftypefn |
787 | 45 |
5428 | 46 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
2312 | 47 ## Created: 24 December 1993 |
48 ## Adapted-By: jwe | |
904 | 49 |
2312 | 50 function y = poly (x) |
1025 | 51 |
52 if (nargin != 1) | |
6046 | 53 print_usage (); |
1025 | 54 endif |
787 | 55 |
56 m = min (size (x)); | |
57 n = max (size (x)); | |
58 if (m == 0) | |
59 y = 1; | |
5158 | 60 return; |
787 | 61 elseif (m == 1) |
62 v = x; | |
63 elseif (m == n) | |
64 v = eig (x); | |
65 else | |
6046 | 66 print_usage (); |
787 | 67 endif |
2325 | 68 |
1336 | 69 y = zeros (1, n+1); |
70 y(1) = 1; | |
787 | 71 for j = 1:n; |
72 y(2:(j+1)) = y(2:(j+1)) - v(j) .* y(1:j); | |
73 endfor | |
2325 | 74 |
787 | 75 if (all (all (imag (x) == 0))) |
76 y = real (y); | |
77 endif | |
2325 | 78 |
787 | 79 endfunction |
7411 | 80 |
81 %!assert(all (all (poly ([1, 2, 3]) == [1, -6, 11, -6]))); | |
82 | |
83 %!assert(all (all (abs (poly ([1, 2; 3, 4]) - [1, -5, -2]) < sqrt (eps)))); | |
84 | |
85 %!error poly ([1, 2, 3; 4, 5, 6]); | |
86 | |
87 %!assert(poly ([]),1); | |
88 |