home *** CD-ROM | disk | FTP | other *** search
- //----------------------------------------------------------------------------
- //
- // ric
- //
- // Syntax: P=ric(A,B,Q,R)
- //
- // This routine solves the Algebriac Riccati Equation (ARE) for
- // Continuous Systems. The ARE is,
- //
- // -1
- // A'P + PA - PBR B'P + Q = 0
- //
- //
- // Ref: (1) Kailath, "Linear Systems," Prentice-Hall, 1980
- // (2) Laub, A. J., "A Schur Method for Solving Algebraic Riccati
- // Equations," IEEE Trans. AC-24, 1979, pp. 913-921.
- // (3) Junkins, J., and Kim, Y., "Introduction to Dynamics and Control
- // of Flexible Structures," AIAA Inc, Washington D.C., 1993.
- //
- // Copyright (C), by Jeffrey B. Layton, 1994
- // Version JBL 940903
- //----------------------------------------------------------------------------
-
- rfile abcdchk
-
- static (ric_compute) // Hide this function
-
- ric = function(a,b,q,r)
- {
- local (nargs,msg,estr)
-
- // Count number of input arguments
- nargs=0;
- if (exist(a)) {nargs=nargs+1;}
- if (exist(b)) {nargs=nargs+1;}
- if (exist(q)) {nargs=nargs+1;}
- if (exist(r)) {nargs=nargs+1;}
-
- if (nargs != 4) {
- error("RIC: Wrong number of input arguments.");
- }
-
- // Check if Q and R are consistent.
- if ( a.nr != q.nr || a.nc != q.nc ) {
- error("RIC: A and Q must be the same size");
- }
-
- if ( r.nr != r.nc || b.nc != r.nr ) {
- error("RIC: B and R must be consistent");
- }
-
- // Check if A and B are empty
- if ( (!length(a)) || (!length(b)) ) {
- error("RIC: A and B matrices cannot be empty.");
- }
-
- // A has to be square
- if (a.nr != a.nc) {
- error("RIC: A has to be square.");
- }
-
- // See if A and B are compatible.
- msg="";
- msg=abcdchk(a,b);
- if (msg != "") {
- estr="RIC: "+msg;
- error(estr);
- }
-
- // Check if Q is positive semi-definite and symmetric
- if (!issymm(q)) {
- printf("%s","RIC: Warning: Q is not symmetric.\n");
- else
- if (any(eig(q).val < -epsilon()*norm(q,"1")) ) {
- printf("%s","RIC: Warning: Q is not positive semi-definite.\n");
- }
- }
-
- // Check if R is positive definite and symmetric
- if (!issymm(r)) {
- printf("%s","RIC: Warning: R is not symmetric.\n");
- else
- if (any(eig(r).val < -epsilon()*norm(r,"1")) ) {
- printf("%s","RIC: Warning: R is not positive semi-definite.\n");
- }
- }
-
- //
- // Call ric_compute to solve Riccatti.
- //
- return ric_compute (a, b, q, r);
- };
-
- //----------------------------------------------------------------------------
- //
- // This is where the computation is performed. Note that ric_compute is a
- // static variable and is never seen from the global workspace.
- //
-
- ric_compute = function (a, b, q, r)
- {
- local (Phi12, Phi22, d, e, index, p, v)
-
- // Start solution by finding the spectral decomposition and eigenvectors
- // of Hamiltonian.
-
- e=eig( [ a, solve(r',b')'*b'; q, -a' ] );
- v=e.vec;
- d=e.val;
-
- // Sort eigenvectors by sorting eigenvalues (and storing the index).
- index=sort( real( d ) ).ind;
- d=real( d[ index ] );
-
- if ( !( d[a.nc] < 0 && d[a.nc+1] > 0 ) ) {
- error("ric: Can't order eigenvalues");
- }
-
- // Form the Partitions of the PHI matrix and solve for P and then the
- // Control Gains.
- Phi12=v[ 1:a.nc; index[1:a.nc] ];
- Phi22=v[ (a.nc+1):(2*a.nc); index[1:a.nc] ];
- p=-real(solve(Phi12',Phi22')');
-
- return p;
- };
-