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