Mercurial > hg > octave-lyh
annotate scripts/polynomial/roots.m @ 8920:eb63fbe60fab
update copyright notices
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Sat, 07 Mar 2009 10:41:27 -0500 |
parents | e07e93c04080 |
children | 1bf0ce0930be |
rev | line source |
---|---|
7017 | 1 ## Copyright (C) 1994, 1995, 1996, 1997, 1999, 2000, 2004, 2005, 2006, |
8920 | 2 ## 2007, 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} {} roots (@var{v}) | |
3426 | 22 ## |
3499 | 23 ## For a vector @var{v} with @math{N} components, return |
3368 | 24 ## the roots of the polynomial |
25 ## @iftex | |
26 ## @tex | |
27 ## $$ | |
28 ## v_1 z^{N-1} + \cdots + v_{N-1} z + v_N. | |
29 ## $$ | |
30 ## @end tex | |
31 ## @end iftex | |
6850 | 32 ## @ifnottex |
3426 | 33 ## |
3368 | 34 ## @example |
5016 | 35 ## v(1) * z^(N-1) + ... + v(N-1) * z + v(N) |
3368 | 36 ## @end example |
6850 | 37 ## @end ifnottex |
38 ## | |
39 ## As an example, the following code finds the roots of the quadratic | |
40 ## polynomial | |
41 ## @iftex | |
42 ## @tex | |
43 ## $$ p(x) = x^2 - 5. $$ | |
44 ## @end tex | |
45 ## @end iftex | |
46 ## @ifnottex | |
47 ## @example | |
48 ## p(x) = x^2 - 5. | |
49 ## @end example | |
50 ## @end ifnottex | |
51 ## @example | |
52 ## c = [1, 0, -5]; | |
53 ## roots(c) | |
54 ## @result{} 2.2361 | |
55 ## @result{} -2.2361 | |
56 ## @end example | |
57 ## Note that the true result is | |
58 ## @iftex | |
59 ## @tex | |
60 ## $\pm \sqrt{5}$ | |
61 ## @end tex | |
62 ## @end iftex | |
63 ## @ifnottex | |
64 ## @math{+/- sqrt(5)} | |
65 ## @end ifnottex | |
66 ## which is roughly | |
67 ## @iftex | |
68 ## @tex | |
69 ## $\pm 2.2361$. | |
70 ## @end tex | |
71 ## @end iftex | |
72 ## @ifnottex | |
73 ## @math{+/- 2.2361}. | |
74 ## @end ifnottex | |
75 ## @seealso{compan} | |
3368 | 76 ## @end deftypefn |
2311 | 77 |
5428 | 78 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
2312 | 79 ## Created: 24 December 1993 |
80 ## Adapted-By: jwe | |
81 | |
787 | 82 function r = roots (v) |
904 | 83 |
6372 | 84 if (nargin != 1 || min (size (v)) > 1) |
6046 | 85 print_usage (); |
7558
690c91f741b8
Apply a scaling factor to leading zero removal in roots.m
Sebastien Loisel
parents:
7411
diff
changeset
|
86 elseif (any (isnan(v) | isinf(v))) |
8664 | 87 error ("roots: inputs must not contain Inf or NaN"); |
1598 | 88 endif |
2325 | 89 |
7558
690c91f741b8
Apply a scaling factor to leading zero removal in roots.m
Sebastien Loisel
parents:
7411
diff
changeset
|
90 n = numel (v); |
690c91f741b8
Apply a scaling factor to leading zero removal in roots.m
Sebastien Loisel
parents:
7411
diff
changeset
|
91 v = v(:); |
1025 | 92 |
2303 | 93 ## If v = [ 0 ... 0 v(k+1) ... v(k+l) 0 ... 0 ], we can remove the |
2325 | 94 ## leading k zeros and n - k - l roots of the polynomial are zero. |
904 | 95 |
7558
690c91f741b8
Apply a scaling factor to leading zero removal in roots.m
Sebastien Loisel
parents:
7411
diff
changeset
|
96 if (isempty (v)) |
690c91f741b8
Apply a scaling factor to leading zero removal in roots.m
Sebastien Loisel
parents:
7411
diff
changeset
|
97 f = v; |
690c91f741b8
Apply a scaling factor to leading zero removal in roots.m
Sebastien Loisel
parents:
7411
diff
changeset
|
98 else |
690c91f741b8
Apply a scaling factor to leading zero removal in roots.m
Sebastien Loisel
parents:
7411
diff
changeset
|
99 f = find (v ./ max (abs (v))); |
690c91f741b8
Apply a scaling factor to leading zero removal in roots.m
Sebastien Loisel
parents:
7411
diff
changeset
|
100 endif |
690c91f741b8
Apply a scaling factor to leading zero removal in roots.m
Sebastien Loisel
parents:
7411
diff
changeset
|
101 m = numel (f); |
2325 | 102 |
1598 | 103 if (m > 0 && n > 1) |
104 v = v(f(1):f(m)); | |
787 | 105 l = max (size (v)); |
106 if (l > 1) | |
107 A = diag (ones (1, l-2), -1); | |
1598 | 108 A(1,:) = -v(2:l) ./ v(1); |
2325 | 109 r = eig (A); |
1598 | 110 if (f(m) < n) |
111 tmp = zeros (n - f(m), 1); | |
3426 | 112 r = [r; tmp]; |
787 | 113 endif |
114 else | |
115 r = zeros (n - f(m), 1); | |
116 endif | |
117 else | |
1598 | 118 r = []; |
787 | 119 endif |
2325 | 120 |
787 | 121 endfunction |
7411 | 122 |
7558
690c91f741b8
Apply a scaling factor to leading zero removal in roots.m
Sebastien Loisel
parents:
7411
diff
changeset
|
123 %!test |
690c91f741b8
Apply a scaling factor to leading zero removal in roots.m
Sebastien Loisel
parents:
7411
diff
changeset
|
124 %! p = [poly([3 3 3 3]), 0 0 0 0]; |
690c91f741b8
Apply a scaling factor to leading zero removal in roots.m
Sebastien Loisel
parents:
7411
diff
changeset
|
125 %! r = sort (roots (p)); |
690c91f741b8
Apply a scaling factor to leading zero removal in roots.m
Sebastien Loisel
parents:
7411
diff
changeset
|
126 %! assert (r, [0; 0; 0; 0; 3; 3; 3; 3], 0.001) |
690c91f741b8
Apply a scaling factor to leading zero removal in roots.m
Sebastien Loisel
parents:
7411
diff
changeset
|
127 |
7411 | 128 %!assert(all (all (abs (roots ([1, -6, 11, -6]) - [3; 2; 1]) < sqrt (eps)))); |
129 | |
130 %!assert(isempty (roots ([]))); | |
131 | |
132 %!error roots ([1, 2; 3, 4]); | |
7558
690c91f741b8
Apply a scaling factor to leading zero removal in roots.m
Sebastien Loisel
parents:
7411
diff
changeset
|
133 |
690c91f741b8
Apply a scaling factor to leading zero removal in roots.m
Sebastien Loisel
parents:
7411
diff
changeset
|
134 %!assert(isempty (roots (1))); |
7411 | 135 |
7558
690c91f741b8
Apply a scaling factor to leading zero removal in roots.m
Sebastien Loisel
parents:
7411
diff
changeset
|
136 %!error roots ([1, 2; 3, 4]); |
690c91f741b8
Apply a scaling factor to leading zero removal in roots.m
Sebastien Loisel
parents:
7411
diff
changeset
|
137 |
690c91f741b8
Apply a scaling factor to leading zero removal in roots.m
Sebastien Loisel
parents:
7411
diff
changeset
|
138 %!error roots ([1 Inf 1]); |
690c91f741b8
Apply a scaling factor to leading zero removal in roots.m
Sebastien Loisel
parents:
7411
diff
changeset
|
139 |
690c91f741b8
Apply a scaling factor to leading zero removal in roots.m
Sebastien Loisel
parents:
7411
diff
changeset
|
140 %!error roots ([1 NaN 1]); |
690c91f741b8
Apply a scaling factor to leading zero removal in roots.m
Sebastien Loisel
parents:
7411
diff
changeset
|
141 |
690c91f741b8
Apply a scaling factor to leading zero removal in roots.m
Sebastien Loisel
parents:
7411
diff
changeset
|
142 %!assert(roots ([1e-200, -1e200, 1]), 1e-200) |
690c91f741b8
Apply a scaling factor to leading zero removal in roots.m
Sebastien Loisel
parents:
7411
diff
changeset
|
143 %!assert(roots ([1e-200, -1e200 * 1i, 1]), -1e-200 * 1i) |