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