2847
|
1 ## Copyright (C) 1996, 1997 John W. Eaton |
2313
|
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. |
904
|
19 |
3368
|
20 ## -*- texinfo -*- |
|
21 ## @deftypefn {Function File} {} residue (@var{b}, @var{a}, @var{tol}) |
|
22 ## If @var{b} and @var{a} are vectors of polynomial coefficients, then |
|
23 ## residue calculates the partial fraction expansion corresponding to the |
|
24 ## ratio of the two polynomials. |
|
25 ## @cindex partial fraction expansion |
3426
|
26 ## |
3368
|
27 ## The function @code{residue} returns @var{r}, @var{p}, @var{k}, and |
|
28 ## @var{e}, where the vector @var{r} contains the residue terms, @var{p} |
|
29 ## contains the pole values, @var{k} contains the coefficients of a direct |
|
30 ## polynomial term (if it exists) and @var{e} is a vector containing the |
|
31 ## powers of the denominators in the partial fraction terms. |
3426
|
32 ## |
3368
|
33 ## Assuming @var{b} and @var{a} represent polynomials |
|
34 ## @iftex |
|
35 ## @tex |
|
36 ## $P(s)$ and $Q(s)$ |
|
37 ## @end tex |
|
38 ## @end iftex |
|
39 ## @ifinfo |
|
40 ## P (s) and Q(s) |
|
41 ## @end ifinfo |
|
42 ## we have: |
|
43 ## @iftex |
|
44 ## @tex |
|
45 ## $$ |
|
46 ## {P(s)\over Q(s)} = \sum_{m=1}^M {r_m\over (s-p_m)^e_m} |
|
47 ## + \sum_{i=1}^N k_i s^{N-i}. |
|
48 ## $$ |
|
49 ## @end tex |
|
50 ## @end iftex |
|
51 ## @ifinfo |
3426
|
52 ## |
3368
|
53 ## @example |
2311
|
54 ## P(s) M r(m) N |
3368
|
55 ## ---- = SUM ------------- + SUM k(i)*s^(N-i) |
|
56 ## Q(s) m=1 (s-p(m))^e(m) i=1 |
|
57 ## @end example |
|
58 ## @end ifinfo |
3426
|
59 ## |
3368
|
60 ## @noindent |
3499
|
61 ## where @math{M} is the number of poles (the length of the @var{r}, |
|
62 ## @var{p}, and @var{e} vectors) and @math{N} is the length of the |
|
63 ## @var{k} vector. |
3426
|
64 ## |
3368
|
65 ## The argument @var{tol} is optional, and if not specified, a default |
|
66 ## value of 0.001 is assumed. The tolerance value is used to determine |
|
67 ## whether poles with small imaginary components are declared real. It is |
|
68 ## also used to determine if two poles are distinct. If the ratio of the |
|
69 ## imaginary part of a pole to the real part is less than @var{tol}, the |
|
70 ## imaginary part is discarded. If two poles are farther apart than |
|
71 ## @var{tol} they are distinct. For example, |
3426
|
72 ## |
3368
|
73 ## @example |
|
74 ## @group |
|
75 ## b = [1, 1, 1]; |
2311
|
76 ## a = [1, -5, 8, -4]; |
3368
|
77 ## [r, p, k, e] = residue (b, a); |
|
78 ## @result{} r = [-2, 7, 3] |
|
79 ## @result{} p = [2, 2, 1] |
|
80 ## @result{} k = [](0x0) |
|
81 ## @result{} e = [1, 2, 1] |
|
82 ## @end group |
|
83 ## @end example |
3426
|
84 ## |
3368
|
85 ## @noindent |
|
86 ## which implies the following partial fraction expansion |
|
87 ## @iftex |
|
88 ## @tex |
|
89 ## $$ |
|
90 ## {s^2+s+1\over s^3-5s^2+8s-4} = {-2\over s-2} + {7\over (s-2)^2} + {3\over s-1} |
|
91 ## $$ |
|
92 ## @end tex |
|
93 ## @end iftex |
|
94 ## @ifinfo |
3426
|
95 ## |
3368
|
96 ## @example |
|
97 ## s^2 + s + 1 -2 7 3 |
2311
|
98 ## ------------------- = ----- + ------- + ----- |
|
99 ## s^3 - 5s^2 + 8s - 4 (s-2) (s-2)^2 (s-1) |
3368
|
100 ## @end example |
|
101 ## @end ifinfo |
|
102 ## @end deftypefn |
3457
|
103 ## @seealso{poly, roots, conv, deconv, polyval, polyderiv, and polyinteg} |
1025
|
104 |
3202
|
105 ## Author: Tony Richardson <arichard@stark.cc.oh.us> |
2312
|
106 ## Created: June 1994 |
|
107 ## Adapted-By: jwe |
559
|
108 |
2312
|
109 function [r, p, k, e] = residue (b, a, toler) |
559
|
110 |
2303
|
111 ## Here's the method used to find the residues. |
|
112 ## The partial fraction expansion can be written as: |
|
113 ## |
|
114 ## |
|
115 ## P(s) D M(k) A(k,m) |
|
116 ## ---- = # # ------------- |
|
117 ## Q(s) k=1 m=1 (s - pr(k))^m |
|
118 ## |
|
119 ## (# is used to represent a summation) where D is the number of |
|
120 ## distinct roots, pr(k) is the kth distinct root, M(k) is the |
|
121 ## multiplicity of the root, and A(k,m) is the residue cooresponding |
|
122 ## to the kth distinct root with multiplicity m. For example, |
|
123 ## |
|
124 ## s^2 A(1,1) A(2,1) A(2,2) |
|
125 ## ------------------- = ------ + ------ + ------- |
|
126 ## s^3 + 4s^2 + 5s + 2 (s+2) (s+1) (s+1)^2 |
|
127 ## |
|
128 ## In this case there are two distinct roots (D=2 and pr = [-2 -1]), |
|
129 ## the first root has multiplicity one and the second multiplicity |
|
130 ## two (M = [1 2]) The residues are actually stored in vector format as |
|
131 ## r = [ A(1,1) A(2,1) A(2,2) ]. |
|
132 ## |
|
133 ## We then multiply both sides by Q(s). Continuing the example: |
|
134 ## |
|
135 ## s^2 = r(1)*(s+1)^2 + r(2)*(s+1)*(s+2) + r(3)*(s+2) |
|
136 ## |
|
137 ## or |
|
138 ## |
|
139 ## s^2 = r(1)*(s^2+2s+1) + r(2)*(s^2+3s+2) +r(3)*(s+2) |
|
140 ## |
|
141 ## The coefficients of the polynomials on the right are stored in a row |
|
142 ## vector called rhs, while the coefficients of the polynomial on the |
|
143 ## left is stored in a row vector called lhs. If the multiplicity of |
|
144 ## any root is greater than one we'll also need derivatives of this |
|
145 ## equation of order up to the maximum multiplicity minus one. The |
|
146 ## derivative coefficients are stored in successive rows of lhs and |
|
147 ## rhs. |
|
148 ## |
|
149 ## For our example lhs and rhs would be: |
|
150 ## |
|
151 ## | 1 0 0 | |
|
152 ## lhs = | | |
|
153 ## | 0 2 0 | |
|
154 ## |
|
155 ## | 1 2 1 1 3 2 0 1 2 | |
|
156 ## rhs = | | |
|
157 ## | 0 2 2 0 2 3 0 0 1 | |
|
158 ## |
|
159 ## We then form a vector B and a matrix A obtained by evaluating the |
|
160 ## polynomials in lhs and rhs at the pole values. If a pole has a |
|
161 ## multiplicity greater than one we also evaluate the derivative |
|
162 ## polynomials (successive rows) at the pole value. |
|
163 ## |
|
164 ## For our example we would have |
|
165 ## |
|
166 ## | 4| | 1 0 0 | | r(1) | |
|
167 ## | 1| = | 0 0 1 | * | r(2) | |
|
168 ## |-2| | 0 1 1 | | r(3) | |
|
169 ## |
|
170 ## We then solve for the residues using matrix division. |
559
|
171 |
1025
|
172 if (nargin < 2 || nargin > 3) |
3456
|
173 usage ("residue (b, a, toler)"); |
559
|
174 endif |
|
175 |
|
176 if (nargin == 2) |
|
177 toler = .001; |
|
178 endif |
|
179 |
2303
|
180 ## Make sure both polynomials are in reduced form. |
1025
|
181 |
|
182 a = polyreduce (a); |
|
183 b = polyreduce (b); |
559
|
184 |
1025
|
185 b = b / a(1); |
|
186 a = a / a(1); |
559
|
187 |
1025
|
188 la = length (a); |
|
189 lb = length (b); |
559
|
190 |
2303
|
191 ## Handle special cases here. |
1025
|
192 |
|
193 if (la == 0 || lb == 0) |
559
|
194 k = r = p = e = []; |
|
195 return; |
|
196 elseif (la == 1) |
1025
|
197 k = b / a; |
559
|
198 r = p = e = []; |
|
199 return; |
|
200 endif |
|
201 |
2303
|
202 ## Find the poles. |
1025
|
203 |
|
204 p = roots (a); |
|
205 lp = length (p); |
559
|
206 |
2303
|
207 ## Determine if the poles are (effectively) real. |
1025
|
208 |
|
209 index = find (abs (imag (p) ./ real (p)) < toler); |
|
210 if (length (index) != 0) |
|
211 p (index) = real (p (index)); |
559
|
212 endif |
|
213 |
2303
|
214 ## Find the direct term if there is one. |
1025
|
215 |
|
216 if (lb >= la) |
2303
|
217 ## Also returns the reduced numerator. |
1025
|
218 [k, b] = deconv (b, a); |
|
219 lb = length (b); |
559
|
220 else |
|
221 k = []; |
|
222 endif |
|
223 |
1025
|
224 if (lp == 1) |
|
225 r = polyval (b, p); |
559
|
226 e = 1; |
|
227 return; |
|
228 endif |
|
229 |
|
230 |
2303
|
231 ## We need to determine the number and multiplicity of the roots. |
|
232 ## |
|
233 ## D is the number of distinct roots. |
|
234 ## M is a vector of length D containing the multiplicity of each root. |
|
235 ## pr is a vector of length D containing only the distinct roots. |
|
236 ## e is a vector of length lp which indicates the power in the partial |
|
237 ## fraction expansion of each term in p. |
559
|
238 |
2303
|
239 ## Set initial values. We'll remove elements from pr as we find |
|
240 ## multiplicities. We'll shorten M afterwards. |
1025
|
241 |
|
242 e = ones (lp, 1); |
|
243 M = zeros (lp, 1); |
559
|
244 pr = p; |
1025
|
245 D = 1; |
|
246 M(1) = 1; |
559
|
247 |
1025
|
248 old_p_index = 1; |
|
249 new_p_index = 2; |
|
250 M_index = 1; |
|
251 pr_index = 2; |
|
252 |
|
253 while (new_p_index <= lp) |
|
254 if (abs (p (new_p_index) - p (old_p_index)) < toler) |
2303
|
255 ## We've found a multiple pole. |
1025
|
256 M (M_index) = M (M_index) + 1; |
|
257 e (new_p_index) = e (new_p_index-1) + 1; |
2303
|
258 ## Remove the pole from pr. |
1025
|
259 pr (pr_index) = []; |
559
|
260 else |
2303
|
261 ## It's a different pole. |
1025
|
262 D++; |
|
263 M_index++; |
|
264 M (M_index) = 1; |
|
265 old_p_index = new_p_index; |
|
266 pr_index++; |
559
|
267 endif |
|
268 new_p_index++; |
|
269 endwhile |
|
270 |
2303
|
271 ## Shorten M to it's proper length |
559
|
272 |
1025
|
273 M = M (1:D); |
|
274 |
2303
|
275 ## Now set up the polynomial matrices. |
559
|
276 |
|
277 MM = max(M); |
1025
|
278 |
2303
|
279 ## Left hand side polynomial |
1025
|
280 |
|
281 lhs = zeros (MM, lb); |
|
282 rhs = zeros (MM, lp*lp); |
|
283 lhs (1, :) = b; |
559
|
284 rhi = 1; |
|
285 dpi = 1; |
|
286 mpi = 1; |
1025
|
287 while (dpi <= D) |
559
|
288 for ind = 1:M(dpi) |
1025
|
289 if (mpi > 1 && (mpi+ind) <= lp) |
559
|
290 cp = [p(1:mpi-1); p(mpi+ind:lp)]; |
1025
|
291 elseif (mpi == 1) |
|
292 cp = p (mpi+ind:lp); |
559
|
293 else |
1025
|
294 cp = p (1:mpi-1); |
559
|
295 endif |
1025
|
296 rhs (1, rhi:rhi+lp-1) = prepad (poly (cp), lp); |
559
|
297 rhi = rhi + lp; |
|
298 endfor |
1025
|
299 mpi = mpi + M (dpi); |
559
|
300 dpi++; |
|
301 endwhile |
1025
|
302 if (MM > 1) |
559
|
303 for index = 2:MM |
1025
|
304 lhs (index, :) = prepad (polyderiv (lhs (index-1, :)), lb); |
559
|
305 ind = 1; |
|
306 for rhi = 1:lp |
1025
|
307 cp = rhs (index-1, ind:ind+lp-1); |
|
308 rhs (index, ind:ind+lp-1) = prepad (polyderiv (cp), lp); |
559
|
309 ind = ind + lp; |
|
310 endfor |
|
311 endfor |
|
312 endif |
|
313 |
2303
|
314 ## Now lhs contains the numerator polynomial and as many derivatives as |
|
315 ## are required. rhs is a matrix of polynomials, the first row |
|
316 ## contains the corresponding polynomial for each residue and |
|
317 ## successive rows are derivatives. |
559
|
318 |
2303
|
319 ## Now we need to evaluate the first row of lhs and rhs at each |
|
320 ## distinct pole value. If there are multiple poles we will also need |
|
321 ## to evaluate the derivatives at the pole value also. |
559
|
322 |
1025
|
323 B = zeros (lp, 1); |
|
324 A = zeros (lp, lp); |
559
|
325 |
|
326 dpi = 1; |
|
327 row = 1; |
1025
|
328 while (dpi <= D) |
559
|
329 for mi = 1:M(dpi) |
1025
|
330 B (row) = polyval (lhs (mi, :), pr (dpi)); |
559
|
331 ci = 1; |
|
332 for col = 1:lp |
1025
|
333 cp = rhs (mi, ci:ci+lp-1); |
|
334 A (row, col) = polyval (cp, pr(dpi)); |
559
|
335 ci = ci + lp; |
|
336 endfor |
|
337 row++; |
|
338 endfor |
|
339 dpi++; |
|
340 endwhile |
|
341 |
2303
|
342 ## Solve for the residues. |
1025
|
343 |
|
344 r = A \ B; |
559
|
345 |
|
346 endfunction |