Mercurial > hg > octave-lyh
annotate scripts/polynomial/poly.m @ 10687:a8ce6bdecce5
Improve documentation strings.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Tue, 08 Jun 2010 20:22:38 -0700 |
parents | 16f53d29049f |
children | 693e22af08ae |
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 -*- |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
21 ## @deftypefn {Function File} {} poly (@var{a}) |
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
22 ## @deftypefnx {Function File} {} poly (@var{x}) |
3499 | 23 ## If @var{a} is a square @math{N}-by-@math{N} matrix, @code{poly (@var{a})} |
3368 | 24 ## is the row vector of the coefficients of @code{det (z * eye (N) - a)}, |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
25 ## the characteristic polynomial of @var{a}. For example, |
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
26 ## the following code finds the eigenvalues of @var{a} which are the roots of |
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
27 ## @code{poly (@var{a})}. |
6850 | 28 ## @example |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
29 ## @group |
6850 | 30 ## roots(poly(eye(3))) |
31 ## @result{} 1.00000 + 0.00000i | |
32 ## @result{} 1.00000 - 0.00000i | |
33 ## @result{} 1.00000 + 0.00000i | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
34 ## @end group |
6850 | 35 ## @end example |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
36 ## For numerical performance, however, the @code{eig} function |
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
37 ## should be used for computing eigenvalues. |
6850 | 38 ## |
39 ## If @var{x} is a vector, @code{poly (@var{x})} is a vector of coefficients | |
40 ## of the polynomial whose roots are the elements of @var{x}. That is, | |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
41 ## if @var{c} is a polynomial, then the elements of |
6850 | 42 ## @code{@var{d} = roots (poly (@var{c}))} are contained in @var{c}. |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
43 ## The vectors @var{c} and @var{d} are not identical, however, due to sorting |
6850 | 44 ## and numerical errors. |
45 ## @seealso{eig, roots} | |
3368 | 46 ## @end deftypefn |
787 | 47 |
5428 | 48 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
2312 | 49 ## Created: 24 December 1993 |
50 ## Adapted-By: jwe | |
904 | 51 |
2312 | 52 function y = poly (x) |
1025 | 53 |
54 if (nargin != 1) | |
6046 | 55 print_usage (); |
1025 | 56 endif |
787 | 57 |
58 m = min (size (x)); | |
59 n = max (size (x)); | |
60 if (m == 0) | |
61 y = 1; | |
5158 | 62 return; |
787 | 63 elseif (m == 1) |
64 v = x; | |
65 elseif (m == n) | |
66 v = eig (x); | |
67 else | |
6046 | 68 print_usage (); |
787 | 69 endif |
2325 | 70 |
1336 | 71 y = zeros (1, n+1); |
72 y(1) = 1; | |
787 | 73 for j = 1:n; |
74 y(2:(j+1)) = y(2:(j+1)) - v(j) .* y(1:j); | |
75 endfor | |
2325 | 76 |
787 | 77 if (all (all (imag (x) == 0))) |
78 y = real (y); | |
79 endif | |
2325 | 80 |
787 | 81 endfunction |
7411 | 82 |
83 %!assert(all (all (poly ([1, 2, 3]) == [1, -6, 11, -6]))); | |
84 | |
85 %!assert(all (all (abs (poly ([1, 2; 3, 4]) - [1, -5, -2]) < sqrt (eps)))); | |
86 | |
87 %!error poly ([1, 2, 3; 4, 5, 6]); | |
88 | |
89 %!assert(poly ([]),1); | |
90 |