annotate scripts/polynomial/residue.m @ 1025:f558749713f1

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