2303
|
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 |
787
|
25 function r = roots (v) |
904
|
26 |
2303
|
27 ## Written by KH (Kurt.Hornik@ci.tuwien.ac.at) on Dec 24, 1993 |
|
28 ## Updated by KH on Nov 25, 1994 |
1598
|
29 |
|
30 if (min (size (v)) > 1 || nargin != 1) |
|
31 usage ("roots (v), where v is a vector"); |
|
32 endif |
|
33 |
|
34 n = length (v); |
|
35 v = reshape (v, 1, n); |
1025
|
36 |
2303
|
37 ## If v = [ 0 ... 0 v(k+1) ... v(k+l) 0 ... 0 ], we can remove the |
|
38 ## leading k zeros and n - k - l roots of the polynomial are zero. |
904
|
39 |
787
|
40 f = find (v); |
|
41 m = max (size (f)); |
1598
|
42 |
|
43 if (m > 0 && n > 1) |
|
44 v = v(f(1):f(m)); |
787
|
45 l = max (size (v)); |
|
46 if (l > 1) |
|
47 A = diag (ones (1, l-2), -1); |
1598
|
48 A(1,:) = -v(2:l) ./ v(1); |
787
|
49 r = eig (A); |
1598
|
50 if (f(m) < n) |
|
51 tmp = zeros (n - f(m), 1); |
|
52 r = [r; tmp]; |
787
|
53 endif |
|
54 else |
|
55 r = zeros (n - f(m), 1); |
|
56 endif |
|
57 else |
1598
|
58 r = []; |
787
|
59 endif |
|
60 |
|
61 endfunction |