annotate scripts/polynomial/mpoles.m @ 17530:0f45d9dd8107

test: Fix 4 failing plot demos. * scripts/plot/legend.m: Demo #11, use 'h' for returned handle. * scripts/plot/plot.m: Fix typo "lenths" -> "lengths". * scripts/plot/ribbon.m: Remove unnecessary call to meshgrid now that sombrero returns meshgridded data. * scripts/plot/trimesh.m: Set colormap so plot is always reproducible.
author Rik <rik@octave.org>
date Tue, 01 Oct 2013 15:39:20 -0700
parents f3d52523cde1
children d63878346099
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
14138
72c96de7a403 maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents: 14104
diff changeset
1 ## Copyright (C) 2007-2012 Ben Abbott
6964
33f20a41aeea [project @ 2007-10-06 04:31:18 by jwe]
jwe
parents:
diff changeset
2 ##
33f20a41aeea [project @ 2007-10-06 04:31:18 by jwe]
jwe
parents:
diff changeset
3 ## This file is part of Octave.
33f20a41aeea [project @ 2007-10-06 04:31:18 by jwe]
jwe
parents:
diff changeset
4 ##
33f20a41aeea [project @ 2007-10-06 04:31:18 by jwe]
jwe
parents:
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
33f20a41aeea [project @ 2007-10-06 04:31:18 by jwe]
jwe
parents:
diff changeset
6 ## 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: 6998
diff changeset
7 ## the Free Software Foundation; either version 3 of the License, or (at
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6998
diff changeset
8 ## your option) any later version.
6964
33f20a41aeea [project @ 2007-10-06 04:31:18 by jwe]
jwe
parents:
diff changeset
9 ##
33f20a41aeea [project @ 2007-10-06 04:31:18 by jwe]
jwe
parents:
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
33f20a41aeea [project @ 2007-10-06 04:31:18 by jwe]
jwe
parents:
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
33f20a41aeea [project @ 2007-10-06 04:31:18 by jwe]
jwe
parents:
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
33f20a41aeea [project @ 2007-10-06 04:31:18 by jwe]
jwe
parents:
diff changeset
13 ## General Public License for more details.
33f20a41aeea [project @ 2007-10-06 04:31:18 by jwe]
jwe
parents:
diff changeset
14 ##
33f20a41aeea [project @ 2007-10-06 04:31:18 by jwe]
jwe
parents:
diff changeset
15 ## 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: 6998
diff changeset
16 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6998
diff changeset
17 ## <http://www.gnu.org/licenses/>.
6964
33f20a41aeea [project @ 2007-10-06 04:31:18 by jwe]
jwe
parents:
diff changeset
18
33f20a41aeea [project @ 2007-10-06 04:31:18 by jwe]
jwe
parents:
diff changeset
19 ## -*- texinfo -*-
14104
614505385171 doc: Overhaul docstrings for polynomial functions.
Rik <octave@nomad.inbox5.com>
parents: 13963
diff changeset
20 ## @deftypefn {Function File} {[@var{multp}, @var{idxp}] =} mpoles (@var{p})
614505385171 doc: Overhaul docstrings for polynomial functions.
Rik <octave@nomad.inbox5.com>
parents: 13963
diff changeset
21 ## @deftypefnx {Function File} {[@var{multp}, @var{idxp}] =} mpoles (@var{p}, @var{tol})
614505385171 doc: Overhaul docstrings for polynomial functions.
Rik <octave@nomad.inbox5.com>
parents: 13963
diff changeset
22 ## @deftypefnx {Function File} {[@var{multp}, @var{idxp}] =} mpoles (@var{p}, @var{tol}, @var{reorder})
614505385171 doc: Overhaul docstrings for polynomial functions.
Rik <octave@nomad.inbox5.com>
parents: 13963
diff changeset
23 ## Identify unique poles in @var{p} and their associated multiplicity. The
614505385171 doc: Overhaul docstrings for polynomial functions.
Rik <octave@nomad.inbox5.com>
parents: 13963
diff changeset
24 ## output is ordered from largest pole to smallest pole.
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
25 ##
14104
614505385171 doc: Overhaul docstrings for polynomial functions.
Rik <octave@nomad.inbox5.com>
parents: 13963
diff changeset
26 ## If the relative difference of two poles is less than @var{tol} then
6964
33f20a41aeea [project @ 2007-10-06 04:31:18 by jwe]
jwe
parents:
diff changeset
27 ## they are considered to be multiples. The default value for @var{tol}
33f20a41aeea [project @ 2007-10-06 04:31:18 by jwe]
jwe
parents:
diff changeset
28 ## is 0.001.
33f20a41aeea [project @ 2007-10-06 04:31:18 by jwe]
jwe
parents:
diff changeset
29 ##
33f20a41aeea [project @ 2007-10-06 04:31:18 by jwe]
jwe
parents:
diff changeset
30 ## If the optional parameter @var{reorder} is zero, poles are not sorted.
33f20a41aeea [project @ 2007-10-06 04:31:18 by jwe]
jwe
parents:
diff changeset
31 ##
14104
614505385171 doc: Overhaul docstrings for polynomial functions.
Rik <octave@nomad.inbox5.com>
parents: 13963
diff changeset
32 ## The output @var{multp} is a vector specifying the multiplicity of the
614505385171 doc: Overhaul docstrings for polynomial functions.
Rik <octave@nomad.inbox5.com>
parents: 13963
diff changeset
33 ## poles. @code{@var{multp}(n)} refers to the multiplicity of the Nth pole
614505385171 doc: Overhaul docstrings for polynomial functions.
Rik <octave@nomad.inbox5.com>
parents: 13963
diff changeset
34 ## @code{@var{p}(@var{idxp}(n))}.
6964
33f20a41aeea [project @ 2007-10-06 04:31:18 by jwe]
jwe
parents:
diff changeset
35 ##
10821
693e22af08ae Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
36 ## For example:
6964
33f20a41aeea [project @ 2007-10-06 04:31:18 by jwe]
jwe
parents:
diff changeset
37 ##
33f20a41aeea [project @ 2007-10-06 04:31:18 by jwe]
jwe
parents:
diff changeset
38 ## @example
33f20a41aeea [project @ 2007-10-06 04:31:18 by jwe]
jwe
parents:
diff changeset
39 ## @group
33f20a41aeea [project @ 2007-10-06 04:31:18 by jwe]
jwe
parents:
diff changeset
40 ## p = [2 3 1 1 2];
14327
4d917a6a858b doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
41 ## [m, n] = mpoles (p)
4d917a6a858b doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
42 ## @result{} m = [1; 1; 2; 1; 2]
4d917a6a858b doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
43 ## @result{} n = [2; 5; 1; 4; 3]
4d917a6a858b doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
44 ## @result{} p(n) = [3, 2, 2, 1, 1]
6964
33f20a41aeea [project @ 2007-10-06 04:31:18 by jwe]
jwe
parents:
diff changeset
45 ## @end group
33f20a41aeea [project @ 2007-10-06 04:31:18 by jwe]
jwe
parents:
diff changeset
46 ## @end example
33f20a41aeea [project @ 2007-10-06 04:31:18 by jwe]
jwe
parents:
diff changeset
47 ##
14104
614505385171 doc: Overhaul docstrings for polynomial functions.
Rik <octave@nomad.inbox5.com>
parents: 13963
diff changeset
48 ## @seealso{residue, poly, roots, conv, deconv}
6964
33f20a41aeea [project @ 2007-10-06 04:31:18 by jwe]
jwe
parents:
diff changeset
49 ## @end deftypefn
33f20a41aeea [project @ 2007-10-06 04:31:18 by jwe]
jwe
parents:
diff changeset
50
33f20a41aeea [project @ 2007-10-06 04:31:18 by jwe]
jwe
parents:
diff changeset
51 ## Author: Ben Abbott <bpabbott@mac.com>
33f20a41aeea [project @ 2007-10-06 04:31:18 by jwe]
jwe
parents:
diff changeset
52 ## Created: Sept 30, 2007
33f20a41aeea [project @ 2007-10-06 04:31:18 by jwe]
jwe
parents:
diff changeset
53
33f20a41aeea [project @ 2007-10-06 04:31:18 by jwe]
jwe
parents:
diff changeset
54 function [multp, indx] = mpoles (p, tol, reorder)
33f20a41aeea [project @ 2007-10-06 04:31:18 by jwe]
jwe
parents:
diff changeset
55
33f20a41aeea [project @ 2007-10-06 04:31:18 by jwe]
jwe
parents:
diff changeset
56 if (nargin < 1 || nargin > 3)
33f20a41aeea [project @ 2007-10-06 04:31:18 by jwe]
jwe
parents:
diff changeset
57 print_usage ();
33f20a41aeea [project @ 2007-10-06 04:31:18 by jwe]
jwe
parents:
diff changeset
58 endif
33f20a41aeea [project @ 2007-10-06 04:31:18 by jwe]
jwe
parents:
diff changeset
59
33f20a41aeea [project @ 2007-10-06 04:31:18 by jwe]
jwe
parents:
diff changeset
60 if (nargin < 2 || isempty (tol))
33f20a41aeea [project @ 2007-10-06 04:31:18 by jwe]
jwe
parents:
diff changeset
61 tol = 0.001;
33f20a41aeea [project @ 2007-10-06 04:31:18 by jwe]
jwe
parents:
diff changeset
62 endif
33f20a41aeea [project @ 2007-10-06 04:31:18 by jwe]
jwe
parents:
diff changeset
63
33f20a41aeea [project @ 2007-10-06 04:31:18 by jwe]
jwe
parents:
diff changeset
64 if (nargin < 3 || isempty (reorder))
33f20a41aeea [project @ 2007-10-06 04:31:18 by jwe]
jwe
parents:
diff changeset
65 reorder = true;
33f20a41aeea [project @ 2007-10-06 04:31:18 by jwe]
jwe
parents:
diff changeset
66 endif
33f20a41aeea [project @ 2007-10-06 04:31:18 by jwe]
jwe
parents:
diff changeset
67
33f20a41aeea [project @ 2007-10-06 04:31:18 by jwe]
jwe
parents:
diff changeset
68 Np = numel (p);
33f20a41aeea [project @ 2007-10-06 04:31:18 by jwe]
jwe
parents:
diff changeset
69
33f20a41aeea [project @ 2007-10-06 04:31:18 by jwe]
jwe
parents:
diff changeset
70 ## Force the poles to be a column vector.
33f20a41aeea [project @ 2007-10-06 04:31:18 by jwe]
jwe
parents:
diff changeset
71
33f20a41aeea [project @ 2007-10-06 04:31:18 by jwe]
jwe
parents:
diff changeset
72 p = p(:);
33f20a41aeea [project @ 2007-10-06 04:31:18 by jwe]
jwe
parents:
diff changeset
73
33f20a41aeea [project @ 2007-10-06 04:31:18 by jwe]
jwe
parents:
diff changeset
74 ## Sort the poles according to their magnitidues, largest first.
33f20a41aeea [project @ 2007-10-06 04:31:18 by jwe]
jwe
parents:
diff changeset
75
33f20a41aeea [project @ 2007-10-06 04:31:18 by jwe]
jwe
parents:
diff changeset
76 if (reorder)
33f20a41aeea [project @ 2007-10-06 04:31:18 by jwe]
jwe
parents:
diff changeset
77 ## Sort with smallest magnitude first.
33f20a41aeea [project @ 2007-10-06 04:31:18 by jwe]
jwe
parents:
diff changeset
78 [p, ordr] = sort (p);
33f20a41aeea [project @ 2007-10-06 04:31:18 by jwe]
jwe
parents:
diff changeset
79 ## Reverse order, largest maginitude first.
33f20a41aeea [project @ 2007-10-06 04:31:18 by jwe]
jwe
parents:
diff changeset
80 n = Np:-1:1;
33f20a41aeea [project @ 2007-10-06 04:31:18 by jwe]
jwe
parents:
diff changeset
81 p = p(n);
33f20a41aeea [project @ 2007-10-06 04:31:18 by jwe]
jwe
parents:
diff changeset
82 ordr = ordr(n);
33f20a41aeea [project @ 2007-10-06 04:31:18 by jwe]
jwe
parents:
diff changeset
83 else
33f20a41aeea [project @ 2007-10-06 04:31:18 by jwe]
jwe
parents:
diff changeset
84 ordr = 1:Np;
33f20a41aeea [project @ 2007-10-06 04:31:18 by jwe]
jwe
parents:
diff changeset
85 endif
33f20a41aeea [project @ 2007-10-06 04:31:18 by jwe]
jwe
parents:
diff changeset
86
33f20a41aeea [project @ 2007-10-06 04:31:18 by jwe]
jwe
parents:
diff changeset
87 ## Find pole multiplicty by comparing the relative differnce in the
33f20a41aeea [project @ 2007-10-06 04:31:18 by jwe]
jwe
parents:
diff changeset
88 ## poles.
33f20a41aeea [project @ 2007-10-06 04:31:18 by jwe]
jwe
parents:
diff changeset
89
33f20a41aeea [project @ 2007-10-06 04:31:18 by jwe]
jwe
parents:
diff changeset
90 multp = zeros (Np, 1);
33f20a41aeea [project @ 2007-10-06 04:31:18 by jwe]
jwe
parents:
diff changeset
91 indx = [];
33f20a41aeea [project @ 2007-10-06 04:31:18 by jwe]
jwe
parents:
diff changeset
92 n = find (multp == 0, 1);
33f20a41aeea [project @ 2007-10-06 04:31:18 by jwe]
jwe
parents:
diff changeset
93 while (n)
33f20a41aeea [project @ 2007-10-06 04:31:18 by jwe]
jwe
parents:
diff changeset
94 dp = abs (p-p(n));
33f20a41aeea [project @ 2007-10-06 04:31:18 by jwe]
jwe
parents:
diff changeset
95 if (p(n) == 0.0)
9360
df42106beec8 mpoles.m: Fix infinite loop for a multiplicity of poles at zero. Test added.
Ben Abbott <bpabbott@mac.com>
parents: 9245
diff changeset
96 if (any (abs (p) > 0 & isfinite (p)))
df42106beec8 mpoles.m: Fix infinite loop for a multiplicity of poles at zero. Test added.
Ben Abbott <bpabbott@mac.com>
parents: 9245
diff changeset
97 p0 = mean (abs (p(abs (p) > 0 & isfinite (p))));
df42106beec8 mpoles.m: Fix infinite loop for a multiplicity of poles at zero. Test added.
Ben Abbott <bpabbott@mac.com>
parents: 9245
diff changeset
98 else
6964
33f20a41aeea [project @ 2007-10-06 04:31:18 by jwe]
jwe
parents:
diff changeset
99 p0 = 1;
7151
aeeb646f6538 [project @ 2007-11-09 19:34:17 by jwe]
jwe
parents: 7016
diff changeset
100 endif
6964
33f20a41aeea [project @ 2007-10-06 04:31:18 by jwe]
jwe
parents:
diff changeset
101 else
33f20a41aeea [project @ 2007-10-06 04:31:18 by jwe]
jwe
parents:
diff changeset
102 p0 = abs (p(n));
33f20a41aeea [project @ 2007-10-06 04:31:18 by jwe]
jwe
parents:
diff changeset
103 endif
33f20a41aeea [project @ 2007-10-06 04:31:18 by jwe]
jwe
parents:
diff changeset
104 k = find (dp < tol * p0);
7360
1e716cb4b2a3 [project @ 2008-01-11 02:59:02 by jwe]
jwe
parents: 7151
diff changeset
105 ## Poles can only be members of one multiplicity group.
1e716cb4b2a3 [project @ 2008-01-11 02:59:02 by jwe]
jwe
parents: 7151
diff changeset
106 if (numel (indx))
1e716cb4b2a3 [project @ 2008-01-11 02:59:02 by jwe]
jwe
parents: 7151
diff changeset
107 k = k(! ismember (k, indx));
1e716cb4b2a3 [project @ 2008-01-11 02:59:02 by jwe]
jwe
parents: 7151
diff changeset
108 endif
6964
33f20a41aeea [project @ 2007-10-06 04:31:18 by jwe]
jwe
parents:
diff changeset
109 m = 1:numel (k);
33f20a41aeea [project @ 2007-10-06 04:31:18 by jwe]
jwe
parents:
diff changeset
110 multp(k) = m;
33f20a41aeea [project @ 2007-10-06 04:31:18 by jwe]
jwe
parents:
diff changeset
111 indx = [indx; k];
33f20a41aeea [project @ 2007-10-06 04:31:18 by jwe]
jwe
parents:
diff changeset
112 n = find (multp == 0, 1);
33f20a41aeea [project @ 2007-10-06 04:31:18 by jwe]
jwe
parents:
diff changeset
113 endwhile
33f20a41aeea [project @ 2007-10-06 04:31:18 by jwe]
jwe
parents:
diff changeset
114 multp = multp(indx);
6998
cc049a392a97 [project @ 2007-10-10 16:51:51 by jwe]
jwe
parents: 6964
diff changeset
115 indx = ordr(indx);
6964
33f20a41aeea [project @ 2007-10-06 04:31:18 by jwe]
jwe
parents:
diff changeset
116
33f20a41aeea [project @ 2007-10-06 04:31:18 by jwe]
jwe
parents:
diff changeset
117 endfunction
9360
df42106beec8 mpoles.m: Fix infinite loop for a multiplicity of poles at zero. Test added.
Ben Abbott <bpabbott@mac.com>
parents: 9245
diff changeset
118
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14327
diff changeset
119
9360
df42106beec8 mpoles.m: Fix infinite loop for a multiplicity of poles at zero. Test added.
Ben Abbott <bpabbott@mac.com>
parents: 9245
diff changeset
120 %!test
df42106beec8 mpoles.m: Fix infinite loop for a multiplicity of poles at zero. Test added.
Ben Abbott <bpabbott@mac.com>
parents: 9245
diff changeset
121 %! [mp, n] = mpoles ([0 0], 0.01);
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14327
diff changeset
122 %! assert (mp, [1; 2]);
9360
df42106beec8 mpoles.m: Fix infinite loop for a multiplicity of poles at zero. Test added.
Ben Abbott <bpabbott@mac.com>
parents: 9245
diff changeset
123