annotate scripts/polynomial/roots.m @ 10821:693e22af08ae

Grammarcheck documentation of m-files Add newlines between @item fields for readability.
author Rik <octave@nomad.inbox5.com>
date Mon, 26 Jul 2010 21:25:36 -0700
parents f0c3d3fc4903
children fd0a3ac60b0e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7017
a1dbe9d80eee [project @ 2007-10-12 21:27:11 by jwe]
jwe
parents: 7016
diff changeset
1 ## Copyright (C) 1994, 1995, 1996, 1997, 1999, 2000, 2004, 2005, 2006,
8920
eb63fbe60fab update copyright notices
John W. Eaton <jwe@octave.org>
parents: 8664
diff changeset
2 ## 2007, 2008, 2009 John W. Eaton
2313
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
3 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
4 ## This file is part of Octave.
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
5 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
6 ## Octave is free software; you can redistribute it and/or modify it
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
7 ## under the terms of the GNU General Public License as published by
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6850
diff changeset
8 ## the Free Software Foundation; either version 3 of the License, or (at
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6850
diff changeset
9 ## your option) any later version.
2313
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
10 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
11 ## Octave is distributed in the hope that it will be useful, but
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
14 ## General Public License for more details.
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
15 ##
5ca126254d15 [project @ 1996-07-11 21:25:22 by jwe]
jwe
parents: 2312
diff changeset
16 ## You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6850
diff changeset
17 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6850
diff changeset
18 ## <http://www.gnu.org/licenses/>.
1025
f558749713f1 [project @ 1995-01-11 20:52:10 by jwe]
jwe
parents: 944
diff changeset
19
3368
a4cd1e9d9962 [project @ 1999-11-20 17:22:48 by jwe]
jwe
parents: 2847
diff changeset
20 ## -*- texinfo -*-
a4cd1e9d9962 [project @ 1999-11-20 17:22:48 by jwe]
jwe
parents: 2847
diff changeset
21 ## @deftypefn {Function File} {} roots (@var{v})
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3368
diff changeset
22 ##
3499
3e3e14ad5149 [project @ 2000-01-31 05:18:07 by jwe]
jwe
parents: 3426
diff changeset
23 ## For a vector @var{v} with @math{N} components, return
3368
a4cd1e9d9962 [project @ 1999-11-20 17:22:48 by jwe]
jwe
parents: 2847
diff changeset
24 ## the roots of the polynomial
a4cd1e9d9962 [project @ 1999-11-20 17:22:48 by jwe]
jwe
parents: 2847
diff changeset
25 ## @tex
a4cd1e9d9962 [project @ 1999-11-20 17:22:48 by jwe]
jwe
parents: 2847
diff changeset
26 ## $$
a4cd1e9d9962 [project @ 1999-11-20 17:22:48 by jwe]
jwe
parents: 2847
diff changeset
27 ## v_1 z^{N-1} + \cdots + v_{N-1} z + v_N.
a4cd1e9d9962 [project @ 1999-11-20 17:22:48 by jwe]
jwe
parents: 2847
diff changeset
28 ## $$
a4cd1e9d9962 [project @ 1999-11-20 17:22:48 by jwe]
jwe
parents: 2847
diff changeset
29 ## @end tex
6850
9398f6a81bdf [project @ 2007-08-31 17:29:22 by jwe]
jwe
parents: 6372
diff changeset
30 ## @ifnottex
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3368
diff changeset
31 ##
3368
a4cd1e9d9962 [project @ 1999-11-20 17:22:48 by jwe]
jwe
parents: 2847
diff changeset
32 ## @example
9051
1bf0ce0930be Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents: 8920
diff changeset
33 ## v(1) * z^(N-1) + @dots{} + v(N-1) * z + v(N)
3368
a4cd1e9d9962 [project @ 1999-11-20 17:22:48 by jwe]
jwe
parents: 2847
diff changeset
34 ## @end example
10821
693e22af08ae Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents: 9211
diff changeset
35 ##
6850
9398f6a81bdf [project @ 2007-08-31 17:29:22 by jwe]
jwe
parents: 6372
diff changeset
36 ## @end ifnottex
9398f6a81bdf [project @ 2007-08-31 17:29:22 by jwe]
jwe
parents: 6372
diff changeset
37 ##
9398f6a81bdf [project @ 2007-08-31 17:29:22 by jwe]
jwe
parents: 6372
diff changeset
38 ## As an example, the following code finds the roots of the quadratic
9398f6a81bdf [project @ 2007-08-31 17:29:22 by jwe]
jwe
parents: 6372
diff changeset
39 ## polynomial
9398f6a81bdf [project @ 2007-08-31 17:29:22 by jwe]
jwe
parents: 6372
diff changeset
40 ## @tex
9398f6a81bdf [project @ 2007-08-31 17:29:22 by jwe]
jwe
parents: 6372
diff changeset
41 ## $$ p(x) = x^2 - 5. $$
9398f6a81bdf [project @ 2007-08-31 17:29:22 by jwe]
jwe
parents: 6372
diff changeset
42 ## @end tex
9398f6a81bdf [project @ 2007-08-31 17:29:22 by jwe]
jwe
parents: 6372
diff changeset
43 ## @ifnottex
10821
693e22af08ae Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents: 9211
diff changeset
44 ##
6850
9398f6a81bdf [project @ 2007-08-31 17:29:22 by jwe]
jwe
parents: 6372
diff changeset
45 ## @example
9398f6a81bdf [project @ 2007-08-31 17:29:22 by jwe]
jwe
parents: 6372
diff changeset
46 ## p(x) = x^2 - 5.
9398f6a81bdf [project @ 2007-08-31 17:29:22 by jwe]
jwe
parents: 6372
diff changeset
47 ## @end example
10821
693e22af08ae Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents: 9211
diff changeset
48 ##
6850
9398f6a81bdf [project @ 2007-08-31 17:29:22 by jwe]
jwe
parents: 6372
diff changeset
49 ## @end ifnottex
10821
693e22af08ae Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents: 9211
diff changeset
50 ##
6850
9398f6a81bdf [project @ 2007-08-31 17:29:22 by jwe]
jwe
parents: 6372
diff changeset
51 ## @example
9051
1bf0ce0930be Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents: 8920
diff changeset
52 ## @group
6850
9398f6a81bdf [project @ 2007-08-31 17:29:22 by jwe]
jwe
parents: 6372
diff changeset
53 ## c = [1, 0, -5];
9398f6a81bdf [project @ 2007-08-31 17:29:22 by jwe]
jwe
parents: 6372
diff changeset
54 ## roots(c)
9398f6a81bdf [project @ 2007-08-31 17:29:22 by jwe]
jwe
parents: 6372
diff changeset
55 ## @result{} 2.2361
9398f6a81bdf [project @ 2007-08-31 17:29:22 by jwe]
jwe
parents: 6372
diff changeset
56 ## @result{} -2.2361
9051
1bf0ce0930be Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents: 8920
diff changeset
57 ## @end group
6850
9398f6a81bdf [project @ 2007-08-31 17:29:22 by jwe]
jwe
parents: 6372
diff changeset
58 ## @end example
10821
693e22af08ae Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents: 9211
diff changeset
59 ##
6850
9398f6a81bdf [project @ 2007-08-31 17:29:22 by jwe]
jwe
parents: 6372
diff changeset
60 ## Note that the true result is
9398f6a81bdf [project @ 2007-08-31 17:29:22 by jwe]
jwe
parents: 6372
diff changeset
61 ## @tex
9398f6a81bdf [project @ 2007-08-31 17:29:22 by jwe]
jwe
parents: 6372
diff changeset
62 ## $\pm \sqrt{5}$
9398f6a81bdf [project @ 2007-08-31 17:29:22 by jwe]
jwe
parents: 6372
diff changeset
63 ## @end tex
9398f6a81bdf [project @ 2007-08-31 17:29:22 by jwe]
jwe
parents: 6372
diff changeset
64 ## @ifnottex
9398f6a81bdf [project @ 2007-08-31 17:29:22 by jwe]
jwe
parents: 6372
diff changeset
65 ## @math{+/- sqrt(5)}
9398f6a81bdf [project @ 2007-08-31 17:29:22 by jwe]
jwe
parents: 6372
diff changeset
66 ## @end ifnottex
9398f6a81bdf [project @ 2007-08-31 17:29:22 by jwe]
jwe
parents: 6372
diff changeset
67 ## which is roughly
9398f6a81bdf [project @ 2007-08-31 17:29:22 by jwe]
jwe
parents: 6372
diff changeset
68 ## @tex
9398f6a81bdf [project @ 2007-08-31 17:29:22 by jwe]
jwe
parents: 6372
diff changeset
69 ## $\pm 2.2361$.
9398f6a81bdf [project @ 2007-08-31 17:29:22 by jwe]
jwe
parents: 6372
diff changeset
70 ## @end tex
9398f6a81bdf [project @ 2007-08-31 17:29:22 by jwe]
jwe
parents: 6372
diff changeset
71 ## @ifnottex
9398f6a81bdf [project @ 2007-08-31 17:29:22 by jwe]
jwe
parents: 6372
diff changeset
72 ## @math{+/- 2.2361}.
9398f6a81bdf [project @ 2007-08-31 17:29:22 by jwe]
jwe
parents: 6372
diff changeset
73 ## @end ifnottex
9398f6a81bdf [project @ 2007-08-31 17:29:22 by jwe]
jwe
parents: 6372
diff changeset
74 ## @seealso{compan}
3368
a4cd1e9d9962 [project @ 1999-11-20 17:22:48 by jwe]
jwe
parents: 2847
diff changeset
75 ## @end deftypefn
2311
2b5788792cad [project @ 1996-07-11 20:18:38 by jwe]
jwe
parents: 2303
diff changeset
76
5428
2a16423e4aa0 [project @ 2005-08-23 18:38:27 by jwe]
jwe
parents: 5307
diff changeset
77 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
2312
204cc7db6f4a [project @ 1996-07-11 21:20:36 by jwe]
jwe
parents: 2311
diff changeset
78 ## Created: 24 December 1993
204cc7db6f4a [project @ 1996-07-11 21:20:36 by jwe]
jwe
parents: 2311
diff changeset
79 ## Adapted-By: jwe
204cc7db6f4a [project @ 1996-07-11 21:20:36 by jwe]
jwe
parents: 2311
diff changeset
80
787
c5d35bb139b6 [project @ 1994-10-11 00:34:13 by jwe]
jwe
parents:
diff changeset
81 function r = roots (v)
904
3470f1e25a79 [project @ 1994-11-09 21:22:15 by jwe]
jwe
parents: 787
diff changeset
82
6372
a9a95bb1bf9e [project @ 2007-03-01 15:39:30 by jwe]
jwe
parents: 6046
diff changeset
83 if (nargin != 1 || min (size (v)) > 1)
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5428
diff changeset
84 print_usage ();
7558
690c91f741b8 Apply a scaling factor to leading zero removal in roots.m
Sebastien Loisel
parents: 7411
diff changeset
85 elseif (any (isnan(v) | isinf(v)))
8664
e07e93c04080 style fixes
John W. Eaton <jwe@octave.org>
parents: 7558
diff changeset
86 error ("roots: inputs must not contain Inf or NaN");
1598
3739611f0803 [project @ 1995-11-01 01:35:40 by jwe]
jwe
parents: 1337
diff changeset
87 endif
2325
b5568c31ee2c [project @ 1996-07-15 22:20:21 by jwe]
jwe
parents: 2313
diff changeset
88
7558
690c91f741b8 Apply a scaling factor to leading zero removal in roots.m
Sebastien Loisel
parents: 7411
diff changeset
89 n = numel (v);
690c91f741b8 Apply a scaling factor to leading zero removal in roots.m
Sebastien Loisel
parents: 7411
diff changeset
90 v = v(:);
1025
f558749713f1 [project @ 1995-01-11 20:52:10 by jwe]
jwe
parents: 944
diff changeset
91
2303
5cffc4b8de57 [project @ 1996-06-24 09:15:24 by jwe]
jwe
parents: 1887
diff changeset
92 ## If v = [ 0 ... 0 v(k+1) ... v(k+l) 0 ... 0 ], we can remove the
2325
b5568c31ee2c [project @ 1996-07-15 22:20:21 by jwe]
jwe
parents: 2313
diff changeset
93 ## leading k zeros and n - k - l roots of the polynomial are zero.
904
3470f1e25a79 [project @ 1994-11-09 21:22:15 by jwe]
jwe
parents: 787
diff changeset
94
7558
690c91f741b8 Apply a scaling factor to leading zero removal in roots.m
Sebastien Loisel
parents: 7411
diff changeset
95 if (isempty (v))
690c91f741b8 Apply a scaling factor to leading zero removal in roots.m
Sebastien Loisel
parents: 7411
diff changeset
96 f = v;
690c91f741b8 Apply a scaling factor to leading zero removal in roots.m
Sebastien Loisel
parents: 7411
diff changeset
97 else
690c91f741b8 Apply a scaling factor to leading zero removal in roots.m
Sebastien Loisel
parents: 7411
diff changeset
98 f = find (v ./ max (abs (v)));
690c91f741b8 Apply a scaling factor to leading zero removal in roots.m
Sebastien Loisel
parents: 7411
diff changeset
99 endif
690c91f741b8 Apply a scaling factor to leading zero removal in roots.m
Sebastien Loisel
parents: 7411
diff changeset
100 m = numel (f);
2325
b5568c31ee2c [project @ 1996-07-15 22:20:21 by jwe]
jwe
parents: 2313
diff changeset
101
1598
3739611f0803 [project @ 1995-11-01 01:35:40 by jwe]
jwe
parents: 1337
diff changeset
102 if (m > 0 && n > 1)
3739611f0803 [project @ 1995-11-01 01:35:40 by jwe]
jwe
parents: 1337
diff changeset
103 v = v(f(1):f(m));
787
c5d35bb139b6 [project @ 1994-10-11 00:34:13 by jwe]
jwe
parents:
diff changeset
104 l = max (size (v));
c5d35bb139b6 [project @ 1994-10-11 00:34:13 by jwe]
jwe
parents:
diff changeset
105 if (l > 1)
c5d35bb139b6 [project @ 1994-10-11 00:34:13 by jwe]
jwe
parents:
diff changeset
106 A = diag (ones (1, l-2), -1);
1598
3739611f0803 [project @ 1995-11-01 01:35:40 by jwe]
jwe
parents: 1337
diff changeset
107 A(1,:) = -v(2:l) ./ v(1);
2325
b5568c31ee2c [project @ 1996-07-15 22:20:21 by jwe]
jwe
parents: 2313
diff changeset
108 r = eig (A);
1598
3739611f0803 [project @ 1995-11-01 01:35:40 by jwe]
jwe
parents: 1337
diff changeset
109 if (f(m) < n)
3739611f0803 [project @ 1995-11-01 01:35:40 by jwe]
jwe
parents: 1337
diff changeset
110 tmp = zeros (n - f(m), 1);
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3368
diff changeset
111 r = [r; tmp];
787
c5d35bb139b6 [project @ 1994-10-11 00:34:13 by jwe]
jwe
parents:
diff changeset
112 endif
c5d35bb139b6 [project @ 1994-10-11 00:34:13 by jwe]
jwe
parents:
diff changeset
113 else
c5d35bb139b6 [project @ 1994-10-11 00:34:13 by jwe]
jwe
parents:
diff changeset
114 r = zeros (n - f(m), 1);
c5d35bb139b6 [project @ 1994-10-11 00:34:13 by jwe]
jwe
parents:
diff changeset
115 endif
c5d35bb139b6 [project @ 1994-10-11 00:34:13 by jwe]
jwe
parents:
diff changeset
116 else
1598
3739611f0803 [project @ 1995-11-01 01:35:40 by jwe]
jwe
parents: 1337
diff changeset
117 r = [];
787
c5d35bb139b6 [project @ 1994-10-11 00:34:13 by jwe]
jwe
parents:
diff changeset
118 endif
2325
b5568c31ee2c [project @ 1996-07-15 22:20:21 by jwe]
jwe
parents: 2313
diff changeset
119
787
c5d35bb139b6 [project @ 1994-10-11 00:34:13 by jwe]
jwe
parents:
diff changeset
120 endfunction
7411
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
121
7558
690c91f741b8 Apply a scaling factor to leading zero removal in roots.m
Sebastien Loisel
parents: 7411
diff changeset
122 %!test
690c91f741b8 Apply a scaling factor to leading zero removal in roots.m
Sebastien Loisel
parents: 7411
diff changeset
123 %! p = [poly([3 3 3 3]), 0 0 0 0];
690c91f741b8 Apply a scaling factor to leading zero removal in roots.m
Sebastien Loisel
parents: 7411
diff changeset
124 %! r = sort (roots (p));
690c91f741b8 Apply a scaling factor to leading zero removal in roots.m
Sebastien Loisel
parents: 7411
diff changeset
125 %! assert (r, [0; 0; 0; 0; 3; 3; 3; 3], 0.001)
690c91f741b8 Apply a scaling factor to leading zero removal in roots.m
Sebastien Loisel
parents: 7411
diff changeset
126
7411
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
127 %!assert(all (all (abs (roots ([1, -6, 11, -6]) - [3; 2; 1]) < sqrt (eps))));
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
128
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
129 %!assert(isempty (roots ([])));
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
130
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
131 %!error roots ([1, 2; 3, 4]);
7558
690c91f741b8 Apply a scaling factor to leading zero removal in roots.m
Sebastien Loisel
parents: 7411
diff changeset
132
690c91f741b8 Apply a scaling factor to leading zero removal in roots.m
Sebastien Loisel
parents: 7411
diff changeset
133 %!assert(isempty (roots (1)));
7411
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
134
10821
693e22af08ae Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents: 9211
diff changeset
135 %!error roots ([1, 2; 3, 4]);
7558
690c91f741b8 Apply a scaling factor to leading zero removal in roots.m
Sebastien Loisel
parents: 7411
diff changeset
136
690c91f741b8 Apply a scaling factor to leading zero removal in roots.m
Sebastien Loisel
parents: 7411
diff changeset
137 %!error roots ([1 Inf 1]);
690c91f741b8 Apply a scaling factor to leading zero removal in roots.m
Sebastien Loisel
parents: 7411
diff changeset
138
690c91f741b8 Apply a scaling factor to leading zero removal in roots.m
Sebastien Loisel
parents: 7411
diff changeset
139 %!error roots ([1 NaN 1]);
690c91f741b8 Apply a scaling factor to leading zero removal in roots.m
Sebastien Loisel
parents: 7411
diff changeset
140
690c91f741b8 Apply a scaling factor to leading zero removal in roots.m
Sebastien Loisel
parents: 7411
diff changeset
141 %!assert(roots ([1e-200, -1e200, 1]), 1e-200)
690c91f741b8 Apply a scaling factor to leading zero removal in roots.m
Sebastien Loisel
parents: 7411
diff changeset
142 %!assert(roots ([1e-200, -1e200 * 1i, 1]), -1e-200 * 1i)