% statex.m - state-space example (done in class)
% 332:345 - Linear Systems & Signals - Fall 2009

clear all;

syms t u s real
syms y0 y1 real                      % notation: y0 = y(0-) and y1 = dy/dt(0-)

f = exp(-u)                          % input

num = [2 13 8]; den = [1 5 6];       % system

[A,B,C,D] = tf2ss(num,den)           % convert to state-space

PHI = inv(s*eye(2) - A)              % Laplace transform of state-transition matrix
phi = expm(A*t)                      % state transition matrix

h = C*expm(A*t)*B                    % impulse response h(t), must add to it the D*delta(t) term
F = sym([C; C*A])                    % observability matrix for an order-2 system
x0= inv(F)*[y0;y1]                   % map initial conditions [y0; y1] to initial state vector x(0-)

xf = simplify(int(expm(A*(t-u))*B*f, u, 0,t))       % forced part of the state
xh = simplify(expm(A*t)*x0)                         % homogeneous part due to initial state
x  = simplify(xh+xf)                                % full state vector

y  = simplify(C*x + D*subs(f,u,t))                          % output obtained from state eq
yf = simplify(int(subs(h,t,t-u)*f,u,0,t) + D*subs(f,u,t))   % forced output via convolution with h(t)
yh = simplify(C*expm(A*t)*x0)                               % homogeneous output

simplify(y-yh-yf)                                           % test equality of y and yh+yf

% -------- diagonal realization ------------

[r,p,k] = residue(num,den)                                  % verify the PFE coefficients
A = diag([-3,-2]); B = [1;1]; C = [13,-10]; D = 2;          % state-space parameters
phi = expm(A*t)                                             % diagonal transition matrix
h1 = C*phi*B;                                               % impulse response
simplify(h-h1)                                              % should be the same as above
F = sym([C; C*A]);                                          % observability matrix
invF = inv(F)
x0= inv(F)*[y0;y1]                                          % map initial conditions to x(0)
xf = simplify(int(expm(A*(t-u))*B*f, u, 0,t))               % forced part of the state
xh = simplify(expm(A*t)*x0)                                 % homogeneous part due to initial state
x = simplify(xh+xf)                                         % full state vector
y1 = simplify(C*x + D*subs(f,u,t))                          % output y(t)
yf = simplify(int(subs(h,t,t-u)*f,u,0,t) + D*subs(f,u,t))   % forced output
yh = simplify(C*expm(A*t)*x0)                               % homogeneous output

simplify(y-y1)                                              % verify y(t) is the same
 
f =
 
                                    exp(-u)

A =

    -5    -6
     1     0


B =

     1
     0


C =

     3    -4


D =

     2

 
PHI =
 
                       [     s                 6      ]
                       [------------    - ------------]
                       [ 2                 2          ]
                       [s  + 5 s + 6      s  + 5 s + 6]
                       [                              ]
                       [     1              s + 5     ]
                       [------------     ------------ ]
                       [ 2                2           ]
                       [s  + 5 s + 6     s  + 5 s + 6 ]
 
phi =
 
           [-2 exp(-2 t) + 3 exp(-3 t)    6 exp(-3 t) - 6 exp(-2 t)]
           [                                                       ]
           [  -exp(-3 t) + exp(-2 t)      3 exp(-2 t) - 2 exp(-3 t)]
 
h =
 
                         -10 exp(-2 t) + 13 exp(-3 t)
 
F =
 
                                 [  3     -4]
                                 [          ]
                                 [-19    -18]
 
x0 =
 
                             [ 9/65 y0 - 2/65 y1 ]
                             [                   ]
                             [  19               ]
                             [- --- y0 - 3/130 y1]
                             [  130              ]
 
xf =
 
                 [- 1/2 (-4 exp(t) + 3 + exp(2 t)) exp(-3 t)]
                 [                                          ]
                 [ 1/2 (1 - 2 exp(t) + exp(2 t)) exp(-3 t)  ]
 
xh =
 
        [3/5 exp(-2 t) y0 + 1/5 exp(-2 t) y1 - 6/13 exp(-3 t) y0

         - 3/13 exp(-3 t) y1]

        [2/13 exp(-3 t) y0 + 1/13 exp(-3 t) y1 - 3/10 exp(-2 t) y0

         - 1/10 exp(-2 t) y1]
 
x =
 
        [3/5 exp(-2 t) y0 + 1/5 exp(-2 t) y1 - 6/13 exp(-3 t) y0

         - 3/13 exp(-3 t) y1 + 2 exp(-2 t) - 3/2 exp(-3 t) - 1/2 exp(-t)]

        [2/13 exp(-3 t) y0 + 1/13 exp(-3 t) y1 - 3/10 exp(-2 t) y0

         - 1/10 exp(-2 t) y1 + 1/2 exp(-3 t) - exp(-2 t) + 1/2 exp(-t)]
 
y =
 
  3 exp(-2 t) y0 + exp(-2 t) y1 - 2 exp(-3 t) y0 - exp(-3 t) y1 + 10 exp(-2 t)

         - 13/2 exp(-3 t) - 3/2 exp(-t)
 
yf =
 
                  10 exp(-2 t) - 13/2 exp(-3 t) - 3/2 exp(-t)
 
yh =
 
         3 exp(-2 t) y0 - 2 exp(-3 t) y0 + exp(-2 t) y1 - exp(-3 t) y1
 
ans =
 
                                       0

r =

   13.0000
  -10.0000


p =

   -3.0000
   -2.0000


k =

     2

 
phi =
 
                           [exp(-3 t)        0    ]
                           [                      ]
                           [    0        exp(-2 t)]
 
ans =
 
                                       0
 
invF =
 
                                  [-2    -1]
                                  [--    --]
                                  [13    13]
                                  [        ]
                                  [-3    -1]
                                  [--    --]
                                  [10    10]
 
x0 =
 
                             [- 2/13 y0 - 1/13 y1]
                             [                   ]
                             [- 3/10 y0 - 1/10 y1]
 
xf =
 
                        [- 1/2 exp(-3 t) + 1/2 exp(-t)]
                        [                             ]
                        [    -exp(-2 t) + exp(-t)     ]
 
xh =
 
                        [- 1/13 exp(-3 t) (2 y0 + y1)]
                        [                            ]
                        [- 1/10 exp(-2 t) (3 y0 + y1)]
 
x =
 
        [- 2/13 exp(-3 t) y0 - 1/13 exp(-3 t) y1 - 1/2 exp(-3 t) + 1/2 exp(-t)

        ]

        [- 3/10 exp(-2 t) y0 - 1/10 exp(-2 t) y1 - exp(-2 t) + exp(-t)]
 
y1 =
 
  3 exp(-2 t) y0 + exp(-2 t) y1 - 2 exp(-3 t) y0 - exp(-3 t) y1 + 10 exp(-2 t)

         - 13/2 exp(-3 t) - 3/2 exp(-t)
 
yf =
 
                  10 exp(-2 t) - 13/2 exp(-3 t) - 3/2 exp(-t)
 
yh =
 
         3 exp(-2 t) y0 - 2 exp(-3 t) y0 + exp(-2 t) y1 - exp(-3 t) y1
 
ans =
 
                                       0