6.245: Multivariable Control Systems

    MATLAB Functions of Interest, Simulating White Noise, and Using Scripts

--------------------------------------------------------------------------

The following MATLAB function help text may be useful to you in solving
LQR problems, Algebraic Riccati Equations, and Lyapunov Equations.
Note that there are two choices for LQR.  We recommend (the slower)
LQR2 (which calls ARE), since it uses a better algorithm (numerically)
and thus may cause you less grief.

As a side note: use the SUBPLOT command so that you can print
several plots on one page, as is reasonable.  Let us kill as few
trees as possible in this course.

 LQR    Linear quadratic regulator design for continuous systems.
        [K,S,E] = LQR(A,B,Q,R)  calculates the optimal feedback gain 
        matrix K such that the feedback law  u = -Kx  minimizes the cost
        function:
                J = Integral {x'Qx + u'Ru} dt
 
        subject to the constraint equation: 
                .
                x = Ax + Bu 
                 
        Also returned is S, the steady-state solution to the associated 
        algebraic Riccati equation and the closed loop eigenvalues E:
                                  -1
                0 = SA + A'S - SBR  B'S + Q     E = EIG(A-B*K)
 
        [K,S,E] = LQR(A,B,Q,R,N) includes the cross-term N that relates
        u to x in the cost function.
 
                J = Integral {x'Qx + u'Ru + 2*x'Nu}
 
        The controller can be formed with REG.
 
        See also: LQRY, LQR2, and REG.


 LQR2   Linear-quadratic regulator design for continuous-time systems.
        [K,S] = LQR2(A,B,Q,R)  calculates the optimal feedback gain matrix
        K such that the feedback law  u = -Kx  minimizes the cost function
 
              J = Integral {x'Qx + u'Ru} dt
                                              .
        subject to the constraint equation:   x = Ax + Bu 
        
        Also returned is S, the steady-state solution to the associated 
        algebraic Riccati equation:
                                  -1
                0 = SA + A'S - SBR  B'S + Q
 
        [K,S] = LQR2(A,B,Q,R,N) includes the cross-term 2x'Nu that 
        relates u to x in the cost functional. 
 
        The controller can be formed with REG.
 
        LQR2 uses the SCHUR algorithm of [1] and is more numerically
        reliable than LQR, which uses eigenvector decomposition.
 
        See also: ARE, LQR, and LQE2.

 ARE    Algebraic Riccati Equation solution.
        X = ARE(A, B, C) returns the stablizing solution (if it
        exists) to the continuous-time Riccati equation:
 
                A'*X + X*A - X*B*X + C = 0
 
        assuming B is symmetric and nonnegative definite and C is
        symmetric.
 
        See also: RIC.

 LYAP   Lyapunov equation.
        X = LYAP(A,C) solves the special form of the Lyapunov matrix 
        equation:
 
                A*X + X*A' = -C
 
        X = LYAP(A,B,C) solves the general form of the Lyapunov matrix
        equation:
 
                A*X + X*B = -C
 
        See also DLYAP.

--------------------------------------------------------------------------

Occasionally you will be called upon to simulate systems with white noise
inputs.  The MATLAB function RANDN generates discrete white gaussian noise,
with zero mean and unit variance.  Since white noise has infinite variance,
you should multiply the discrete white noise by sqrt(1/dt), where dt
is your simulation integration step size.  For example, to generate
white noise for a simulation of 20 seconds with an integration step size
of 0.1 second, try:

randn('seed',42); % initialize the random number generator
dt = 0.1;         % stepsize
t = [0:dt:20]';   % column vector
white_noise = sqrt(1/dt) * randn(size(t));

Notice that the random number seed was set explicitly!  This ensures
that we get the "same" white noise every time that we execute the
simulation.  Additional information on RANDN is given below.  Also,
note that RAND (for uniform samples) and RANDN (for Gaussian samples)
are different generators in MATLAB, and have separate random number seeds.

 RANDN  Normally distributed random numbers and matrices.
        RANDN(N) is an N-by-N matrix with random entries, chosen
        from a normal distribution with mean 0.0 and variance 1.0.
        RANDN(M,N) or RANDN([M,N]) is an M-by-N matrix with random entries.
        RANDN(SIZE(A)) is the same size as A.
        RANDN with no arguments is a scalar whose value changes each time
        it is referenced.
 
        RANDN('seed') returns the current seed of the normal generator.
        RANDN('seed',s) sets the normal generator seed to s.
        RANDN('seed',0) resets the seed its startup value.
        RANDN('seed',sum(100*clock)) sets it to a different value each time.
 
        RANDN and RAND have separate generators, each with its own seed.
 
        See also RAND, SPRANDN.

--------------------------------------------------------------------------

When it comes time to simulate and document your work, you may be
interested in functions like these (to use from the MATLAB command
line and/or from your m-file scripts):

 LSIM   Simulation of continuous-time linear systems to arbitrary inputs.
        LSIM(A,B,C,D,U,T) plots the time response of the linear system:
                        .
                        x = Ax + Bu
                        y = Cx + Du
        to the input time history U. Matrix U must have as many columns as
        there are inputs, U.  Each row of U corresponds to a new time 
        point, and U must have LENGTH(T) rows.  The time vector T must be
        regularly spaced.  LSIM(A,B,C,D,U,T,X0) can be used if initial 
        conditions exist.
 
        LSIM(NUM,DEN,U,T) plots the time response of the polynomial 
        transfer function  G(s) = NUM(s)/DEN(s)  where NUM and DEN contain
        the polynomial coefficients in descending powers of s.  When 
        invoked with left hand arguments,
                [Y,X] = LSIM(A,B,C,D,U,T)
                [Y,X] = LSIM(NUM,DEN,U,T)
        returns the output and state time history in the matrices Y and X.
        No plot is drawn on the screen.  Y has as many columns as there 
        are outputs, y, and with LENGTH(T) rows.  X has as many columns 
        as there are states.
 
        See also: STEP,IMPULSE,INITIAL and DLSIM.

 DIARY  Save the text of a MATLAB session.
        DIARY file_name  causes a copy of all subsequent terminal input
        and most of the resulting output to be written on the named
        file. DIARY OFF suspends it. DIARY ON turns it back on.
        DIARY, by itself, toggles the diary state.

For example, in a script file you might say:
  diary p1_4.txt
   ... (matlab commands that output comments, values, etc.)
  diary off

I include the following function, DISP, because you can do spiffy
things in an m-file script by using commands like:
  disp(sprintf('iteration %d: rho is %f, while my gain margin is
%f',iter,rho,gm));

 DISP   Displays a matrix as text.
        DISP(X) displays the matrix, without printing the matrix name.
        If X contains text, the text is displayed.