Professional Documents
Culture Documents
Appendix Continuing Matlab Example M-File: τ (t), and the output is the angular displacement θ (t) - This
Appendix Continuing Matlab Example M-File: τ (t), and the output is the angular displacement θ (t) - This
CONTINUING MATLAB
EXAMPLE M-FILE
%------------------------------------------------------
% Continuing MATLAB Example m-file
% Chapter 1 through Chapter 9
% Dr. Bob Williams
%------------------------------------------------------
% Chapter 1. State-Space Description
%------------------------------------------------------
J = 1;
b = 4;
kR = 40;
[num,den] = tfdata(JbkR,'v');
% Extract transfer function
% description
[z,p,k] = zpkdata(JbkR,'v');
% Extract zero-pole description
%------------------------------------------------------
% Chapter 2. Simulation of State-Space Systems
%------------------------------------------------------
%------------------------------------------------------
% Chapter 2. Coordinate Transformations and Diagonal
% Canonical Form
%------------------------------------------------------
[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
%------------------------------------------------------
%------------------------------------------------------
% Chapter 3. Coordinate Transformations and
% Controller Canonical Form
%------------------------------------------------------
%------------------------------------------------------
% Chapter 4. Observability
%------------------------------------------------------
%------------------------------------------------------
% 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
%------------------------------------------------------
%------------------------------------------------------
% Chapter 7. Dynamic Shaping
%------------------------------------------------------
figure;
td = [0:0.01:1.5];
CONTINUING MATLAB EXAMPLE M-FILE 453
%------------------------------------------------------
% Chapter 7. Design of Linear State Feedback Control
% Laws
%------------------------------------------------------
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
%------------------------------------------------------
ObsEig2 = 10*DesEig2;
%------------------------------------------------------
% 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';
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');
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');