home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21eb.zip / octave / SCRIPTS.ZIP / scripts.fat / control / dlyap.m < prev    next >
Text File  |  1999-04-29  |  2KB  |  102 lines

  1. # Copyright (C) 1993, 1994, 1995 John W. Eaton
  2. # This file is part of Octave.
  3. # Octave is free software; you can redistribute it and/or modify it
  4. # under the terms of the GNU General Public License as published by the
  5. # Free Software Foundation; either version 2, or (at your option) any
  6. # later version.
  7. # Octave is distributed in the hope that it will be useful, but WITHOUT
  8. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  9. # FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  10. # for more details.
  11. # You should have received a copy of the GNU General Public License
  12. # along with Octave; see the file COPYING.  If not, write to the Free
  13. # Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  14.  
  15. function x = dlyap (a, b)
  16.  
  17. # Usage: x = dlyap (a, b)
  18. #
  19. # Solve a x a' - x + b = 0 (discrete Lyapunov equation) for square
  20. # matrices a and b.  If b is not square, then the function attempts 
  21. # to solve either
  22. #
  23. #  a x a' - x + b b' = 0
  24. #
  25. # or
  26. #
  27. #  a' x a - x + b' b = 0
  28. #
  29. # whichever is appropriate.  Uses Schur decomposition as in Kitagawa
  30. # (1977).
  31.  
  32. # Written by A. S. Hodel (scotte@eng.auburn.edu) August 1993.
  33.  
  34.   if ((n = is_sqr (a)) == 0)
  35.     warning ("dlyap: a must be square");
  36.   endif
  37.  
  38.   if ((m = is_sqr (b)) == 0)
  39.     [n1, m] = size (b);
  40.     if (n1 == n)
  41.       b = b*b';
  42.       m = n1;
  43.     else
  44.       b = b'*b;
  45.       a = a';
  46.     endif
  47.   endif
  48.  
  49.   if (n != m)
  50.     warning ("dlyap: a,b not conformably dimensioned");
  51.   endif
  52.  
  53.   # Solve the equation column by column.
  54.  
  55.   [u, s] = schur (a);
  56.   b = u'*b*u;
  57.  
  58.   j = n;
  59.   while (j > 0)
  60.     j1 = j;
  61.  
  62. # Check for Schur block.
  63.  
  64.     if (j == 1)
  65.       blksiz = 1;
  66.     elseif (s (j, j-1) != 0)
  67.       blksiz = 2;
  68.       j = j - 1;
  69.     else
  70.       blksiz = 1;
  71.     endif
  72.  
  73.     Ajj = kron (s (j:j1, j:j1), s) - eye (blksiz*n);
  74.  
  75.     rhs = reshape (b (:, j:j1), blksiz*n, 1);
  76.  
  77.     if (j1 < n)
  78.       rhs2 = s*(x (:, (j1+1):n) * s (j:j1, (j1+1):n)');
  79.       rhs = rhs + reshape (rhs2, blksiz*n, 1);
  80.     endif
  81.  
  82.     v = - Ajj\rhs;
  83.     x (:, j) = v (1:n);
  84.  
  85.     if(blksiz == 2)
  86.       x (:, j1) = v ((n+1):blksiz*n);
  87.     endif
  88.  
  89.     j = j - 1;
  90.  
  91.   endwhile
  92.  
  93. # Back-transform to original coordinates.
  94.  
  95.   x = u*x*u';
  96.  
  97. endfunction
  98.