annotate scripts/control/util/zgscal.m @ 7132:b01db194c526

[project @ 2007-11-08 16:17:34 by jwe]
author jwe
date Thu, 08 Nov 2007 16:17:34 +0000
parents f084ba47812b
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7017
a1dbe9d80eee [project @ 2007-10-12 21:27:11 by jwe]
jwe
parents: 7016
diff changeset
1 ## Copyright (C) 1996, 1998, 2000, 2002, 2004, 2005, 2007
a1dbe9d80eee [project @ 2007-10-12 21:27:11 by jwe]
jwe
parents: 7016
diff changeset
2 ## Auburn University. All rights reserved.
3431
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
3 ##
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
4 ## This file is part of Octave.
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
5 ##
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
6 ## Octave is free software; you can redistribute it and/or modify it
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 5307
diff changeset
7 ## under the terms of the GNU General Public License as published by
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 5307
diff changeset
8 ## the Free Software Foundation; either version 3 of the License, or (at
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 5307
diff changeset
9 ## your option) any later version.
3431
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
10 ##
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 5307
diff changeset
11 ## Octave is distributed in the hope that it will be useful, but
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 5307
diff changeset
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 5307
diff changeset
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 5307
diff changeset
14 ## General Public License for more details.
3431
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
15 ##
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
16 ## You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 5307
diff changeset
17 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 5307
diff changeset
18 ## <http://www.gnu.org/licenses/>.
3431
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
19
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
20 ## -*- texinfo -*-
5016
bdbee5282954 [project @ 2004-09-22 02:50:35 by jwe]
jwe
parents: 4030
diff changeset
21 ## @deftypefn {Function File} {@var{x} =} zgscal (@var{f}, @var{z}, @var{n}, @var{m}, @var{p})
3431
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
22 ## Generalized conjugate gradient iteration to
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
23 ## solve zero-computation generalized eigenvalue problem balancing equation
5016
bdbee5282954 [project @ 2004-09-22 02:50:35 by jwe]
jwe
parents: 4030
diff changeset
24 ## @math{fx=z}; called by @command{zgepbal}.
3431
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
25 ## @end deftypefn
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
26
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
27 ## References:
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
28 ## ZGEP: Hodel, "Computation of Zeros with Balancing," 1992, submitted to LAA
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
29 ## Generalized CG: Golub and Van Loan, "Matrix Computations, 2nd ed" 1989
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
30
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
31 ## Author: A. S. Hodel <a.s.hodel@eng.auburn.edu>
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
32 ## Created: July 24, 1992
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
33 ## Conversion to Octave R. Bruce Tenison July 3, 1994
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
34
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
35 function x = zgscal (a, b, c, d, z, n, m, p)
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
36
7125
f084ba47812b [project @ 2007-11-08 02:29:23 by jwe]
jwe
parents: 7017
diff changeset
37 if (nargin != 8)
f084ba47812b [project @ 2007-11-08 02:29:23 by jwe]
jwe
parents: 7017
diff changeset
38 print_usage ();
f084ba47812b [project @ 2007-11-08 02:29:23 by jwe]
jwe
parents: 7017
diff changeset
39 endif
f084ba47812b [project @ 2007-11-08 02:29:23 by jwe]
jwe
parents: 7017
diff changeset
40
3431
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
41 ## initialize parameters:
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
42 ## Givens rotations, diagonalized 2x2 block of F, gcg vector initialization
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
43
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
44 nmp = n+m+p;
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
45
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
46 ## x_0 = x_{-1} = 0, r_0 = z
7132
b01db194c526 [project @ 2007-11-08 16:17:34 by jwe]
jwe
parents: 7125
diff changeset
47 x = zeros (nmp, 1);
3431
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
48 xk1 = x;
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
49 xk2 = x;
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
50 rk1 = z;
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
51 k = 0;
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
52
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
53 ## construct balancing least squares problem
7132
b01db194c526 [project @ 2007-11-08 16:17:34 by jwe]
jwe
parents: 7125
diff changeset
54 F = eye (nmp);
b01db194c526 [project @ 2007-11-08 16:17:34 by jwe]
jwe
parents: 7125
diff changeset
55 for kk = 1:nmp
b01db194c526 [project @ 2007-11-08 16:17:34 by jwe]
jwe
parents: 7125
diff changeset
56 F(1:nmp,kk) = zgfmul (a, b, c, d, F(:,kk));
3431
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
57 endfor
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
58
7132
b01db194c526 [project @ 2007-11-08 16:17:34 by jwe]
jwe
parents: 7125
diff changeset
59 [U, H, k1] = krylov (F, z, nmp, 1e-12, 1);
b01db194c526 [project @ 2007-11-08 16:17:34 by jwe]
jwe
parents: 7125
diff changeset
60 if (! issquare (H))
b01db194c526 [project @ 2007-11-08 16:17:34 by jwe]
jwe
parents: 7125
diff changeset
61 if (columns (H) != k1)
b01db194c526 [project @ 2007-11-08 16:17:34 by jwe]
jwe
parents: 7125
diff changeset
62 error ("zgscal(tzero): k1=%d, columns(H)=%d", k1, columns (H));
b01db194c526 [project @ 2007-11-08 16:17:34 by jwe]
jwe
parents: 7125
diff changeset
63 elseif (rows (H) != k1+1)
b01db194c526 [project @ 2007-11-08 16:17:34 by jwe]
jwe
parents: 7125
diff changeset
64 error ("zgscal: k1=%d, rows(H) = %d", k1, rows (H));
b01db194c526 [project @ 2007-11-08 16:17:34 by jwe]
jwe
parents: 7125
diff changeset
65 elseif (norm (H(k1+1,:)) > 1e-12*norm (H, "inf"))
3431
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
66 zgscal_last_row_of_H = H(k1+1,:)
7132
b01db194c526 [project @ 2007-11-08 16:17:34 by jwe]
jwe
parents: 7125
diff changeset
67 error ("zgscal: last row of H nonzero (norm(H)=%e)", norm (H, "inf"))
3431
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
68 endif
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
69 H = H(1:k1,1:k1);
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
70 U = U(:,1:k1);
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
71 endif
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
72
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
73 ## tridiagonal H can still be rank deficient, so do permuted qr
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
74 ## factorization
7132
b01db194c526 [project @ 2007-11-08 16:17:34 by jwe]
jwe
parents: 7125
diff changeset
75 [qq, rr, pp] = qr (H); # H = qq*rr*pp'
b01db194c526 [project @ 2007-11-08 16:17:34 by jwe]
jwe
parents: 7125
diff changeset
76 nn = rank (rr);
3431
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
77 qq = qq(:,1:nn);
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
78 rr = rr(1:nn,:); # rr may not be square, but "\" does least
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
79 xx = U*pp*(rr\qq'*(U'*z)); # squares solution, so this works
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
80 ## xx1 = pinv(F)*z;
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
81 ## zgscal_x_xx1_err = [xx,xx1,xx-xx1]
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
82 return;
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
83
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
84 ## the rest of this is left from the original zgscal;
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
85 ## I've had some numerical problems with the GCG algorithm,
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
86 ## so for now I'm solving it with the krylov routine.
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
87
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
88 ## initialize residual error norm
7132
b01db194c526 [project @ 2007-11-08 16:17:34 by jwe]
jwe
parents: 7125
diff changeset
89 rnorm = norm (rk1, 1);
3431
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
90
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
91 xnorm = 0;
7132
b01db194c526 [project @ 2007-11-08 16:17:34 by jwe]
jwe
parents: 7125
diff changeset
92 fnorm = 1e-12 * norm ([a, b; c, d], 1);
3431
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
93
7132
b01db194c526 [project @ 2007-11-08 16:17:34 by jwe]
jwe
parents: 7125
diff changeset
94 gamk2 = 0;
b01db194c526 [project @ 2007-11-08 16:17:34 by jwe]
jwe
parents: 7125
diff changeset
95 omega1 = 0;
b01db194c526 [project @ 2007-11-08 16:17:34 by jwe]
jwe
parents: 7125
diff changeset
96 ztmz2 = 0;
3431
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
97
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
98 ## do until small changes to x
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
99 len_x = length(x);
7132
b01db194c526 [project @ 2007-11-08 16:17:34 by jwe]
jwe
parents: 7125
diff changeset
100 while ((k < 2*len_x && xnorm > 0.5 && rnorm > fnorm) || k == 0)
b01db194c526 [project @ 2007-11-08 16:17:34 by jwe]
jwe
parents: 7125
diff changeset
101 k++;
3431
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
102
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
103 ## solve F_d z_{k-1} = r_{k-1}
7132
b01db194c526 [project @ 2007-11-08 16:17:34 by jwe]
jwe
parents: 7125
diff changeset
104 zk1= zgfslv (n, m, p, rk1);
3431
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
105
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
106 ## Generalized CG iteration
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
107 ## gamk1 = (zk1'*F_d*zk1)/(zk1'*F*zk1);
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
108 ztMz1 = zk1'*rk1;
7132
b01db194c526 [project @ 2007-11-08 16:17:34 by jwe]
jwe
parents: 7125
diff changeset
109 gamk1 = ztMz1/(zk1'*zgfmul (a, b, c, d, zk1));
3431
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
110
7132
b01db194c526 [project @ 2007-11-08 16:17:34 by jwe]
jwe
parents: 7125
diff changeset
111 if (rem (k, len_x) == 1)
b01db194c526 [project @ 2007-11-08 16:17:34 by jwe]
jwe
parents: 7125
diff changeset
112 omega = 1;
b01db194c526 [project @ 2007-11-08 16:17:34 by jwe]
jwe
parents: 7125
diff changeset
113 else
b01db194c526 [project @ 2007-11-08 16:17:34 by jwe]
jwe
parents: 7125
diff changeset
114 omega = 1/(1-gamk1*ztMz1/(gamk2*omega1*ztmz2));
3431
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
115 endif
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
116
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
117 ## store x in xk2 to save space
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
118 xk2 = xk2 + omega*(gamk1*zk1 + xk1 - xk2);
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
119
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
120 ## compute new residual error: rk = z - F xk, check end conditions
7132
b01db194c526 [project @ 2007-11-08 16:17:34 by jwe]
jwe
parents: 7125
diff changeset
121 rk1 = z - zgfmul (a, b, c, d, xk2);
b01db194c526 [project @ 2007-11-08 16:17:34 by jwe]
jwe
parents: 7125
diff changeset
122 rnorm = norm (rk1);
b01db194c526 [project @ 2007-11-08 16:17:34 by jwe]
jwe
parents: 7125
diff changeset
123 xnorm = max (abs (xk1 - xk2));
3431
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
124
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
125 ## printf("zgscal: k=%d, gamk1=%e, gamk2=%e, \nztMz1=%e ztmz2=%e\n", ...
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
126 ## k,gamk1, gamk2, ztMz1, ztmz2);
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
127 ## xk2_1_zk1 = [xk2 xk1 zk1]
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
128 ## ABCD = [a,b;c,d]
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
129 ## prompt
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
130
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
131 ## get ready for next iteration
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
132 gamk2 = gamk1;
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
133 omega1 = omega;
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
134 ztmz2 = ztMz1;
7132
b01db194c526 [project @ 2007-11-08 16:17:34 by jwe]
jwe
parents: 7125
diff changeset
135 [xk1, xk2] = swap (xk1, xk2);
3431
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
136 endwhile
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
137 x = xk2;
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
138
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
139 ## check convergence
7132
b01db194c526 [project @ 2007-11-08 16:17:34 by jwe]
jwe
parents: 7125
diff changeset
140 if (xnorm> 0.5 && rnorm > fnorm)
b01db194c526 [project @ 2007-11-08 16:17:34 by jwe]
jwe
parents: 7125
diff changeset
141 warning ("zgscal(tzero): GCG iteration failed; solving with pinv");
3431
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
142
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
143 ## perform brute force least squares; construct F
7132
b01db194c526 [project @ 2007-11-08 16:17:34 by jwe]
jwe
parents: 7125
diff changeset
144 Am = eye (nmp);
b01db194c526 [project @ 2007-11-08 16:17:34 by jwe]
jwe
parents: 7125
diff changeset
145 for ii = 1:nmp
b01db194c526 [project @ 2007-11-08 16:17:34 by jwe]
jwe
parents: 7125
diff changeset
146 Am(:,ii) = zgfmul (a, b, c, d, Am(:,ii));
3431
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
147 endfor
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
148
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
149 ## now solve with qr factorization
7132
b01db194c526 [project @ 2007-11-08 16:17:34 by jwe]
jwe
parents: 7125
diff changeset
150 x = pinv (Am) * z;
3431
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
151 endif
7132
b01db194c526 [project @ 2007-11-08 16:17:34 by jwe]
jwe
parents: 7125
diff changeset
152
3431
99ab64f4a09d [project @ 2000-01-14 03:53:03 by jwe]
jwe
parents:
diff changeset
153 endfunction