comparison scripts/polynomial/mpoles.m @ 6964:33f20a41aeea

[project @ 2007-10-06 04:31:18 by jwe]
author jwe
date Sat, 06 Oct 2007 04:31:18 +0000
parents
children cc049a392a97
comparison
equal deleted inserted replaced
6963:642f481d2d50 6964:33f20a41aeea
1 ## Copyright (C) 2007 Ben Abbott
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, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 ## 02110-1301, USA.
19
20 ## -*- texinfo -*-
21 ## @deftypefn {Function File} {[@var{multp}, @var{indx}] =} mpoles (@var{p})
22 ## @deftypefnx {Function File} {[@var{multp}, @var{indx}] =} mpoles (@var{p}, @var{tol})
23 ## @deftypefnx {Function File} {[@var{multp}, @var{indx}] =} mpoles (@var{p}, @var{tol}, @var{reorder})
24 ## Identifiy unique poles in @var{p} and associates their multiplicity,
25 ## ordering them from largest to smallest.
26 ##
27 ## If the relative difference of the poles is less than @var{tol}, then
28 ## they are considered to be multiples. The default value for @var{tol}
29 ## is 0.001.
30 ##
31 ## If the optional parameter @var{reorder} is zero, poles are not sorted.
32 ##
33 ## The value @var{multp} is a vector specifying the multiplicity of the
34 ## poles. @var{multp}(:) refers to mulitplicity of @var{p}(@var{indx}(:)).
35 ##
36 ## For example,
37 ##
38 ## @example
39 ## @group
40 ## p = [2 3 1 1 2];
41 ## [m, n] = mpoles(p);
42 ## @result{} m = [1; 1; 2; 1; 2]
43 ## @result{} n = [2; 5; 1; 4; 3]
44 ## @result{} p(n) = [3, 2, 2, 1, 1]
45 ## @end group
46 ## @end example
47 ##
48 ## @seealso{poly, roots, conv, deconv, polyval, polyderiv, polyinteg, residue}
49 ## @end deftypefn
50
51 ## Author: Ben Abbott <bpabbott@mac.com>
52 ## Created: Sept 30, 2007
53
54 function [multp, indx] = mpoles (p, tol, reorder)
55
56 if (nargin < 1 || nargin > 3)
57 print_usage ();
58 endif
59
60 if (nargin < 2 || isempty (tol))
61 tol = 0.001;
62 endif
63
64 if (nargin < 3 || isempty (reorder))
65 reorder = true;
66 endif
67
68 Np = numel (p);
69
70 ## Force the poles to be a column vector.
71
72 p = p(:);
73
74 ## Sort the poles according to their magnitidues, largest first.
75
76 if (reorder)
77 ## Sort with smallest magnitude first.
78 [p, ordr] = sort (p);
79 ## Reverse order, largest maginitude first.
80 n = Np:-1:1;
81 p = p(n);
82 ordr = ordr(n);
83 else
84 ordr = 1:Np;
85 endif
86
87 ## Find pole multiplicty by comparing the relative differnce in the
88 ## poles.
89
90 multp = zeros (Np, 1);
91 indx = [];
92 n = find (multp == 0, 1);
93 while (n)
94 dp = abs (p-p(n));
95 if (p(n) == 0.0)
96 p0 = mean (abs (p(find (abs (p) > 0))));
97 if (isempty (p0))
98 p0 = 1;
99 end
100 else
101 p0 = abs (p(n));
102 endif
103 k = find (dp < tol * p0);
104 m = 1:numel (k);
105 multp(k) = m;
106 indx = [indx; k];
107 n = find (multp == 0, 1);
108 endwhile
109 multp = multp(indx);
110 indx = indx(ordr);
111
112 endfunction