Professional Documents
Culture Documents
Homework (3) - 0
Homework (3) - 0
Homework 3
Reinaldy Maslim
A0225239E
for m1,
−𝑘1 𝑥1 − 𝑐1𝑥1̇ + 𝑘2 (𝑥2 − 𝑥1 ) + 𝑐2(𝑥2̇ − 𝑥1̇ ) + 𝑓1 = 𝑚1 𝑥1̈
(𝑐1 + 𝑐2 ) (𝑘1 + 𝑘2 ) 𝑐2 𝑘2 𝑓1
− 𝑥1̇ − 𝑥1 + 𝑥2̇ + 𝑥2 + = 𝑥1̈
𝑚1 𝑚1 𝑚1 𝑚1 𝑚1
for m2,
−𝑘2 (𝑥2 − 𝑥1 ) − 𝑐2 (𝑥2̇ − 𝑥1̇ ) + 𝑓2 = 𝑚2 𝑥2̈
𝑐1 𝑘2 𝑐2 𝑘2 𝑓2
𝑥1̇ + 𝑥1 − 𝑥2̇ − 𝑥2 + = 𝑥2̈
𝑚2 𝑚2 𝑚2 𝑚2 𝑚2
we now let
clc
clear
close all
%% Define parameters
m1 = 1.0; % m1 mass
c1 = 0.2; % c1 damping
k1 = 2.0; % k1 stiffness
m2 = 1.0; % m2 mass
c2 = 0.2; % c2 damping
k2 = 2.0; % k2 stiffness
T0 = 0; % initial time
TN = 50; % final time
dt = 0.01; % time-step
tspan = T0:dt:TN; % time set
%q1_prime=y(1)
%q1=y(2)
%q2_prime=y(3)
%q2=y(4)
%[q1'',q1',q2'',q2']
%% Construct the right-hand side
rhs_mck = @(t,y)[
(-(c1+c2)/m1)*y(1) - (k1+k2)/m1*y(2) + c2/m1*y(3) + k2/m1*y(4); ...
y(1); ...
c2/m2*y(1) + k2/m2*y(2) - c2/m2*y(3) - k2/m2*y(4);...
y(3);...
];
%% Solve the system via the ODE package
[t,sol] = ode45(rhs_mck,tspan,Y0,options);
q1_s = sol(:,2);
q2_s = sol(:,4);
%% Plot results
figure()
subplot(2,1,1); plot(t,q1_s) ; xlabel('t'); ylabel('q_1'); title('Solution vs. time')
grid on
subplot(2,1,2); plot(t,q2_s) ; xlabel('t'); ylabel('q_2')
grid on
figure()
for ic = 1 : 5
x0_prime = ic;
x0 = 0;
y0_prime=ic;
y0 = 0;
%% Plot results
subplot(2,1,1);plot(q1_s,q1_prime); xlabel('q1'); ylabel('q1_{prime}'); title('q1 Phase
Portrait')
plot(q1_s(1) ,q1_prime(1) ,'ob')
plot(q1_s(end),q1_prime(end),'sr')
hold on
subplot(2,1,2);plot(q2_s,q2_prime); xlabel('q2'); ylabel('q2_{prime}'); title('q2 Phase
Portrait')
plot(q2_s(1) ,q2_prime(1) ,'ob')
plot(q2_s(end),q2_prime(end),'sr')
hold on
end
A = [ ...
(-(c1+c2)/m1), -(k1+k2)/m1, c2/m1, k2/m1; ...
1, 0, 0, 0; ...
c2/m2, k2/m2, -c2/m2, -k2/m2;...
0, 0, 1, 0;...
];
B = [ ...
1/m1, 0; ...
0, 0;...
0, 1/m2;...
0, 0;...
];
C = [0 1 0 0;
0 0 0 0];
D=[0 0;0 0]
[v,d]=eig(A)
Output:
4. Discuss the stability of the system for different damping parameters c1 and c2
we vary c1 and c2 by setting c1=0, c2=0.2 in the first case and c1=0.2, c2=0 in the second case
case 1, c1=0, c2=0.2
clc
clear
close all
%vary c1 and c2
%first case, c1=0, c2=0.2
%second case, c1=0.2, c2=0
%third case, c1=5, c2=0.2
m2 = 1.0; % m2 mass
c2 = 0.2; % c2 damping
k2 = 2.0; % k2 stiffness
T0 = 0; % initial time
TN = 50; % final time
dt = 0.01; % time-step
tspan = T0:dt:TN; % time set
%q1_prime=y(1)
%q1=y(2)
%q2_prime=y(3)
%q2=y(4)
%[q1'',q1',q2'',q2']
%% Construct the right-hand side
rhs_mck = @(t,y)[
(-(c1+c2)/m1)*y(1) - (k1+k2)/m1*y(2) + c2/m1*y(3) + k2/m1*y(4); ...
y(1); ...
c2/m2*y(1) + k2/m2*y(2) - c2/m2*y(3) - k2/m2*y(4);...
y(3);...
];
%% Solve the system via the ODE package
[t,sol] = ode45(rhs_mck,tspan,Y0,options);
q1_s = sol(:,2);
q2_s = sol(:,4);
%% Plot results
figure()
subplot(2,1,1); plot(t,q1_s) ; xlabel('t'); ylabel('q_1'); title('Solution vs.
time')
grid on
subplot(2,1,2); plot(t,q2_s) ; xlabel('t'); ylabel('q_2')
grid on
figure()
for ic = 1 : 5
x0_prime = ic;
x0 = 0;
y0_prime=ic;
y0 = 0;
%% Plot results
subplot(2,1,1);plot(q1_s,q1_prime); xlabel('q1'); ylabel('q1_{prime}');
title('q1 Phase Portrait')
plot(q1_s(1) ,q1_prime(1) ,'ob')
plot(q1_s(end),q1_prime(end),'sr')
hold on
subplot(2,1,2);plot(q2_s,q2_prime); xlabel('q2'); ylabel('q2_{prime}');
title('q2 Phase Portrait')
plot(q2_s(1) ,q2_prime(1) ,'ob')
plot(q2_s(end),q2_prime(end),'sr')
hold on
end
A = [ ...
(-(c1+c2)/m1), -(k1+k2)/m1, c2/m1, k2/m1; ...
1, 0, 0, 0; ...
c2/m2, k2/m2, -c2/m2, -k2/m2;...
0, 0, 1, 0;...
];
B = [ ...
1/m1, 0; ...
0, 0;...
0, 1/m2;...
0, 0;...
];
C = [0 1 0 0;
0 0 0 0];
D=[0 0;0 0]
[v,d]=eig(A)
Output:
%% Define parameters
m1 = 1.0; % m1 mass
c1 = 0.2; % c1 damping
k1 = 2.0; % k1 stiffness
m2 = 1.0; % m2 mass
c2 = 0.0; % c2 damping
k2 = 2.0; % k2 stiffness
T0 = 0; % initial time
TN = 50; % final time
dt = 0.01; % time-step
tspan = T0:dt:TN; % time set
%q1_prime=y(1)
%q1=y(2)
%q2_prime=y(3)
%q2=y(4)
%[q1'',q1',q2'',q2']
%% Construct the right-hand side
rhs_mck = @(t,y)[
(-(c1+c2)/m1)*y(1) - (k1+k2)/m1*y(2) + c2/m1*y(3) + k2/m1*y(4); ...
y(1); ...
c2/m2*y(1) + k2/m2*y(2) - c2/m2*y(3) - k2/m2*y(4);...
y(3);...
];
%% Solve the system via the ODE package
[t,sol] = ode45(rhs_mck,tspan,Y0,options);
q1_s = sol(:,2);
q2_s = sol(:,4);
%% Plot results
figure()
subplot(2,1,1); plot(t,q1_s) ; xlabel('t'); ylabel('q_1'); title('Solution vs.
time')
grid on
subplot(2,1,2); plot(t,q2_s) ; xlabel('t'); ylabel('q_2')
grid on
figure()
for ic = 1 : 5
x0_prime = ic;
x0 = 0;
y0_prime=ic;
y0 = 0;
%% Plot results
subplot(2,1,1);plot(q1_s,q1_prime); xlabel('q1'); ylabel('q1_{prime}');
title('q1 Phase Portrait')
plot(q1_s(1) ,q1_prime(1) ,'ob')
plot(q1_s(end),q1_prime(end),'sr')
hold on
subplot(2,1,2);plot(q2_s,q2_prime); xlabel('q2'); ylabel('q2_{prime}');
title('q2 Phase Portrait')
plot(q2_s(1) ,q2_prime(1) ,'ob')
plot(q2_s(end),q2_prime(end),'sr')
hold on
end
A = [ ...
(-(c1+c2)/m1), -(k1+k2)/m1, c2/m1, k2/m1; ...
1, 0, 0, 0; ...
c2/m2, k2/m2, -c2/m2, -k2/m2;...
0, 0, 1, 0;...
];
B = [ ...
1/m1, 0; ...
0, 0;...
0, 1/m2;...
0, 0;...
];
C = [0 1 0 0;
0 0 0 0];
D=[0 0;0 0]
[v,d]=eig(A)
Output:
In the two cases, we observed that m1 decay faster than m2, as we can see from the eigenvalues
of A.
−𝟎. 𝟏𝟖𝟗𝟓 + 𝟐. 𝟐𝟕𝟖𝟑𝐢 −𝟎. 𝟎𝟕𝟐𝟑 + 𝟐. 𝟐𝟖𝟓𝟏𝐢
−𝟎. 𝟏𝟖𝟗𝟓 − 𝟐. 𝟐𝟕𝟖𝟑𝐢 −𝟎. 𝟎𝟕𝟐𝟑 − 𝟐. 𝟐𝟖𝟓𝟏𝐢
𝝀𝟏 = [ ] 𝝀𝟐 = [ ]
−𝟎. 𝟎𝟏𝟎𝟓 + 𝟎. 𝟖𝟕𝟒𝟕𝐢 −𝟎. 𝟎𝟐𝟕𝟕 + 𝟎. 𝟖𝟕𝟒𝟒𝐢
−𝟎. 𝟎𝟏𝟎𝟓 − 𝟎. 𝟖𝟕𝟒𝟕𝐢 −𝟎. 𝟎𝟐𝟕𝟕 − 𝟎. 𝟖𝟕𝟒𝟒𝐢
for large c’s, if either c1 or c2 is large, the system will immediately decay to zero
case 3, c1=5, c2=0.2
clc
clear
close all
%% Define parameters
m1 = 1.0; % m1 mass
c1 = 5.0; % c1 damping
k1 = 2.0; % k1 stiffness
m2 = 1.0; % m2 mass
c2 = 0.2; % c2 damping
k2 = 2.0; % k2 stiffness
T0 = 0; % initial time
TN = 50; % final time
dt = 0.01; % time-step
tspan = T0:dt:TN; % time set
%q1_prime=y(1)
%q1=y(2)
%q2_prime=y(3)
%q2=y(4)
%[q1'',q1',q2'',q2']
%% Construct the right-hand side
rhs_mck = @(t,y)[
(-(c1+c2)/m1)*y(1) - (k1+k2)/m1*y(2) + c2/m1*y(3) + k2/m1*y(4); ...
y(1); ...
c2/m2*y(1) + k2/m2*y(2) - c2/m2*y(3) - k2/m2*y(4);...
y(3);...
];
%% Solve the system via the ODE package
[t,sol] = ode45(rhs_mck,tspan,Y0,options);
q1_s = sol(:,2);
q2_s = sol(:,4);
%% Plot results
figure()
subplot(2,1,1); plot(t,q1_s) ; xlabel('t'); ylabel('q_1'); title('Solution vs.
time')
grid on
subplot(2,1,2); plot(t,q2_s) ; xlabel('t'); ylabel('q_2')
grid on
figure()
for ic = 1 : 5
x0_prime = ic;
x0 = 0;
y0_prime=ic;
y0 = 0;
%% Plot results
subplot(2,1,1);plot(q1_s,q1_prime); xlabel('q1'); ylabel('q1_{prime}');
title('q1 Phase Portrait')
plot(q1_s(1) ,q1_prime(1) ,'ob')
plot(q1_s(end),q1_prime(end),'sr')
hold on
subplot(2,1,2);plot(q2_s,q2_prime); xlabel('q2'); ylabel('q2_{prime}');
title('q2 Phase Portrait')
plot(q2_s(1) ,q2_prime(1) ,'ob')
plot(q2_s(end),q2_prime(end),'sr')
hold on
end
A = [ ...
(-(c1+c2)/m1), -(k1+k2)/m1, c2/m1, k2/m1; ...
1, 0, 0, 0; ...
c2/m2, k2/m2, -c2/m2, -k2/m2;...
0, 0, 1, 0;...
];
B = [ ...
1/m1, 0; ...
0, 0;...
0, 1/m2;...
0, 0;...
];
C = [0 1 0 0;
0 0 0 0];
D=[0 0;0 0]
[v,d]=eig(A)
Output:
5. Discuss the observability of the system assuming that you can measure q1 and q2, q1 only and
q2 only, respectively
Suppose we change the initial parameters to:
m1 = m2 = 2.0
c1 = c2 = 0.2
k1 = k2 = 1.0
then, we can get the observalibility gramian and calculate the eigenvalues.
change entries of C
%change entries of C
%first case C = [0 1 0 0;0 0 0 1]; %can measure q1 and q2
%second case C= [0 1 0 0;0 0 0 0] %can measure q1 only
%third case C= [0 0 0 0;0 0 0 1] %can measure q2 only
%% Define parameters
m1 = 2.0; % m1 mass
c1 = 0.2; % c1 damping
k1 = 1.0; % k1 stiffness
m2 = 2.0; % m2 mass
c2 = 0.2; % c2 damping
k2 = 1.0; % k2 stiffness
T0 = 0; % initial time
TN = 50; % final time
dt = 0.01; % time-step
tspan = T0:dt:TN; % time set
%q1_prime=y(1)
%q1=y(2)
%q2_prime=y(3)
%q2=y(4)
%[x'',x',y'',y']
%% Construct the right-hand side
rhs_mck = @(t,y)[
(-(c1+c2)/m1)*y(1) - (k1+k2)/m1*y(2) + c2/m1*y(3) +
k2/m1*y(4); ...
y(1); ...
c2/m2*y(1) + k2/m2*y(2) - c2/m2*y(3) - k2/m2*y(4);...
y(3);...
];
%% Solve the system via the ODE package
[t,sol] = ode45(rhs_mck,tspan,Y0,options);
q1_s = sol(:,2);
q2_s = sol(:,4);
%% Plot results
figure()
subplot(2,1,1); plot(t,q1_s) ; xlabel('t'); ylabel('q_1'); title('Solution
vs. time')
subplot(2,1,2); plot(t,q2_s) ; xlabel('t'); ylabel('q_2')
figure()
for ic = 1 : 5
x0_prime = ic;
x0 = 0;
y0_prime=ic;
y0 = 0;
%% Plot results
subplot(2,1,1);plot(q1_s,q1_prime); xlabel('q1');
ylabel('q1_{prime}'); title('q1 Phase Portrait')
plot(q1_s(1) ,q1_prime(1) ,'ob')
plot(q1_s(end),q1_prime(end),'sr')
hold on
subplot(2,1,2);plot(q2_s,q2_prime); xlabel('q2');
ylabel('q2_{prime}'); title('q2 Phase Portrait')
plot(q2_s(1) ,q2_prime(1) ,'ob')
plot(q2_s(end),q2_prime(end),'sr')
hold on
end
B = [ ...
1/m1, 0; ...
0, 0;...
0, 1/m2;...
0, 0;...
];
C = [0 1 0 0;
0 0 0 1];
OO = obsv(A,C);
rankOO=rank(OO)
sys = ss(A,B,C,D);
Wo = gram(sys,'o')
[vo,do]=eig(Wo)
Output:
rankOO=
4
Wo =
20.0000 1.0000 30.0000 1.0000
1.0000 5.1000 1.0000 5.0000
30.0000 1.0000 50.0000 2.0000
1.0000 5.0000 2.0000 10.1000
vo =
0.7572 0.3875 -0.0248 0.5251
-0.3875 0.7572 0.5251 0.0248
-0.4680 -0.2395 -0.0401 0.8497
0.2395 -0.4680 0.8497 0.0401
do =
1.2635 0 0 0
0 2.2053 0 0
0 0 13.0666 0
0 0 0 68.6646
%change entries of C
%first case C = [0 1 0 0;0 0 0 1]; %can measure q1 and q2
%second case C= [0 1 0 0;0 0 0 0] %can measure q1 only
%third case C= [0 0 0 0;0 0 0 1] %can measure q2 only
%% Define parameters
m1 = 2.0; % m1 mass
c1 = 0.2; % c1 damping
k1 = 1.0; % k1 stiffness
m2 = 2.0; % m2 mass
c2 = 0.2; % c2 damping
k2 = 1.0; % k2 stiffness
T0 = 0; % initial time
TN = 50; % final time
dt = 0.01; % time-step
tspan = T0:dt:TN; % time set
%q1_prime=y(1)
%q1=y(2)
%q2_prime=y(3)
%q2=y(4)
%[x'',x',y'',y']
%% Construct the right-hand side
rhs_mck = @(t,y)[
(-(c1+c2)/m1)*y(1) - (k1+k2)/m1*y(2) + c2/m1*y(3) +
k2/m1*y(4); ...
y(1); ...
c2/m2*y(1) + k2/m2*y(2) - c2/m2*y(3) - k2/m2*y(4);...
y(3);...
];
%% Solve the system via the ODE package
[t,sol] = ode45(rhs_mck,tspan,Y0,options);
q1_s = sol(:,2);
q2_s = sol(:,4);
%% Plot results
figure()
subplot(2,1,1); plot(t,q1_s) ; xlabel('t'); ylabel('q_1'); title('Solution
vs. time')
subplot(2,1,2); plot(t,q2_s) ; xlabel('t'); ylabel('q_2')
figure()
for ic = 1 : 5
x0_prime = ic;
x0 = 0;
y0_prime=ic;
y0 = 0;
%% Plot results
subplot(2,1,1);plot(q1_s,q1_prime); xlabel('q1');
ylabel('q1_{prime}'); title('q1 Phase Portrait')
plot(q1_s(1) ,q1_prime(1) ,'ob')
plot(q1_s(end),q1_prime(end),'sr')
hold on
subplot(2,1,2);plot(q2_s,q2_prime); xlabel('q2');
ylabel('q2_{prime}'); title('q2 Phase Portrait')
plot(q2_s(1) ,q2_prime(1) ,'ob')
plot(q2_s(end),q2_prime(end),'sr')
hold on
end
%% Controllability and Observability
A = [ ...
(-(c1+c2)/m1), -(k1+k2)/m1, c2/m1, k2/m1; ...
1, 0, 0, 0; ...
c2/m2, k2/m2, -c2/m2, -k2/m2;...
0, 0, 1, 0;...
];
B = [ ...
1/m1, 0; ...
0, 0;...
0, 1/m2;...
0, 0;...
];
C = [0 1 0 0;
0 0 0 0];
OO = obsv(A,C);
rankOO=rank(OO)
sys = ss(A,B,C,D);
Wo = gram(sys,'o')
[vo,do]=eig(Wo)
Output:
rankOO=
4
Wo =
vo =
0.7628 0.3537 0.1073 0.5306
-0.4113 0.6981 0.5860 0.0075
-0.4824 -0.1925 -0.1201 0.8461
0.1276 -0.5920 0.7941 0.0507
do =
0.8798 0 0 0
0 1.5496 0 0
0 0 3.6841 0
0 0 0 18.9865
%change entries of C
%first case C = [0 1 0 0;0 0 0 1]; %can measure q1 and q2
%second case C= [0 1 0 0;0 0 0 0] %can measure q1 only
%third case C= [0 0 0 0;0 0 0 1] %can measure q2 only
%% Define parameters
m1 = 2.0; % m1 mass
c1 = 0.2; % c1 damping
k1 = 1.0; % k1 stiffness
m2 = 2.0; % m2 mass
c2 = 0.2; % c2 damping
k2 = 1.0; % k2 stiffness
T0 = 0; % initial time
TN = 50; % final time
dt = 0.01; % time-step
tspan = T0:dt:TN; % time set
%q1_prime=y(1)
%q1=y(2)
%q2_prime=y(3)
%q2=y(4)
%[x'',x',y'',y']
%% Construct the right-hand side
rhs_mck = @(t,y)[
(-(c1+c2)/m1)*y(1) - (k1+k2)/m1*y(2) + c2/m1*y(3) +
k2/m1*y(4); ...
y(1); ...
c2/m2*y(1) + k2/m2*y(2) - c2/m2*y(3) - k2/m2*y(4);...
y(3);...
];
%% Solve the system via the ODE package
[t,sol] = ode45(rhs_mck,tspan,Y0,options);
q1_s = sol(:,2);
q2_s = sol(:,4);
%% Plot results
figure()
subplot(2,1,1); plot(t,q1_s) ; xlabel('t'); ylabel('q_1'); title('Solution
vs. time')
subplot(2,1,2); plot(t,q2_s) ; xlabel('t'); ylabel('q_2')
figure()
for ic = 1 : 5
x0_prime = ic;
x0 = 0;
y0_prime=ic;
y0 = 0;
%% Plot results
subplot(2,1,1);plot(q1_s,q1_prime); xlabel('q1');
ylabel('q1_{prime}'); title('q1 Phase Portrait')
plot(q1_s(1) ,q1_prime(1) ,'ob')
plot(q1_s(end),q1_prime(end),'sr')
hold on
subplot(2,1,2);plot(q2_s,q2_prime); xlabel('q2');
ylabel('q2_{prime}'); title('q2 Phase Portrait')
plot(q2_s(1) ,q2_prime(1) ,'ob')
plot(q2_s(end),q2_prime(end),'sr')
hold on
end
%% Controllability and Observability
A = [ ...
(-(c1+c2)/m1), -(k1+k2)/m1, c2/m1, k2/m1; ...
1, 0, 0, 0; ...
c2/m2, k2/m2, -c2/m2, -k2/m2;...
0, 0, 1, 0;...
];
B = [ ...
1/m1, 0; ...
0, 0;...
0, 1/m2;...
0, 0;...
];
C = [0 0 0 0;
0 0 0 1];
OO = obsv(A,C);
rankOO=rank(OO)
sys = ss(A,B,C,D);
Wo = gram(sys,'o')
[vo,do]=eig(Wo)
Output:
rankOO=
4
Wo =
vo =
0.7703 0.3595 -0.0614 0.5230
-0.3471 0.7859 0.5108 0.0310
-0.4716 -0.2305 -0.0174 0.8510
0.2524 -0.4472 0.8573 0.0363
do =
0.3387 0 0 0
0 0.5981 0 0
0 0 9.4741 0
0 0 0 49.6891
From the three cases, the eigenvalues of the observability gramian matrix tells us that
observability is better when q2 can be measured.
6. Discuss the controllability of the system for different damping parameters c1 and c2
clc
clear
close all
%vary c1 and c2
%first case, c1=0.2; c2=10
%second case, c1=10, c2=0.2
%third case, c1=10, c2=10
%% Define parameters
m1 = 2.0; % m1 mass
c1 = 0.2; % c1 damping
k1 = 1.0; % k1 stiffness
m2 = 2.0; % m2 mass
c2 = 10; % c2 damping
k2 = 1.0; % k2 stiffness
T0 = 0; % initial time
TN = 50; % final time
dt = 0.01; % time-step
tspan = T0:dt:TN; % time set
%q1_prime=y(1)
%q1=y(2)
%q2_prime=y(3)
%q2=y(4)
%[x'',x',y'',y']
%% Construct the right-hand side
rhs_mck = @(t,y)[
(-(c1+c2)/m1)*y(1) - (k1+k2)/m1*y(2) + c2/m1*y(3) +
k2/m1*y(4); ...
y(1); ...
c2/m2*y(1) + k2/m2*y(2) - c2/m2*y(3) - k2/m2*y(4);...
y(3);...
];
%% Solve the system via the ODE package
[t,sol] = ode45(rhs_mck,tspan,Y0,options);
q1_s = sol(:,2);
q2_s = sol(:,4);
%% Plot results
figure()
subplot(2,1,1); plot(t,q1_s) ; xlabel('t'); ylabel('q_1'); title('Solution
vs. time')
subplot(2,1,2); plot(t,q2_s) ; xlabel('t'); ylabel('q_2')
figure()
for ic = 1 : 5
x0_prime = ic;
x0 = 0;
y0_prime=ic;
y0 = 0;
%% Plot results
subplot(2,1,1);plot(q1_s,q1_prime); xlabel('q1');
ylabel('q1_{prime}'); title('q1 Phase Portrait')
plot(q1_s(1) ,q1_prime(1) ,'ob')
plot(q1_s(end),q1_prime(end),'sr')
hold on
subplot(2,1,2);plot(q2_s,q2_prime); xlabel('q2');
ylabel('q2_{prime}'); title('q2 Phase Portrait')
plot(q2_s(1) ,q2_prime(1) ,'ob')
plot(q2_s(end),q2_prime(end),'sr')
hold on
end
%% Controllability and Observability
A = [ ...
(-(c1+c2)/m1), -(k1+k2)/m1, c2/m1, k2/m1; ...
1, 0, 0, 0; ...
c2/m2, k2/m2, -c2/m2, -k2/m2;...
0, 0, 1, 0;...
];
B = [ ...
1/m1, 0; ...
0, 0;...
0, 1/m2;...
0, 0;...
];
C = [0 1 0 0;
0 0 0 1];
CC = ctrb(A,B);
rankCC=rank(CC)
sys = ss(A,B,C,D);
Wc = gram(sys,'c')
[vc,dc]=eig(Wc)
Output:
rankCC =
Wc =
vc =
dc =
0.0124 0 0 0
0 0.0223 0 0
0 0 1.7465 0
0 0 0 7.1351
%vary c1 and c2
%first case, c1=0.2; c2=10
%second case, c1=10, c2=0.2
%third case, c1=10, c2=10
%% Define parameters
m1 = 2.0; % m1 mass
c1 = 10; % c1 damping
k1 = 1.0; % k1 stiffness
m2 = 2.0; % m2 mass
c2 = 0.2; % c2 damping
k2 = 1.0; % k2 stiffness
T0 = 0; % initial time
TN = 50; % final time
dt = 0.01; % time-step
tspan = T0:dt:TN; % time set
%q1_prime=y(1)
%q1=y(2)
%q2_prime=y(3)
%q2=y(4)
%[x'',x',y'',y']
%% Construct the right-hand side
rhs_mck = @(t,y)[
(-(c1+c2)/m1)*y(1) - (k1+k2)/m1*y(2) + c2/m1*y(3) +
k2/m1*y(4); ...
y(1); ...
c2/m2*y(1) + k2/m2*y(2) - c2/m2*y(3) - k2/m2*y(4);...
y(3);...
];
%% Solve the system via the ODE package
[t,sol] = ode45(rhs_mck,tspan,Y0,options);
q1_s = sol(:,2);
q2_s = sol(:,4);
%% Plot results
figure()
subplot(2,1,1); plot(t,q1_s) ; xlabel('t'); ylabel('q_1'); title('Solution
vs. time')
subplot(2,1,2); plot(t,q2_s) ; xlabel('t'); ylabel('q_2')
figure()
for ic = 1 : 5
x0_prime = ic;
x0 = 0;
y0_prime=ic;
y0 = 0;
%% Plot results
subplot(2,1,1);plot(q1_s,q1_prime); xlabel('q1');
ylabel('q1_{prime}'); title('q1 Phase Portrait')
plot(q1_s(1) ,q1_prime(1) ,'ob')
plot(q1_s(end),q1_prime(end),'sr')
hold on
subplot(2,1,2);plot(q2_s,q2_prime); xlabel('q2');
ylabel('q2_{prime}'); title('q2 Phase Portrait')
plot(q2_s(1) ,q2_prime(1) ,'ob')
plot(q2_s(end),q2_prime(end),'sr')
hold on
end
%% Controllability and Observability
A = [ ...
(-(c1+c2)/m1), -(k1+k2)/m1, c2/m1, k2/m1; ...
1, 0, 0, 0; ...
c2/m2, k2/m2, -c2/m2, -k2/m2;...
0, 0, 1, 0;...
];
B = [ ...
1/m1, 0; ...
0, 0;...
0, 1/m2;...
0, 0;...
];
C = [0 1 0 0;
0 0 0 1];
CC = ctrb(A,B);
rankCC=rank(CC)
sys = ss(A,B,C,D);
Wc = gram(sys,'c')
[vc,dc]=eig(Wc)
Output:
rankCC =
Wc =
vc =
dc =
0.0243 0 0 0
0 0.0760 0 0
0 0 0.6993 0
0 0 0 1.6078
clc
clear
close all
%vary c1 and c2
%first case, c1=0.2; c2=10
%second case, c1=10, c2=0.2
%third case, c1=10, c2=10
%% Define parameters
m1 = 2.0; % m1 mass
c1 = 10; % c1 damping
k1 = 1.0; % k1 stiffness
m2 = 2.0; % m2 mass
c2 = 10; % c2 damping
k2 = 1.0; % k2 stiffness
T0 = 0; % initial time
TN = 50; % final time
dt = 0.01; % time-step
tspan = T0:dt:TN; % time set
%q1_prime=y(1)
%q1=y(2)
%q2_prime=y(3)
%q2=y(4)
%[x'',x',y'',y']
%% Construct the right-hand side
rhs_mck = @(t,y)[
(-(c1+c2)/m1)*y(1) - (k1+k2)/m1*y(2) + c2/m1*y(3) +
k2/m1*y(4); ...
y(1); ...
c2/m2*y(1) + k2/m2*y(2) - c2/m2*y(3) - k2/m2*y(4);...
y(3);...
];
%% Solve the system via the ODE package
[t,sol] = ode45(rhs_mck,tspan,Y0,options);
q1_s = sol(:,2);
q2_s = sol(:,4);
%% Plot results
figure()
subplot(2,1,1); plot(t,q1_s) ; xlabel('t'); ylabel('q_1'); title('Solution
vs. time')
subplot(2,1,2); plot(t,q2_s) ; xlabel('t'); ylabel('q_2')
figure()
for ic = 1 : 5
x0_prime = ic;
x0 = 0;
y0_prime=ic;
y0 = 0;
%% Plot results
subplot(2,1,1);plot(q1_s,q1_prime); xlabel('q1');
ylabel('q1_{prime}'); title('q1 Phase Portrait')
plot(q1_s(1) ,q1_prime(1) ,'ob')
plot(q1_s(end),q1_prime(end),'sr')
hold on
subplot(2,1,2);plot(q2_s,q2_prime); xlabel('q2');
ylabel('q2_{prime}'); title('q2 Phase Portrait')
plot(q2_s(1) ,q2_prime(1) ,'ob')
plot(q2_s(end),q2_prime(end),'sr')
hold on
end
%% Controllability and Observability
A = [ ...
(-(c1+c2)/m1), -(k1+k2)/m1, c2/m1, k2/m1; ...
1, 0, 0, 0; ...
c2/m2, k2/m2, -c2/m2, -k2/m2;...
0, 0, 1, 0;...
];
B = [ ...
1/m1, 0; ...
0, 0;...
0, 1/m2;...
0, 0;...
];
C = [0 1 0 0;
0 0 0 1];
CC = ctrb(A,B);
rankCC=rank(CC)
sys = ss(A,B,C,D);
Wc = gram(sys,'c')
[vc,dc]=eig(Wc)
Output:
rankCC =
Wc =
vc =
dc =
0.0073 0 0 0
0 0.0095 0 0
0 0 0.0655 0
0 0 0 0.3427
From the three cases, based on the eigenvalues of the controllability gramian matrix and
the plots, we can conclude that a larger value of c1 has adverse effects on the
controllability of the system compared to a large value of c2, in which the system still
oscillates. For large values of c1 and c2, the system is overdamped and decay to zero
immediately.
7. What happens to the controllability of the system when we apply the input u on the first
mass only?
The system is still controllable, with rank of CC=4, however the degree of controllability
decreased significantly as reflected from the eigenvalues of the controllability gramian
matrix and the zero columns in CC.
We change the entry in the matrix B.
clc
clear
close all
%% Define parameters
m1 = 2.0; % m1 mass
c1 = 0.2; % c1 damping
k1 = 1.0; % k1 stiffness
m2 = 2.0; % m2 mass
c2 = 0.2; % c2 damping
k2 = 1.0; % k2 stiffness
T0 = 0; % initial time
TN = 50; % final time
dt = 0.01; % time-step
tspan = T0:dt:TN; % time set
%q1_prime=y(1)
%q1=y(2)
%q2_prime=y(3)
%q2=y(4)
%[x'',x',y'',y']
%% Construct the right-hand side
rhs_mck = @(t,y)[
(-(c1+c2)/m1)*y(1) - (k1+k2)/m1*y(2) + c2/m1*y(3) + k2/m1*y(4) +
exp(-(t - 15)^16 /1e10) * sin(8*pi*t); ...
y(1); ...
c2/m2*y(1) + k2/m2*y(2) - c2/m2*y(3) - k2/m2*y(4);...
y(3);...
];
%% Solve the system via the ODE package
[t,sol] = ode45(rhs_mck,tspan,Y0,options);
q1_s = sol(:,2);
q2_s = sol(:,4);
%% Plot results
figure()
subplot(2,1,1); plot(t,q1_s) ; xlabel('t'); ylabel('q_1'); title('Solution
vs. time')
subplot(2,1,2); plot(t,q2_s) ; xlabel('t'); ylabel('q_2')
figure()
for ic = 1 : 5
x0_prime = ic;
x0 = 0;
y0_prime=ic;
y0 = 0;
%% Plot results
subplot(2,1,1);plot(q1_s,q1_prime); xlabel('q1');
ylabel('q1_{prime}'); title('q1 Phase Portrait')
plot(q1_s(1) ,q1_prime(1) ,'ob')
plot(q1_s(end),q1_prime(end),'sr')
hold on
subplot(2,1,2);plot(q2_s,q2_prime); xlabel('q2');
ylabel('q2_{prime}'); title('q2 Phase Portrait')
plot(q2_s(1) ,q2_prime(1) ,'ob')
plot(q2_s(end),q2_prime(end),'sr')
hold on
end
%% Controllability and Observability
A = [ ...
(-(c1+c2)/m1), -(k1+k2)/m1, c2/m1, k2/m1; ...
1, 0, 0, 0; ...
c2/m2, k2/m2, -c2/m2, -k2/m2;...
0, 0, 1, 0;...
];
B = [ ...
1/m1, 0; ...
0, 0;...
0, 0;...
0, 0;...
];
C = [0 1 0 0;
0 0 0 1];
D=[0 0;0 0]
CC = ctrb(A,B)
OO = obsv(A,C)
rank(CC)
rank(OO)
sys = ss(A,B,C,D);
Wc = gram(sys,'c')
[vc,dc]=eig(Wc)
Wo = gram(sys,'o')
[vo,do]=eig(Wo)
%[b,a] = ss2tf(A,B,C,D)
systf=tf(sys);
figure()
subplot(2,1,1)
step(systf)
subplot(2,1,2)
impulse(systf)
Output:
ans =
4
Wc =
vc =
dc =
0.2492 0 0 0
0 0.3432 0 0
0 0 0.9192 0
0 0 0 4.7384
8. Write the transfer function of the system and its impulse response in Matlab
clc
clear
close all
%% Define parameters
m1 = 1.0; % m1 mass
c1 = 0.2; % c1 damping
k1 = 2.0; % k1 stiffness
m2 = 1.0; % m2 mass
c2 = 0.2; % c2 damping
k2 = 2.0; % k2 stiffness
T0 = 0; % initial time
TN = 50; % final time
dt = 0.01; % time-step
tspan = T0:dt:TN; % time set
%q1_prime=y(1)
%q1=y(2)
%q2_prime=y(3)
%q2=y(4)
%[x'',x',y'',y']
%% Construct the right-hand side
rhs_mck = @(t,y)[
(-(c1+c2)/m1)*y(1) - (k1+k2)/m1*y(2) + c2/m1*y(3) +
k2/m1*y(4); ...
y(1); ...
c2/m2*y(1) + k2/m2*y(2) - c2/m2*y(3) - k2/m2*y(4);...
y(3);...
];
%% Solve the system via the ODE package
[t,sol] = ode45(rhs_mck,tspan,Y0,options);
q1_s = sol(:,2);
q2_s = sol(:,4);
%% Plot results
figure()
subplot(2,1,1); plot(t,q1_s) ; xlabel('t'); ylabel('q_1');
title('Solution vs. time')
subplot(2,1,2); plot(t,q2_s) ; xlabel('t'); ylabel('q_2')
figure()
for ic = 1 : 5
x0_prime = ic;
x0 = 0;
y0_prime=ic;
y0 = 0;
%% Plot results
subplot(2,1,1);plot(q1_s,q1_prime); xlabel('q1');
ylabel('q1_{prime}'); title('q1 Phase Portrait')
plot(q1_s(1) ,q1_prime(1) ,'ob')
plot(q1_s(end),q1_prime(end),'sr')
hold on
subplot(2,1,2);plot(q2_s,q2_prime); xlabel('q2');
ylabel('q2_{prime}'); title('q2 Phase Portrait')
plot(q2_s(1) ,q2_prime(1) ,'ob')
plot(q2_s(end),q2_prime(end),'sr')
hold on
end
%% Controllability and Observability
A = [ ...
(-(c1+c2)/m1), -(k1+k2)/m1, c2/m1, k2/m1; ...
1, 0, 0, 0; ...
c2/m2, k2/m2, -c2/m2, -k2/m2;...
0, 0, 1, 0;...
];
B = [ ...
1/m1, 0; ...
0, 0;...
0, 1/m2;...
0, 0;...
];
C = [0 1 0 0;
0 0 0 1];
D=[0 0;0 0]
CC = ctrb(A,B)
OO = obsv(A,C)
rank(CC)
rank(OO)
sys = ss(A,B,C,D);
Wc = gram(sys,'c')
[vc,dc]=eig(Wc)
Wo = gram(sys,'o')
[vo,do]=eig(Wo)
%[b,a] = ss2tf(A,B,C,D)
systf=tf(sys)
figure()
subplot(2,1,1)
step(systf)
subplot(2,1,2)
impulse(systf)
systf =
From input 1 to output...
s^2 + 0.2 s + 2
1: ------------------------------------
s^4 + 0.6 s^3 + 6.04 s^2 + 0.8 s + 4
0.2 s + 2
2: ------------------------------------
s^4 + 0.6 s^3 + 6.04 s^2 + 0.8 s + 4
s^2 + 0.4 s + 4
2: ------------------------------------
s^4 + 0.6 s^3 + 6.04 s^2 + 0.8 s + 4