home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 5 / DATAFILE_PDCD5.iso / utilities / r / rlab / CTB / lqr < prev    next >
Encoding:
Text File  |  1995-11-15  |  4.7 KB  |  174 lines

  1. //----------------------------------------------------------------------------
  2. //
  3. // lqr
  4. //
  5. // Syntax: G=lqr(A,B,Q,R,CT)
  6. //
  7. // This routine computes the Linear Quadratic Deisgn for Continuous Systems.
  8. // It calculates the optimal feedback gain matrix K such that the feedback
  9. // law u = -Kx  minimizes the following cost function:
  10. //
  11. //           Inf
  12. //     J = Integral (x'Qx + u'Ru) dt
  13. //            0
  14. //
  15. // subject to the constraint
  16. //   .
  17. //   x = Ax + Bu
  18. //
  19. // The solution P solves the following equation:
  20. //
  21. //                 -1
  22. //   A'P + PA - PBR  B'P + Q = 0
  23. //
  24. // The routine returns the gains (G.k), the steady-solution to the Algebraic
  25. // Riccatti Eqn. (G.p) and the eigenvalues of the closed loop system
  26. // eig(A-B*K) (G.e).
  27. //
  28. // When the term CT is included, a Cross-Term is added that relates u to x
  29. // in the following cost function:
  30. //
  31. //           Inf
  32. //     J = Integral (x'Qx + u'Ru + 2*x'CTu) dt
  33. //            0
  34. //
  35. // Note: Three matrices are returned in a list.
  36. //
  37. //       G.k = Optimal Feedback Gain matrix
  38. //       G.p = Steady-State Solution to the Algebraic Riccatti Eqn.
  39. //       G.e = Closed Loop eigenvalues.
  40. //
  41. // Ref: (1) Kailath, "Linear Systems," Prentice-Hall, 1980
  42. //      (2) Laub, A. J., "A Schur Method for Solving Algebraic Riccati
  43. //          Equations," IEEE Trans. AC-24, 1979, pp. 913-921.
  44. //      (3) Junkins, J., and Kim, Y., "Introduction to Dynamics and Control
  45. //          of Flexible Structures," AIAA Inc, Washington D.C., 1993.
  46. //
  47. // Copyright (C), by Jeffrey B. Layton, 1994
  48. // Version JBL 940925
  49. //----------------------------------------------------------------------------
  50.  
  51. rfile abcdchk
  52. rfile ric
  53.  
  54. static (lqr_compute)    // Hide this function
  55.  
  56. lqr = function(a,b,q,r,ct)
  57. {
  58.    local (msg,estr)
  59.  
  60. // Count number of input arguments
  61.    nargs=0;
  62.    if (exist(a)) {nargs=nargs+1;}
  63.    if (exist(b)) {nargs=nargs+1;}
  64.    if (exist(q)) {nargs=nargs+1;}
  65.    if (exist(r)) {nargs=nargs+1;}
  66.    if (exist(ct)) {nargs=nargs+1;}
  67.  
  68.    if (nargs < 4 ) {
  69.        error("LQR: Wrong number of input arguments.");
  70.    }
  71.  
  72. // Check if Q and R are consistent.
  73.    if ( a.nr != q.nr || a.nc != q.nc ) {
  74.        error("LQR: A and Q must be the same size");
  75.    }
  76.    
  77.    if ( r.nr != r.nc || b.nc != r.nr ) {
  78.        error("LQR: B and R must be consistent");
  79.    }
  80.    
  81. // Check if A and B are empty
  82.    if ( (!length(a)) || (!length(b)) ) {
  83.        error("LQR: A and B matrices cannot be empty.");
  84.    }
  85.  
  86. // A has to be square
  87.    if (a.nr != a.nc) {
  88.        error("LQR: A has to be square.");
  89.    }
  90.  
  91. // See if A and B are compatible.
  92.    msg="";
  93.    msg=abcdchk(a,b);
  94.    if (msg != "") {
  95.        estr="LQR: "+msg;
  96.        error(estr);
  97.    }
  98.  
  99. // Check if Q is positive semi-definite and symmetric
  100.    if (!issymm(q)) {
  101.        printf("%s","LQR: Warning: Q is not symmetric.\n");
  102.    else
  103.        if (any(eig(q).val < -epsilon()*norm(q,"1")) ) {    
  104.            printf("%s","LQR: Warning: Q is not positive semi-definite.\n");
  105.        }
  106.    }
  107.  
  108. // Check if R is positive definite and symmetric
  109.    if (!issymm(r)) {
  110.        printf("%s","LQR: Warning: R is not symmetric.\n");
  111.    else
  112.        if (any(eig(r).val < -epsilon()*norm(r,"1")) ) {
  113.            printf("%s","LQR: Warning: R is not positive semi-definite.\n");
  114.        }
  115.    }
  116.  
  117. //
  118. // Call lqr_compute with revised arguments. This prevents us from needlessly
  119. // making a copy if ct does not exist, and also prevents us from overwriting
  120. // a and q if ct exists.
  121. //
  122.  
  123. // If Cross-Term exists, modify Q and A.
  124.    if (exist(ct)) {
  125.        if ( ct.nr != a.nr || ct.nc != r.nc) {
  126.             error("LQR: CT must be consistent with Q and R");
  127.        }
  128.        return lqr_compute (a-solve(r',b')'*ct', b, ...
  129.                            q-solve(r',ct')'*ct', r, ct);
  130.    else
  131.        return lqr_compute (a, b, q, r, zeros (a.nr, b.nc));
  132.    }
  133. };
  134.  
  135. //----------------------------------------------------------------------------
  136. //
  137. // This is where the computation is performed. Note that lqr_compute is a
  138. // static variable and is never seen from the global workspace.
  139. //
  140.  
  141. lqr_compute = function (a, b, q, r, ct)
  142. {
  143.    local (Phi12, Phi22, d, e, index, k, p, v)
  144.  
  145. // Start solution by finding the spectral decomposition and eigenvectors
  146. // of Hamiltonian.
  147.     
  148.    e=eig( [ a, solve(r',b')'*b'; q, -a' ] );
  149.    v=e.vec;
  150.    d=e.val;
  151.    
  152. // Sort eigenvectors by sorting eigenvalues (and storing the index).
  153.    index=sort( real( d ) ).ind;
  154.    d=real( d[ index ] );
  155.    
  156.    if ( !( d[a.nc] < 0 && d[a.nc+1] > 0 ) ) {
  157.        error("lqr: Can't order eigenvalues");
  158.    }
  159.    
  160. // Form the Partitions of the PHI matrix and solve for P and then the
  161. // Control Gains.
  162.    Phi12=v[ 1:a.nc; index[1:a.nc] ];
  163.    Phi22=v[ (a.nc+1):(2*a.nc); index[1:a.nc] ];
  164.    p=-real(solve(Phi12',Phi22')');
  165.  
  166. // Compute Control Gains
  167.    k=solve( r, ct'+b'*p );
  168.  
  169. // Compute eigenvalues
  170.    e=d;
  171.    
  172.    return << k=k; p=p; e=e >>;
  173. };
  174.