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

  1. # Copyright (C) 1996, 1997 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 [K,Q1,P1,Ee,Er] = lqg(sys,Sigw,Sigv,Q,R,input_list)
  18. #
  19. # function [K,Q,P,Ee,Er] = lqg(sys,Sigw,Sigv,Q,R,input_list)
  20. # design a linear-quadratic-gaussian optimal controller for the system
  21. #
  22. #  dx/dt = A x + B u + G w       [w]=N(0,[Sigw 0    ])
  23. #      y = C x + v               [v]  (    0   Sigv ])
  24. #
  25. # or
  26. #  x(k+1) = A x(k) + B u(k) + G w(k)       [w]=N(0,[Sigw 0    ])
  27. #    y(k) = C x(k) + v(k)                  [v]  (    0   Sigv ])
  28. #
  29. # inputs:
  30. #  sys: system data structure
  31. #  Sigw, Sigv: intensities of independent Gaussian noise processes (as above)
  32. #  Q, R: state, control weighting respectively.  Control ARE is
  33. #  input_list: indices of controlled inputs
  34. #     default: last dim(R) inputs are assumed to be controlled inputs, all
  35. #              others are assumed to be noise inputs.
  36. # Outputs:
  37. #    K: system data structure LQG optimal controller
  38. #       (Obtain A,B,C matrices with sys2ss, sys2tf, or sys2zp as appropriate)
  39. #    P: Solution of control (state feedback) algebraic Riccati equation
  40. #    Q: Solution of estimation algebraic Riccati equation
  41. #    Ee: estimator poles
  42. #    Es: controller poles
  43. #
  44. # See also: h2syn, lqe, lqr
  45.  
  46. # Written by A. S. Hodel August 1995; revised for new system format
  47. # August 1996
  48.  
  49. sav_val = implicit_str_to_num_ok;
  50. implicit_str_to_num_ok = 1;
  51.  
  52. if ( (nargin < 5) | (nargin > 6))
  53.   usage("[K,Q1,P1,Ee,Er] = lqg(sys,Sigw, Sigv,Q,R{,input_list})");
  54.  
  55. elseif(!is_struct(sys) )
  56.   error("sys must be in system data structure");
  57. endif
  58.  
  59. DIG = is_digit(sys);
  60. [A,B,C,D,tsam,n,nz,stname,inname,outname] = sys2ss(sys);
  61. [n,nz,nin,nout] = sysdimen(sys);
  62. if(nargin == 5)
  63.   #construct default input_list
  64.   input_list = (columns(Sigw)+1):nin;
  65. endif
  66.  
  67. if( !(n+nz) )
  68.     error(["lqg: 0 states in system"]);
  69.  
  70. elseif(nin != columns(Sigw)+ columns(R))
  71.   error(["lqg: sys has ",num2str(nin)," inputs, dim(Sigw)=", ...
  72.     num2str(columns(Sigw)),", dim(u)=",num2str(columns(R))])
  73.  
  74. elseif(nout != columns(Sigv))
  75.   error(["lqg: sys has ",num2str(nout)," outputs, dim(Sigv)=", ...
  76.     num2str(columns(Sigv)),")"])
  77. elseif(length(input_list) != columns(R))
  78.   error(["lqg: length(input_list)=",num2str(length(input_list)), ...
  79.     ", columns(R)=", num2str(columns(R))]);
  80. endif
  81.  
  82. varname = list("Sigw","Sigv","Q","R");
  83. for kk=1:length(varname);
  84.   eval(sprintf("chk = is_sqr(%s);",nth(varname,kk)));
  85.   if(! chk ) error("lqg: %s is not square",nth(varname,kk)); endif
  86. endfor
  87.  
  88. # permute (if need be)
  89. if(nargin == 6)
  90.   all_inputs = sysreord(nin,input_list);
  91.   B = B(:,all_inputs);
  92.   inname = inname(all_inputs);
  93. endif
  94.  
  95. # put parameters into correct variables
  96. m1 = columns(Sigw);
  97. m2 = m1+1;
  98. G = B(:,1:m1);
  99. B = B(:,m2:nin);
  100.  
  101. # now we can just do the design; call dlqr and dlqe, since all matrices
  102. # are not given in Cholesky factor form (as in h2syn case)
  103. if(DIG)
  104.   [Ks, P1, Er] = dlqr(A,B,Q,R);
  105.   [Ke, Q1, jnk, Ee] = dlqe(A,G,C,Sigw,Sigv);
  106. else
  107.   [Ks, P1, Er] = lqr(A,B,Q,R);
  108.   [Ke, Q1, Ee] = lqe(A,G,C,Sigw,Sigv);
  109. endif
  110. Ac = A - Ke*C - B*Ks;
  111. Bc = Ke;
  112. Cc = -Ks;
  113. Dc = zeros(rows(Cc),columns(Bc));
  114.  
  115. # fix state names
  116. stname1 = strappen(stname,"_e");
  117.  
  118. # fix controller output names
  119. outname1 = strappen(inname(m2:nin),"_K");
  120.  
  121. # fix controller input names
  122. inname1 = strappen(outname,"_K");
  123.  
  124. if(DIG)
  125.   K = ss2sys(Ac,Bc,Cc,Dc,tsam,n,nz,stname1,inname1,outname1,1:rows(Cc));
  126. else
  127.   K = ss2sys(Ac,Bc,Cc,Dc,tsam,n,nz,stname,inname1,outname1);
  128. endif
  129.  
  130. implicit_str_to_num_ok = sav_val;
  131. endfunction
  132.