2847
|
1 ## Copyright (C) 1996, 1997 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 -*- |
|
20 ## @deftypefn {Function File} {} roots (@var{v}) |
3426
|
21 ## |
3499
|
22 ## For a vector @var{v} with @math{N} components, return |
3368
|
23 ## the roots of the polynomial |
|
24 ## @iftex |
|
25 ## @tex |
|
26 ## $$ |
|
27 ## v_1 z^{N-1} + \cdots + v_{N-1} z + v_N. |
|
28 ## $$ |
|
29 ## @end tex |
|
30 ## @end iftex |
6850
|
31 ## @ifnottex |
3426
|
32 ## |
3368
|
33 ## @example |
5016
|
34 ## v(1) * z^(N-1) + ... + v(N-1) * z + v(N) |
3368
|
35 ## @end example |
6850
|
36 ## @end ifnottex |
|
37 ## |
|
38 ## As an example, the following code finds the roots of the quadratic |
|
39 ## polynomial |
|
40 ## @iftex |
|
41 ## @tex |
|
42 ## $$ p(x) = x^2 - 5. $$ |
|
43 ## @end tex |
|
44 ## @end iftex |
|
45 ## @ifnottex |
|
46 ## @example |
|
47 ## p(x) = x^2 - 5. |
|
48 ## @end example |
|
49 ## @end ifnottex |
|
50 ## @example |
|
51 ## c = [1, 0, -5]; |
|
52 ## roots(c) |
|
53 ## @result{} 2.2361 |
|
54 ## @result{} -2.2361 |
|
55 ## @end example |
|
56 ## Note that the true result is |
|
57 ## @iftex |
|
58 ## @tex |
|
59 ## $\pm \sqrt{5}$ |
|
60 ## @end tex |
|
61 ## @end iftex |
|
62 ## @ifnottex |
|
63 ## @math{+/- sqrt(5)} |
|
64 ## @end ifnottex |
|
65 ## which is roughly |
|
66 ## @iftex |
|
67 ## @tex |
|
68 ## $\pm 2.2361$. |
|
69 ## @end tex |
|
70 ## @end iftex |
|
71 ## @ifnottex |
|
72 ## @math{+/- 2.2361}. |
|
73 ## @end ifnottex |
|
74 ## @seealso{compan} |
3368
|
75 ## @end deftypefn |
2311
|
76 |
5428
|
77 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
2312
|
78 ## Created: 24 December 1993 |
|
79 ## Adapted-By: jwe |
|
80 |
787
|
81 function r = roots (v) |
904
|
82 |
6372
|
83 if (nargin != 1 || min (size (v)) > 1) |
6046
|
84 print_usage (); |
1598
|
85 endif |
2325
|
86 |
1598
|
87 n = length (v); |
2325
|
88 v = reshape (v, 1, n); |
1025
|
89 |
2303
|
90 ## If v = [ 0 ... 0 v(k+1) ... v(k+l) 0 ... 0 ], we can remove the |
2325
|
91 ## leading k zeros and n - k - l roots of the polynomial are zero. |
904
|
92 |
787
|
93 f = find (v); |
|
94 m = max (size (f)); |
2325
|
95 |
1598
|
96 if (m > 0 && n > 1) |
|
97 v = v(f(1):f(m)); |
787
|
98 l = max (size (v)); |
|
99 if (l > 1) |
|
100 A = diag (ones (1, l-2), -1); |
1598
|
101 A(1,:) = -v(2:l) ./ v(1); |
2325
|
102 r = eig (A); |
1598
|
103 if (f(m) < n) |
|
104 tmp = zeros (n - f(m), 1); |
3426
|
105 r = [r; tmp]; |
787
|
106 endif |
|
107 else |
|
108 r = zeros (n - f(m), 1); |
|
109 endif |
|
110 else |
1598
|
111 r = []; |
787
|
112 endif |
2325
|
113 |
787
|
114 endfunction |