home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21eb.zip / octave / SCRIPTS.ZIP / scripts.fat / control / lqr.m < prev    next >
Text File  |  1999-04-29  |  3KB  |  110 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 [k, p, e] = lqr (a, b, q, r, s)
  16.  
  17. # Usage: [k, p, e] = lqr (A, B, Q, R {,S})
  18. #
  19. # Linear quadratic regulator design for the continuous time system
  20. #   dx/dt = A x + B u
  21. # to minimize the cost functional
  22. #
  23. #  J = int_0^\infty{ [x' u'] [Q  S'; S R] [x ; u] dt}
  24. #
  25. # inputs:
  26. #   A, B: coefficient matrices for continuous time system
  27. #   Q, R, S: cost functional coefficient matrices.
  28. #      Q: must be nonnegative definite.
  29. #      R: must be positive definite
  30. #      S: defaults to 0
  31. # if S is omitted, the optimization simplifies to the usual
  32. #
  33. #  J = int_0^\infty{ x' Q x + u' R u }
  34. #
  35. # Returns:
  36. #
  37. #   k = state feedback gain, (A - B K) is stable and minimizes the
  38. #       cost functional
  39. #   p = solution of algebraic Riccati equation
  40. #   e = closed loop poles of (A - B K)
  41. #
  42. # reference: Anderson and Moore, OPTIMAL CONTROL: LINEAR QUADRATIC METHODS,
  43. # Prentice-Hall, 1990, pp. 56-58
  44.  
  45.  
  46. # Written by A. S. Hodel (scotte@eng.auburn.edu) August 1993.
  47.  
  48.   # disp("lqr: entry");
  49.  
  50.   if ((nargin != 4) && (nargin != 5))
  51.     error ("lqr: invalid number of arguments");
  52.   endif
  53.  
  54. # Check a.
  55.   if ((n = is_sqr (a)) == 0)
  56.     error ("lqr: requires 1st parameter(a) to be square");
  57.   endif
  58.  
  59. # Check b.
  60.   [n1, m] = size (b);
  61.   if (n1 != n)
  62.     error ("lqr: a,b not conformal");
  63.   endif
  64.  
  65. # Check q.
  66.   
  67.   if ( ((n1 = is_sqr (q)) == 0) || (n1 != n))
  68.     error ("lqr: q must be square and conformal with a");
  69.   endif
  70.  
  71. # Check r.
  72.   if ( ((m1 = is_sqr(r)) == 0) || (m1 != m))
  73.     error ("lqr: r must be square and conformal with column dimension of b");
  74.   endif
  75.  
  76. # Check if n is there.
  77.   if (nargin == 5)
  78.     [n1, m1] = size (s);
  79.     if ( (n1 != n) || (m1 != m))
  80.       error ("lqr: z must be identically dimensioned with b");
  81.     endif
  82.  
  83. # Incorporate cross term into a and q.
  84.     ao = a - (b/r)*s';
  85.     qo = q - (s/r)*s';
  86.   else
  87.     s = zeros (n, m);
  88.     ao = a;
  89.     qo = q;
  90.   endif
  91.  
  92. # Check that q, (r) are symmetric, positive (semi)definite
  93.  
  94.   if (is_symm (q) && is_symm (r) ...
  95.       && all (eig (q) >= 0) && all (eig (r) > 0))
  96.     p = are (ao, (b/r)*b', qo);
  97.     k = r\(b'*p + s');
  98.     e = eig (a - b*k);
  99.   else
  100.     error ("lqr: q (r) must be symmetric positive (semi) definite");
  101.   endif
  102.  
  103.   # disp("lqr: exit");
  104. endfunction
  105.