You are on page 1of 9

APPENDIX C

CONTINUING MATLAB
EXAMPLE M-FILE

Chapter 1 introduced the Continuing MATLAB Example, based on the


single-input, single-output rotational mechanical system of Figure 1.9. The
input is torque τ (t), and the output is the angular displacement θ (t). This
example was revisited each chapter, illustrating the important topics in
the context of one familiar example for which most computations can be
done by hand to check the MATLAB results. MATLAB code segments
were presented in each chapter to demonstrate important calculations; in
general, each chapter’s code did not stand alone but required MATLAB
code from previous chapters to execute properly.
This appendix presents the entire m-file for the Continuing MATLAB
Example, from Chapter 1 through Chapter 9. No new code is given; rather,
the complete code is listed here for the convenience of the student.

%------------------------------------------------------
% Continuing MATLAB Example m-file
% Chapter 1 through Chapter 9
% Dr. Bob Williams

%------------------------------------------------------
% Chapter 1. State-Space Description
%------------------------------------------------------

J = 1;
b = 4;
kR = 40;

Linear State-Space Control Systems. Robert L. Williams II and Douglas A. Lawrence


Copyright  2007 John Wiley & Sons, Inc. ISBN: 978-0-471-73555-7
447
448 CONTINUING MATLAB EXAMPLE M-FILE

A = [0 1;-kR/J -b/J]; % Define the state-space


% realization
B = [0;1/J];
C = [1 0];
D = [0];

JbkR = ss(A,B,C,D); % Define model from state-space

JbkRtf = tf(JbkR); % Convert to transfer function


JbkRzpk = zpk(JbkR); % Convert to zero-pole
% description

[num,den] = tfdata(JbkR,'v');
% Extract transfer function
% description
[z,p,k] = zpkdata(JbkR,'v');
% Extract zero-pole description

JbkRss = ss(JbkRtf) % Convert to state-space


% description

%------------------------------------------------------
% Chapter 2. Simulation of State-Space Systems
%------------------------------------------------------

t = [0:.01:4]; % Define array of time


% values
U = [zeros(size(t))]; % Zero single input of
% proper size to go with
x0 = [0.4; 0.2]; % t Define initial state
% vector [x10; x20]

CharPoly = poly(A) % Find characteristic


% polynomial from A
Poles = roots(CharPoly) % Find the system poles

EigsO = eig(A); % Calculate open-loop


% system eigenvalues
damp(A); % Calculate eigenvalues,
% zeta, and wn from ABCD

[Yo,t,Xo] = lsim(JbkR,U,t,x0);% Open-loop response


CONTINUING MATLAB EXAMPLE M-FILE 449

% (zero input, given ICs)


Xo(101,:); % State vector value at
% t=1 sec
X1 = expm(A*1)*X0; % Compare with state
% transition matrix
% method

figure; % Open-loop State Plots


subplot(211), plot(t,Xo(:,1)); grid;
axis([0 4 -0.2 0.5]);
set(gca,'FontSize',18);
ylabel('{\itx} 1 (\itrad)')
subplot(212), plot(t,Xo(:,2)); grid; axis([0 4 -2 1]);
set(gca,'FontSize',18);
xlabel('\ittime (sec)'); ylabel('{\itx} 2 (\itrad/s)');

%------------------------------------------------------
% Chapter 2. Coordinate Transformations and Diagonal
% Canonical Form
%------------------------------------------------------

[Tdcf,E] = eig(A); % Transform to DCF via


% formula
Adcf = inv(Tdcf)*A*Tdcf;
Bdcf = inv(Tdcf)*B;
Cdcf = C*Tdcf;
Ddcf = D;

[JbkRm,Tm] = canon(JbkR,'modal');
% Calculate DCF using
% MATLAB canon
Am = JbkRm.a
Bm = JbkRm.b
Cm = JbkRm.c
Dm = JbkRm.d

%------------------------------------------------------
% Chapter 3. Controllability
%------------------------------------------------------

P = ctrb(JbkR); % Calculate controllability


% matrix P
450 CONTINUING MATLAB EXAMPLE M-FILE

if (rank(P) == size(A,1)) % Logic to assess


% controllability
disp('System is controllable.');
else
disp('System is NOT controllable.');
end

P1 = [B A*B]; % Check P via the formula

%------------------------------------------------------
% Chapter 3. Coordinate Transformations and
% Controller Canonical Form
%------------------------------------------------------

CharPoly = poly(A); % Determine the system


% characteristic polynomial
a1 = CharPoly(2); % Extract a1

Pccfi = [a1 1;1 0]; % Calculate the inverse of


% matrix Pccf
Tccf = P*Pccfi; % Calculate the CCF
% transformation matrix

Accf = inv(Tccf)*A*Tccf;% Transform to CCF via


% formula
Bccf = inv(Tccf)*B;
Cccf = C*Tccf;
Dccf = D;

%------------------------------------------------------
% Chapter 4. Observability
%------------------------------------------------------

Q = obsv(JbkR); % Calculate observability


% matrix Q
if (rank(Q) == size(A,1))% Logic to assess
% observability
disp('System is observable.');
else
disp('System is NOT observable.');
end
CONTINUING MATLAB EXAMPLE M-FILE 451

Q1 = [C; C*A]; % Check Q via the formula

%------------------------------------------------------
% Chapter 4. Coordinate Transformations and Observer
% Canonical Form
%------------------------------------------------------

Qocf = inv(Pccfi);
Tocf = inv(Q)*Qocf; % Calculate OCF transformation
% matrix
Aocf = inv(Tocf)*A*Tocf; % Transform to OCF via formula
Bocf = inv(Tocf)*B;
Cocf = C*Tocf;
Docf = D;

[JbkROCF,TOCF] = canon(JbkR,'companion');
% Compute OCF using canon
AOCF = JbkROCF.a
BOCF = JbkROCF.b
COCF = JbkROCF.c
DOCF = JbkROCF.d

%------------------------------------------------------
% Chapter 5. Minimal Realizations
% The Continuing Matlab Example is already minimal;
% hence, there is nothing to do here. The student
% may verify this with MATLAB function minreal.
%------------------------------------------------------

%------------------------------------------------------
% Chapter 6. Lyapunov Stability Analysis
%------------------------------------------------------

if (real(Poles(1))==0 | real(Poles(2))==0) % lyap will


fail
if (real(Poles(1))<=0 | real(Poles(2))<=0)
disp('System is marginally stable.');
else
disp('System is unstable.');
end
else % lyap will
% succeed
452 CONTINUING MATLAB EXAMPLE M-FILE

Q = eye(2); % Given positive definite


% matrix
P = lyap(A',Q); % Solve for P
pm1 = det(P(1,1)); % Sylvester's method to see if
P is positive definite
pm2 = det(P(1:2,1:2));
if (pm1>0 & pm2>0) % Logic to assess stability
% condition
disp('System is asymptotically stable.');
else
disp('System is unstable.');
end
end

figure; % Plot phase portraits to enforce


% stability analysis
plot(Xo(:,1),Xo(:,2),'k'); grid; axis('square');
axis([-1.5 1.5 -2 1]);
set(gca,'FontSize',18);
xlabel('{\itx} 1 (rad)'); ylabel('{\itx} 2 (rad/s)');

%------------------------------------------------------
% Chapter 7. Dynamic Shaping
%------------------------------------------------------

PO = 3; ts = 0.7; % Specify percent


% overshoot and settling
% time
^ + log(PO/100)2
term = pi2 ^;
zeta = log(PO/100)/sqrt(term) % Damping ratio from PO
wn = 4/(zeta*ts) % Natural frequency from
% settling time and zeta
num2 = wn^
2; % Generic desired
% second-order system
den2 = [1 2*zeta*wn wn^
2]
DesEig2 = roots(den2) % Desired control law
% eigenvalues
Des2 = tf(num2,den2); % Create desired system
% from num2 and den2

figure;
td = [0:0.01:1.5];
CONTINUING MATLAB EXAMPLE M-FILE 453

step(Des2,td); % Right-click to get


% performance measures

%------------------------------------------------------
% Chapter 7. Design of Linear State Feedback Control
% Laws
%------------------------------------------------------

K = place(A,B,DesEig2) % Compute state


% feedback gain matrix
% K
Kack = acker(A,B, DesEig2); % Check K via
% Ackerman's formula

Ac = A-B*K; Bc = B; % Compute closed-loop


% state feedback system
Cc = C; Dc = D;
JbkRc = ss(Ac,Bc,Cc,Dc); % Create the
% closed-loop
% state-space system

[Yc,t,Xc] = lsim(JbkRc,U,t,X0); % Compare open-loop and


% closed-loop responses

figure;
subplot(211), plot(t,Xo(:,1),'r',t,Xc(:,1),'g'); grid;
axis([0 4 -0.2 0.5]);
set(gca,'FontSize',18);
legend('Open-loop','Closed-loop');
ylabel('{\itx} 1')
subplot(212), plot(t,Xo(:,2),'r',t,Xc(:,2),'g'); grid;
axis([0 4 -2 1]);
set(gca,'FontSize',18);
xlabel('\ittime (sec)'); ylabel('{\itx} 2');

%------------------------------------------------------
% Chapter 8. Design and Simulation of Linear
% Observers for State Feedback
%------------------------------------------------------

% Select desired observer eigenvalues; ten times


control law eigenvalues
454 CONTINUING MATLAB EXAMPLE M-FILE

ObsEig2 = 10*DesEig2;

L = place(A',C', ObsEig2)';% Compute observer gain


% matrix L
Lack = acker(A',C', ObsEig2)';% Check L via Ackerman's
% formula

Ahat = A-L*C; % Compute the closed-loop observer


% estimation error matrix
eig(Ahat); % Check to ensure desired eigenvalues
% are in Ahat

% Compute and simulate closed-loop system with control


% law and observer
Xr0 = [0.4;0.2;0.10;0]; % Define vector of
% initial conditions
Ar = [(A-B*K) B*K;zeros(size(A)) (A-L*C)];
Br = [B;zeros(size(B))];
Cr = [C zeros(size(C))];
Dr = D;
JbkRr = ss(Ar,Br,Cr,Dr); % Create the closed-loop
% system with observer
r = [zeros(size(t))]; % Define zero reference
% input to go with t
[Yr,t,Xr] = lsim(JbkRr,r,t,Xr0);

% Compare Open, Closed, and Control Law/Observer


% responses
figure;
plot(t,Yo,'r',t,Yc,'g',t,Yr,'b'); grid;
axis([0 4 -0.2 0.5]);
set(gca,'FontSize',18);
legend('Open-loop','Closed-loop','w/ Observer');
xlabel('\ittime (sec)'); ylabel('\ity');

figure; % Plot observer errors


plot(t,Xr(:,3),'r',t,Xr(:,4),'g'); grid;
axis([0 0.2 -3.5 0.2]);
set(gca,'FontSize',18);
legend('Obs error 1','Obs error 2');
xlabel('\ittime (sec)'); ylabel('\ite');
CONTINUING MATLAB EXAMPLE M-FILE 455

%------------------------------------------------------
% Chapter 9. Linear Quadratic Regulator Design
%------------------------------------------------------
Q = 20*eye(2); % Weighting matrix for
% state error
R = [1]; % Weighting matrix for
% input effort
BB = B*inv(R)*B';

KLQR = are(A,BB,Q); % Solve algebraic Ricatti


% equation
ALQR = A-B*inv(R)*B'*KLQR; % Compute the closed-loop
% state feedback system
JbkRLQR = ss(ALQR,Bc,Cc,Dc); % Create LQR closed-loop
% state-space system

% Compare open- and closed-loop step responses


[YLQR,t,XLQR] = lsim(JbkRLQR,U,t,X0);

figure;
subplot(211),
plot(t,Xo(:,1),'r',t,Xc(:,1),'g',t,XLQR(:,1),'b');
grid; axis([0 4 -0.2 0.5]);
set(gca,'FontSize',18);
legend('Open-loop','Closed-loop','LQR');
ylabel('{\itx} 1')
subplot(212),
plot(t,Xo(:,2),'r',t,Xc(:,2),'g',t,XLQR(:,2),'b');
grid; axis([0 4 -2 1]);
set(gca,'FontSize',18);
xlabel('\ittime (sec)'); ylabel('{\itx} 2');

% Calculate and plot to compare closed-loop and LQR


% input efforts required
Uc = -K*Xc'; % Chapter 7 input effort
ULQR = -inv(R)*B'*KLQR*XLQR'; % LQR input effort

figure;
plot(t,Uc,'g',t,ULQR,'b'); grid; axis([0 4 -10 6]);
set(gca,'FontSize',18);
legend('Closed-loop','LQR');
xlabel('\ittime (sec)'); ylabel('\itU');

You might also like