Mercurial > hg > octave-lyh
annotate scripts/polynomial/poly.m @ 11523:fd0a3ac60b0e
update copyright notices
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Fri, 14 Jan 2011 05:47:45 -0500 |
parents | 994e2a93a8e2 |
children | c792872f8942 |
rev | line source |
---|---|
11523 | 1 ## Copyright (C) 1994-2011 John W. Eaton |
2313 | 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 | |
7016 | 7 ## the Free Software Foundation; either version 3 of the License, or (at |
8 ## your option) any later version. | |
2313 | 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 | |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
1025 | 18 |
3368 | 19 ## -*- texinfo -*- |
11471
994e2a93a8e2
Use uppercase 'A' to refer to matrix inputs in m-files.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
20 ## @deftypefn {Function File} {} poly (@var{A}) |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
21 ## @deftypefnx {Function File} {} poly (@var{x}) |
11471
994e2a93a8e2
Use uppercase 'A' to refer to matrix inputs in m-files.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
22 ## If @var{A} is a square @math{N}-by-@math{N} matrix, @code{poly (@var{A})} |
994e2a93a8e2
Use uppercase 'A' to refer to matrix inputs in m-files.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
23 ## is the row vector of the coefficients of @code{det (z * eye (N) - A)}, |
994e2a93a8e2
Use uppercase 'A' to refer to matrix inputs in m-files.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
24 ## the characteristic polynomial of @var{A}. For example, |
994e2a93a8e2
Use uppercase 'A' to refer to matrix inputs in m-files.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
25 ## the following code finds the eigenvalues of @var{A} which are the roots of |
994e2a93a8e2
Use uppercase 'A' to refer to matrix inputs in m-files.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
26 ## @code{poly (@var{A})}. |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10687
diff
changeset
|
27 ## |
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))) |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10687
diff
changeset
|
31 ## @result{} 1.00001 + 0.00001i |
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10687
diff
changeset
|
32 ## @result{} 1.00001 - 0.00001i |
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10687
diff
changeset
|
33 ## @result{} 0.99999 + 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 |
36 ## | |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10687
diff
changeset
|
37 ## In fact, all three eigenvalues are exactly 1 which emphasizes that for |
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10687
diff
changeset
|
38 ## numerical performance the @code{eig} function should be used to compute |
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10687
diff
changeset
|
39 ## eigenvalues. |
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10687
diff
changeset
|
40 ## |
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10687
diff
changeset
|
41 ## If @var{x} is a vector, @code{poly (@var{x})} is a vector of the coefficients |
6850 | 42 ## 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
|
43 ## if @var{c} is a polynomial, then the elements of |
6850 | 44 ## @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
|
45 ## The vectors @var{c} and @var{d} are not identical, however, due to sorting |
6850 | 46 ## and numerical errors. |
47 ## @seealso{eig, roots} | |
3368 | 48 ## @end deftypefn |
787 | 49 |
5428 | 50 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
2312 | 51 ## Created: 24 December 1993 |
52 ## Adapted-By: jwe | |
904 | 53 |
2312 | 54 function y = poly (x) |
1025 | 55 |
56 if (nargin != 1) | |
6046 | 57 print_usage (); |
1025 | 58 endif |
787 | 59 |
60 m = min (size (x)); | |
61 n = max (size (x)); | |
62 if (m == 0) | |
63 y = 1; | |
5158 | 64 return; |
787 | 65 elseif (m == 1) |
66 v = x; | |
67 elseif (m == n) | |
68 v = eig (x); | |
69 else | |
6046 | 70 print_usage (); |
787 | 71 endif |
2325 | 72 |
1336 | 73 y = zeros (1, n+1); |
74 y(1) = 1; | |
787 | 75 for j = 1:n; |
76 y(2:(j+1)) = y(2:(j+1)) - v(j) .* y(1:j); | |
77 endfor | |
2325 | 78 |
787 | 79 if (all (all (imag (x) == 0))) |
80 y = real (y); | |
81 endif | |
2325 | 82 |
787 | 83 endfunction |
7411 | 84 |
85 %!assert(all (all (poly ([1, 2, 3]) == [1, -6, 11, -6]))); | |
86 | |
87 %!assert(all (all (abs (poly ([1, 2; 3, 4]) - [1, -5, -2]) < sqrt (eps)))); | |
88 | |
89 %!error poly ([1, 2, 3; 4, 5, 6]); | |
90 | |
91 %!assert(poly ([]),1); | |
92 |