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