home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21eb.zip / octave / SCRIPTS.ZIP / scripts / control / zgscal.m < prev    next >
Text File  |  1999-03-05  |  4KB  |  143 lines

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