2847
|
1 ## Copyright (C) 1996, 1997 John W. Eaton |
6964
|
2 ## Copyright (C) 2007 Ben Abbott |
2313
|
3 ## |
|
4 ## This file is part of Octave. |
|
5 ## |
|
6 ## Octave is free software; you can redistribute it and/or modify it |
|
7 ## under the terms of the GNU General Public License as published by |
|
8 ## the Free Software Foundation; either version 2, or (at your option) |
|
9 ## any later version. |
|
10 ## |
|
11 ## Octave is distributed in the hope that it will be useful, but |
|
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
14 ## General Public License for more details. |
|
15 ## |
|
16 ## You should have received a copy of the GNU General Public License |
|
17 ## along with Octave; see the file COPYING. If not, write to the Free |
5307
|
18 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
19 ## 02110-1301, USA. |
904
|
20 |
3368
|
21 ## -*- texinfo -*- |
6978
|
22 ## @deftypefn {Function File} {[@var{r}, @var{p}, @var{k}, @var{e}] =} residue (@var{b}, @var{a}) |
|
23 ## Compute the partial fraction expansion for the quotient of the |
|
24 ## polynomials, @var{b} and @var{a}. |
3426
|
25 ## |
3368
|
26 ## @iftex |
|
27 ## @tex |
|
28 ## $$ |
6978
|
29 ## {B(s)\over A(s)} = \sum_{m=1}^M {r_m\over (s-p_m)^e_m} |
3368
|
30 ## + \sum_{i=1}^N k_i s^{N-i}. |
|
31 ## $$ |
|
32 ## @end tex |
|
33 ## @end iftex |
|
34 ## @ifinfo |
3426
|
35 ## |
3368
|
36 ## @example |
6978
|
37 ## B(s) M r(m) N |
3368
|
38 ## ---- = SUM ------------- + SUM k(i)*s^(N-i) |
6978
|
39 ## A(s) m=1 (s-p(m))^e(m) i=1 |
3368
|
40 ## @end example |
|
41 ## @end ifinfo |
3426
|
42 ## |
3368
|
43 ## @noindent |
3499
|
44 ## where @math{M} is the number of poles (the length of the @var{r}, |
6978
|
45 ## @var{p}, and @var{e}), the @var{k} vector is a polynomial of order @math{N-1} |
|
46 ## representing the direct contribution, and the @var{e} vector specifies |
|
47 ## the multiplicity of the mth residue's pole. |
3426
|
48 ## |
6964
|
49 ## For example, |
3426
|
50 ## |
3368
|
51 ## @example |
|
52 ## @group |
6964
|
53 ## b = [1, 1, 1]; |
|
54 ## a = [1, -5, 8, -4]; |
|
55 ## [r, p, k] = residue (b, a); |
3368
|
56 ## @result{} r = [-2, 7, 3] |
|
57 ## @result{} p = [2, 2, 1] |
|
58 ## @result{} k = [](0x0) |
|
59 ## @end group |
|
60 ## @end example |
3426
|
61 ## |
3368
|
62 ## @noindent |
6978
|
63 ## which represents the following partial fraction expansion |
3368
|
64 ## @iftex |
|
65 ## @tex |
|
66 ## $$ |
|
67 ## {s^2+s+1\over s^3-5s^2+8s-4} = {-2\over s-2} + {7\over (s-2)^2} + {3\over s-1} |
|
68 ## $$ |
|
69 ## @end tex |
|
70 ## @end iftex |
|
71 ## @ifinfo |
3426
|
72 ## |
3368
|
73 ## @example |
|
74 ## s^2 + s + 1 -2 7 3 |
2311
|
75 ## ------------------- = ----- + ------- + ----- |
|
76 ## s^3 - 5s^2 + 8s - 4 (s-2) (s-2)^2 (s-1) |
3368
|
77 ## @end example |
6964
|
78 ## |
3368
|
79 ## @end ifinfo |
6978
|
80 ## |
|
81 ## @deftypefnx {Function File} {[@var{b}, @var{a}] =} residue (@var{r}, @var{p}, @var{k}) |
|
82 ## Compute the reconstituted quotient of polynomials, |
|
83 ## @var{b}(s)/@var{a}(s), from the partial fraction expansion |
|
84 ## represented by the residues, poles, and a direct polynomial specified |
|
85 ## by @var{r}, @var{p} and @var{k}. |
|
86 ## |
|
87 ## For example, |
6964
|
88 ## |
|
89 ## @example |
|
90 ## @group |
|
91 ## r = [-2, 7, 3]; |
|
92 ## p = [2, 2, 1]; |
|
93 ## k = [1 0]; |
|
94 ## [b, a] = residue (r, p, k); |
|
95 ## @result{} b = [1, -5, 9, -3, 1] |
6978
|
96 ## @result{} a = [1, -5, 8, -4] |
6964
|
97 ## @end group |
|
98 ## @end example |
|
99 ## |
|
100 ## @noindent |
6978
|
101 ## which represents the following partial fraction expansion |
6964
|
102 ## @iftex |
|
103 ## @tex |
|
104 ## $$ |
6978
|
105 ## {-2\over s-2} + {7\over (s-2)^2} + {3\over s-1} + s = {s^4-5s^3+9s^2-3s+1\over s^3-5s^2+8s-4} |
6964
|
106 ## $$ |
|
107 ## @end tex |
|
108 ## @end iftex |
|
109 ## @ifinfo |
|
110 ## |
|
111 ## @example |
6978
|
112 ## -2 7 3 s^4 - 5s^3 + 9s^2 - 3s + 1 |
|
113 ## ----- + ------- + ----- + s = -------------------------- |
|
114 ## (s-2) (s-2)^2 (s-1) s^3 - 5s^2 + 8s - 4 |
6964
|
115 ## @end example |
|
116 ## @end ifinfo |
|
117 ## @seealso{poly, roots, conv, deconv, mpoles, polyval, polyderiv, polyinteg} |
3368
|
118 ## @end deftypefn |
1025
|
119 |
3202
|
120 ## Author: Tony Richardson <arichard@stark.cc.oh.us> |
6964
|
121 ## Author: Ben Abbott <bpabbott@mac.com> |
2312
|
122 ## Created: June 1994 |
|
123 ## Adapted-By: jwe |
559
|
124 |
6978
|
125 %!test |
|
126 %! b = [1, 1, 1]; |
|
127 %! a = [1, -5, 8, -4]; |
|
128 %! [r, p, k, e] = residue (b, a); |
|
129 %! assert ((abs (r - [-2; 7; 3]) < 1e-5 |
|
130 %! && abs (p - [2; 2; 1]) < 1e-7 |
|
131 %! && isempty (k) |
|
132 %! && e == [1; 2; 1])); |
|
133 %! k = [1 0]; |
|
134 %! [b, a] = residue (r, p, k); |
|
135 %! assert ((abs (b - [1, -5, 9, -3, 1]) < 1e-12 |
|
136 %! && abs (a - [1, -5, 8, -4]) < 1e-12)); |
|
137 |
|
138 %!test |
|
139 %! b = [1, 0, 1]; |
|
140 %! a = [1, 0, 18, 0, 81]; |
|
141 %! [r, p, k, e] = residue(b, a); |
|
142 %! assert ((abs (54*r - [-5i; 12; 5i; 12]) < 1e-6 |
|
143 %! && abs (p - [3i; 3i; -3i; -3i]) < 1e-7 |
|
144 %! && isempty (k) |
|
145 %! && e == [1; 2; 1; 2])); |
|
146 %! [br, ar] = residue (r, p, k); |
|
147 %! assert ((abs (br - b) < 1e-12 |
|
148 %! && abs (ar - a) < 1e-12)); |
|
149 |
|
150 function [r, p, k, e] = residue (b, a, varargin) |
559
|
151 |
1025
|
152 if (nargin < 2 || nargin > 3) |
6046
|
153 print_usage (); |
559
|
154 endif |
|
155 |
6964
|
156 toler = .001; |
|
157 |
|
158 if (nargin == 3) |
|
159 ## The inputs are the residue, pole, and direct part. Solve for the |
|
160 ## corresponding numerator and denominator polynomials |
|
161 [r, p] = rresidue (b, a, varargin{1}, toler); |
|
162 return |
|
163 end |
559
|
164 |
2303
|
165 ## Make sure both polynomials are in reduced form. |
1025
|
166 |
|
167 a = polyreduce (a); |
|
168 b = polyreduce (b); |
559
|
169 |
1025
|
170 b = b / a(1); |
|
171 a = a / a(1); |
559
|
172 |
1025
|
173 la = length (a); |
|
174 lb = length (b); |
559
|
175 |
2303
|
176 ## Handle special cases here. |
1025
|
177 |
|
178 if (la == 0 || lb == 0) |
559
|
179 k = r = p = e = []; |
|
180 return; |
|
181 elseif (la == 1) |
1025
|
182 k = b / a; |
559
|
183 r = p = e = []; |
|
184 return; |
|
185 endif |
|
186 |
2303
|
187 ## Find the poles. |
1025
|
188 |
|
189 p = roots (a); |
|
190 lp = length (p); |
559
|
191 |
5464
|
192 ## Determine if the poles are (effectively) zero. |
5463
|
193 |
6964
|
194 small = max (abs (p)); |
|
195 small = max ([small, 1] ) * 1e-8 * (1 + numel (p))^2; |
|
196 p(abs (p) < small) = 0; |
|
197 |
|
198 ## Determine if the poles are (effectively) real, or imaginary. |
5463
|
199 |
6964
|
200 index = (abs (imag (p)) < small); |
|
201 p(index) = real (p(index)); |
|
202 index = (abs (real (p)) < small); |
|
203 p(index) = 1i * imag (p(index)); |
1025
|
204 |
6964
|
205 ## Sort poles so that multiplicity loop will work. |
|
206 |
|
207 [e, indx] = mpoles (p, toler, 1); |
|
208 p = p (indx); |
559
|
209 |
2303
|
210 ## Find the direct term if there is one. |
1025
|
211 |
|
212 if (lb >= la) |
6964
|
213 ## Also return the reduced numerator. |
1025
|
214 [k, b] = deconv (b, a); |
|
215 lb = length (b); |
559
|
216 else |
|
217 k = []; |
|
218 endif |
|
219 |
1025
|
220 if (lp == 1) |
|
221 r = polyval (b, p); |
559
|
222 return; |
|
223 endif |
|
224 |
6964
|
225 ## Determine the order of the denominator and remaining numerator. |
|
226 ## With the direct term removed the potential order of the numerator |
|
227 ## is one less than the order of the denominator. |
1025
|
228 |
6964
|
229 aorder = numel (a) - 1; |
|
230 border = aorder - 1; |
1025
|
231 |
6964
|
232 ## Construct a system of equations relating the individual |
|
233 ## contributions from each residue to the complete numerator. |
559
|
234 |
6964
|
235 A = zeros (border+1, border+1); |
|
236 B = prepad (reshape (b, [numel(b), 1]), border+1, 0); |
|
237 for ip = 1:numel(p) |
|
238 ri = zeros (size (p)); |
|
239 ri(ip) = 1; |
|
240 A(:,ip) = prepad (rresidue (ri, p, [], toler), border+1, 0).'; |
|
241 endfor |
559
|
242 |
2303
|
243 ## Solve for the residues. |
1025
|
244 |
|
245 r = A \ B; |
559
|
246 |
|
247 endfunction |
6964
|
248 |
6978
|
249 function [pnum, pden, multp] = rresidue (r, p, k, toler) |
6964
|
250 |
|
251 ## Reconstitute the numerator and denominator polynomials from the |
|
252 ## residues, poles, and direct term. |
|
253 |
|
254 if (nargin < 2 || nargin > 4) |
|
255 print_usage (); |
|
256 endif |
|
257 |
|
258 if (nargin < 4) |
|
259 toler = []; |
|
260 endif |
|
261 |
|
262 if (nargin < 3) |
|
263 k = []; |
|
264 endif |
|
265 |
|
266 [multp, indx] = mpoles (p, toler, 0); |
|
267 |
|
268 p = p (indx); |
|
269 r = r (indx); |
|
270 |
|
271 indx = 1:numel(p); |
|
272 |
|
273 for n = indx |
|
274 pn = [1, -p(n)]; |
|
275 if n == 1 |
|
276 pden = pn; |
|
277 else |
|
278 pden = conv (pden, pn); |
|
279 endif |
|
280 endfor |
|
281 |
|
282 ## D is the order of the denominator |
|
283 ## K is the order of the direct polynomial |
|
284 ## N is the order of the resulting numerator |
|
285 ## pnum(1:(N+1)) is the numerator's polynomial |
|
286 ## pden(1:(D+1)) is the denominator's polynomial |
|
287 ## pm is the multible pole for the nth residue |
|
288 ## pn is the numerator contribution for the nth residue |
|
289 |
|
290 D = numel (pden) - 1; |
|
291 K = numel (k) - 1; |
|
292 N = K + D; |
|
293 pnum = zeros (1, N+1); |
|
294 for n = indx(abs(r)>0) |
|
295 p1 = [1, -p(n)]; |
|
296 for m = 1:multp(n) |
|
297 if m == 1 |
|
298 pm = p1; |
|
299 else |
|
300 pm = conv (pm, p1); |
|
301 endif |
|
302 endfor |
|
303 pn = deconv (pden, pm); |
|
304 pn = r(n) * pn; |
|
305 pnum = pnum + prepad ( pn, N+1, 0); |
|
306 endfor |
|
307 |
|
308 ## Add the direct term. |
|
309 |
|
310 if (numel (k)) |
|
311 pnum = pnum + conv (pden, k); |
|
312 endif |
|
313 |
|
314 ## Check for leading zeros and trim the polynomial coefficients. |
|
315 |
|
316 small = max ([max(abs(pden)), max(abs(pnum)), 1]) * eps; |
|
317 |
|
318 pnum (abs (pnum) < small) = 0; |
|
319 pden (abs (pden) < small) = 0; |
|
320 |
|
321 pnum = polyreduce (pnum); |
|
322 pden = polyreduce (pden); |
|
323 |
|
324 endfunction |
6968
|
325 |
|
326 %% test/octave.test/poly/residue-1.m |
|
327 %!test |
|
328 %! b = [1, 1, 1]; |
|
329 %! a = [1, -5, 8, -4]; |
|
330 %! [r, p, k, e] = residue (b, a); |
|
331 %! assert((abs (r - [-2; 7; 3]) < 1e-6 |
|
332 %! && abs (p - [2; 2; 1]) < 1e-7 |
|
333 %! && isempty (k) |
|
334 %! && e == [1; 2; 1])); |