1025
|
1 # Copyright (C) 1995 John W. Eaton |
|
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 the |
|
7 # Free Software Foundation; either version 2, or (at your option) any |
|
8 # later version. |
|
9 # |
|
10 # Octave is distributed in the hope that it will be useful, but WITHOUT |
|
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
12 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
13 # 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 |
1315
|
17 # Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
1025
|
18 |
787
|
19 function r = roots (v) |
904
|
20 |
1025
|
21 # usage: roots (v) |
|
22 # |
904
|
23 # For a vector v with n components, return the roots of the |
|
24 # polynomial v(1) * z^(n-1) + ... + v(n-1) * z + v(n). |
787
|
25 |
1598
|
26 # Written by KH (Kurt.Hornik@ci.tuwien.ac.at) on Dec 24, 1993 |
|
27 # Updated by KH on Nov 25, 1994 |
|
28 |
|
29 if (min (size (v)) > 1 || nargin != 1) |
|
30 usage ("roots (v), where v is a vector"); |
|
31 endif |
|
32 |
|
33 n = length (v); |
|
34 v = reshape (v, 1, n); |
1025
|
35 |
1598
|
36 # If v = [ 0 ... 0 v(k+1) ... v(k+l) 0 ... 0 ], we can remove the |
|
37 # leading k zeros and n - k - l roots of the polynomial are zero. |
904
|
38 |
787
|
39 f = find (v); |
|
40 m = max (size (f)); |
1598
|
41 |
|
42 if (m > 0 && n > 1) |
|
43 v = v(f(1):f(m)); |
787
|
44 l = max (size (v)); |
|
45 if (l > 1) |
|
46 A = diag (ones (1, l-2), -1); |
1598
|
47 A(1,:) = -v(2:l) ./ v(1); |
787
|
48 r = eig (A); |
1598
|
49 if (f(m) < n) |
|
50 tmp = zeros (n - f(m), 1); |
|
51 r = [r; tmp]; |
787
|
52 endif |
|
53 else |
|
54 r = zeros (n - f(m), 1); |
|
55 endif |
|
56 else |
1598
|
57 r = []; |
787
|
58 endif |
|
59 |
|
60 endfunction |