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 |
|
7 ## the Free Software Foundation; either version 2, or (at your option) |
|
8 ## any later version. |
|
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 |
|
16 ## along with Octave; see the file COPYING. If not, write to the Free |
5307
|
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301, USA. |
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 (); |
1598
|
86 endif |
2325
|
87 |
1598
|
88 n = length (v); |
2325
|
89 v = reshape (v, 1, n); |
1025
|
90 |
2303
|
91 ## If v = [ 0 ... 0 v(k+1) ... v(k+l) 0 ... 0 ], we can remove the |
2325
|
92 ## leading k zeros and n - k - l roots of the polynomial are zero. |
904
|
93 |
787
|
94 f = find (v); |
|
95 m = max (size (f)); |
2325
|
96 |
1598
|
97 if (m > 0 && n > 1) |
|
98 v = v(f(1):f(m)); |
787
|
99 l = max (size (v)); |
|
100 if (l > 1) |
|
101 A = diag (ones (1, l-2), -1); |
1598
|
102 A(1,:) = -v(2:l) ./ v(1); |
2325
|
103 r = eig (A); |
1598
|
104 if (f(m) < n) |
|
105 tmp = zeros (n - f(m), 1); |
3426
|
106 r = [r; tmp]; |
787
|
107 endif |
|
108 else |
|
109 r = zeros (n - f(m), 1); |
|
110 endif |
|
111 else |
1598
|
112 r = []; |
787
|
113 endif |
2325
|
114 |
787
|
115 endfunction |