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 |
|
32 ## @ifinfo |
3426
|
33 ## |
3368
|
34 ## @example |
5016
|
35 ## v(1) * z^(N-1) + ... + v(N-1) * z + v(N) |
3368
|
36 ## @end example |
|
37 ## @end ifinfo |
|
38 ## @end deftypefn |
2311
|
39 |
2312
|
40 ## Author: KH <Kurt.Hornik@ci.tuwien.ac.at> |
|
41 ## Created: 24 December 1993 |
|
42 ## Adapted-By: jwe |
|
43 |
787
|
44 function r = roots (v) |
904
|
45 |
1598
|
46 if (min (size (v)) > 1 || nargin != 1) |
|
47 usage ("roots (v), where v is a vector"); |
|
48 endif |
2325
|
49 |
1598
|
50 n = length (v); |
2325
|
51 v = reshape (v, 1, n); |
1025
|
52 |
2303
|
53 ## If v = [ 0 ... 0 v(k+1) ... v(k+l) 0 ... 0 ], we can remove the |
2325
|
54 ## leading k zeros and n - k - l roots of the polynomial are zero. |
904
|
55 |
787
|
56 f = find (v); |
|
57 m = max (size (f)); |
2325
|
58 |
1598
|
59 if (m > 0 && n > 1) |
|
60 v = v(f(1):f(m)); |
787
|
61 l = max (size (v)); |
|
62 if (l > 1) |
|
63 A = diag (ones (1, l-2), -1); |
1598
|
64 A(1,:) = -v(2:l) ./ v(1); |
2325
|
65 r = eig (A); |
1598
|
66 if (f(m) < n) |
|
67 tmp = zeros (n - f(m), 1); |
3426
|
68 r = [r; tmp]; |
787
|
69 endif |
|
70 else |
|
71 r = zeros (n - f(m), 1); |
|
72 endif |
|
73 else |
1598
|
74 r = []; |
787
|
75 endif |
2325
|
76 |
787
|
77 endfunction |