MATLAB Code For No.2: All All

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

MATLAB Code for No.

2
-----------------------------------------------------------------------------
clear all
clc
close all

% State Space data


A = [-0.0157 5.899 -9.789 0;
-0.0014 -0.7481 0 0.985;
0 0 0 1;
-0.0006 -4.257 0 -0.881];
B = [0 0.7443;
-0.0727 -0.0041;
0 0;
-4.387 0.0384];
C = [1 0 -2 0];
D = zeros(1,2);

% Observability and Controlability check


AB = A*B;
A2B = A*A*B;
A3B = A*A*A*B;
CA = C*A;
CA2 = C*A*A;
CA3 = C*A*A*A;
ConMat = [B AB A2B A3B]
rankConMat = rank(ConMat)
ObsMat = [C.' CA.' CA2.' CA3.'].'
rankObsMat = rank(ObsMat)

% State Space
sys = ss(A,B,C,D);

% Y/U Transfer Function


[elevatorNum,elevatorDen] = ss2tf(A,B,C,D,1)
[thrustNum,thrustDen] = ss2tf(A,B,C,D,2)

% X/U Transfer Function


[elevatorXperUNum,elevatorXperUDen] = ss2tf(A,B,eye(4),zeros(4,2),1)
[thrustXperUNum,thrustXperUDen] = ss2tf(A,B,eye(4),zeros(4,2),2)

% Stability check from Characteristic Polynomial


disp('Characteristics Polynomial Roots: ')
disp(roots(elevatorDen))

% Stability check (Routh Hurwitz)


RootNumber = size(elevatorDen);
RHmatColumn = ceil(RootNumber(2)/2);
RHmat = zeros(2,RHmatColumn);
col = 1;
for i = 1:RHmatColumn
RHmat(1,i)=elevatorDen(col);
col=col+1;
if col>RootNumber
RHmat(2,i)=0;
else
RHmat(2,i)=elevatorDen(col);
col=col+1;
end
end
j = 3;
while true
for i=1:RHmatColumn-1
RHmat(j,i)=(RHmat(j-1,1)*RHmat(j-2,i+1)-RHmat(j-2,1)*RHmat(j-
1,i+1))/RHmat(j-1,1);
end
if RHmat(j,:)==0
break
else
j=j+1;
end
end
disp('Routh Hurwitz Matrix')
disp(RHmat)

% Setting the simulation time


t = 0:1:1000;

% The input
x0 = zeros(2,1001);
x0(1,:) = 0.2;
x0(2,:) = 0;

% Simulating and plotting the response


[y,t,x] = lsim(sys,x0,t);

figure()
plot(t,y)
title('y(t) response')
xlabel('time(second)')
ylabel('y(t)')

figure()
plot(t,x(:,1))
title('u(t) response')
xlabel('time(second)')
ylabel('u')

figure()
plot(t,x(:,2))
title('\alpha (t) response')
xlabel('time(second)')
ylabel('\alpha')

figure()
plot(t,x(:,3))
title('\theta (t) response')
xlabel('time(second)')
ylabel('\theta')

figure()
plot(t,x(:,4))
title('q(t) response')
xlabel('time(second)')
ylabel('q')
----------------------------------------------------------------
Run Result:

You might also like