annotate scripts/polynomial/residue.m @ 2311:2b5788792cad

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