Mercurial > hg > octave-lyh
annotate scripts/polynomial/poly.m @ 17176:19191d0ef0bf
build: Remove second re-definition of octetc_DATA from Makefile.am.
* Makefile.am: Remove second re-definition of octetc_DATA from Makefile.am.
author | Rik <rik@octave.org> |
---|---|
date | Sun, 04 Aug 2013 07:56:41 -0700 |
parents | f3d52523cde1 |
children |
rev | line source |
---|---|
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
14104
diff
changeset
|
1 ## Copyright (C) 1994-2012 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)}, |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
24 ## the characteristic polynomial of @var{A}. For example, |
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
25 ## the following code finds the eigenvalues of @var{A} which are the roots of |
11471
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 |
14327
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
30 ## roots (poly (eye (3))) |
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
31 ## @result{} 1.00001 + 0.00001i |
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
32 ## 1.00001 - 0.00001i |
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
33 ## 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 ## | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
37 ## In fact, all three eigenvalues are exactly 1 which emphasizes that for |
10821
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 ## |
14104
614505385171
doc: Overhaul docstrings for polynomial functions.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
41 ## If @var{x} is a vector, @code{poly (@var{x})} is a vector of the |
614505385171
doc: Overhaul docstrings for polynomial functions.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
42 ## coefficients of the polynomial whose roots are the elements of @var{x}. |
614505385171
doc: Overhaul docstrings for polynomial functions.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
43 ## That is, if @var{c} is a polynomial, then the elements of @code{@var{d} = |
614505385171
doc: Overhaul docstrings for polynomial functions.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
44 ## roots (poly (@var{c}))} are contained in @var{c}. The vectors @var{c} and |
614505385171
doc: Overhaul docstrings for polynomial functions.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
45 ## @var{d} are not identical, however, due to sorting and numerical errors. |
614505385171
doc: Overhaul docstrings for polynomial functions.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
46 ## @seealso{roots, eig} |
3368 | 47 ## @end deftypefn |
787 | 48 |
5428 | 49 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
2312 | 50 ## Created: 24 December 1993 |
51 ## Adapted-By: jwe | |
904 | 52 |
2312 | 53 function y = poly (x) |
1025 | 54 |
55 if (nargin != 1) | |
6046 | 56 print_usage (); |
1025 | 57 endif |
787 | 58 |
59 m = min (size (x)); | |
60 n = max (size (x)); | |
61 if (m == 0) | |
62 y = 1; | |
5158 | 63 return; |
787 | 64 elseif (m == 1) |
65 v = x; | |
66 elseif (m == n) | |
67 v = eig (x); | |
68 else | |
6046 | 69 print_usage (); |
787 | 70 endif |
2325 | 71 |
1336 | 72 y = zeros (1, n+1); |
73 y(1) = 1; | |
787 | 74 for j = 1:n; |
75 y(2:(j+1)) = y(2:(j+1)) - v(j) .* y(1:j); | |
76 endfor | |
2325 | 77 |
787 | 78 if (all (all (imag (x) == 0))) |
79 y = real (y); | |
80 endif | |
2325 | 81 |
787 | 82 endfunction |
7411 | 83 |
84 | |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
85 %!assert (poly ([]), 1) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
86 %!assert (poly ([1, 2, 3]), [1, -6, 11, -6]) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
87 %!assert (poly ([1, 2; 3, 4]), [1, -5, -2], sqrt (eps)) |
7411 | 88 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
89 %!error poly ([1, 2, 3; 4, 5, 6]) |
7411 | 90 |