2313
|
1 ## Copyright (C) 1996 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 |
|
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 |
|
17 ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA |
|
18 ## 02111-1307, USA. |
1025
|
19 |
2311
|
20 ## usage: roots (v) |
|
21 ## |
|
22 ## For a vector v with n components, return the roots of the |
|
23 ## polynomial v(1) * z^(n-1) + ... + v(n-1) * z + v(n). |
|
24 |
2312
|
25 ## Author: KH <Kurt.Hornik@ci.tuwien.ac.at> |
|
26 ## Created: 24 December 1993 |
|
27 ## Adapted-By: jwe |
|
28 |
787
|
29 function r = roots (v) |
904
|
30 |
1598
|
31 if (min (size (v)) > 1 || nargin != 1) |
|
32 usage ("roots (v), where v is a vector"); |
|
33 endif |
|
34 |
|
35 n = length (v); |
|
36 v = reshape (v, 1, n); |
1025
|
37 |
2303
|
38 ## If v = [ 0 ... 0 v(k+1) ... v(k+l) 0 ... 0 ], we can remove the |
|
39 ## leading k zeros and n - k - l roots of the polynomial are zero. |
904
|
40 |
787
|
41 f = find (v); |
|
42 m = max (size (f)); |
1598
|
43 |
|
44 if (m > 0 && n > 1) |
|
45 v = v(f(1):f(m)); |
787
|
46 l = max (size (v)); |
|
47 if (l > 1) |
|
48 A = diag (ones (1, l-2), -1); |
1598
|
49 A(1,:) = -v(2:l) ./ v(1); |
787
|
50 r = eig (A); |
1598
|
51 if (f(m) < n) |
|
52 tmp = zeros (n - f(m), 1); |
|
53 r = [r; tmp]; |
787
|
54 endif |
|
55 else |
|
56 r = zeros (n - f(m), 1); |
|
57 endif |
|
58 else |
1598
|
59 r = []; |
787
|
60 endif |
|
61 |
|
62 endfunction |