MATLAB: Help, urgent: Error using DynamicSystem/lsim

lsim

Hello I have a problem. I have a power train system, as an input I have torque with a constant slope and afte 0.4 seconds i need a constant torque. I realized it with a loop and if conditions. It doesn't work and I don't understand how to change my Code that there won't be an Error anymore.
This is the ERROR:
Error using DynamicSystem/lsim (line 84)
In time response commands, the time vector must be real, finite, and
must contain monotonically increasing and evenly spaced time samples.
Error in Antriebsstrang_Schmitt (line 99)
[y,t,x]=lsim(sys,h1,t);
THIS IS MY CODE:
% Antriebsstrang : Zustandsdarstellung
clear
clc
close all
% System-Parameter
J4 = 0.0784;
J2 = 0.000385;
J1 = 0.000271;
iG = 3.2;
iA = 4.1;
JM = 0.1322; % Motor, ...
cK = 1000;
MM = 90;
JA = J4+iA*iA*J2+iG*iA*iA*iA+J1 % Achswelle, ...
cA = 7300;
JF = 0.3426; % RAd, ...
JR = 1.0457; % Reifen, ...
cR = 31600;
% Systemmatrizen
J = [ JM 0 0 0;...
0 JA 0 0;...
0 0 JF 0;...
0 0 0 JR];
C = [cK (-cK*iG*iA) 0 0;...
(-iG*iA*cK) (-cK*iG*iG*iA*iA+cA) -cA 0;...
0 -cA cA+cR -cR;...
0 0 -cR cR];
% Lastverteilungsvektoren
p1 = [ 1;...
0;...
0;...
0 ]; % zu MM
% Matritzen für System- und Ausgangsgleichung & Modellaufbau
A = [ zeros(4,4) eye(4) ;...
-inv(J)*C -inv(J)*zeros(4,4) ]
B = [ zeros(4,1) ;...
inv(J)*p1 ]
Ca = [ 0 0 0 0 0 0 0 1 ] % phiR'
% phiM' phiM phiA' phiA phiF' phiF phiR' phiR
Da = [ 0 ]
%Loop and If-
for x=0:0.003:1.6;
if x< 0.4
t=datevec(x);
h1= 200*t ;
else
h1= 80;
h1=datevec(h1);
end
hold on
sys = ss(A, B, Ca, Da)
figure (1)
hold on
bode(sys)
%LSIM
[y,t,x]=lsim(sys,h1,t);
end
figure(2) %Ausgabe nach Anregung
figure (2)
plot(t, y);
xlabel('Zeit [s]');
ylabel('Beschleunigung [m/s^2]');
title('Beschleunigung')

Best Answer

It is best to use a combination logical vector with conditions rather than a loop to create your input vector. My ‘h1’ assignment creates two logical vectors, one that is 1 (or true) for t<0.4, and zero elsewhere, multiplies it by 200*t (using element-wise multiplication) and adds it to another complementary vector calculated the same way to create the constant section. Note that logical true converts to numeric 1 in calculations, and false converts to numeric 0. (I plotted the ‘h1’ as a funciton of ‘t’ and it produces the result you describe as what you want.
The reason you are getting the error with your time vector is that datevec produces a 6-element vector for each time. This is not what you want for your simulation.
I added these lines to your code:
% Create ‘h1’ as logical vector with conditions on intervals — NEW CODE REPLACING ‘for’ LOOP AND ‘if’ BLOCK

x = 0:0.003:1.6; % Define ‘x’

t = x; % Define ‘t’

h1 = (200*t).*(t < 0.4) + 80*(t >= 0.4); % Create Input Vector

Your full code with my changes:
% System-Parameter
J4 = 0.0784;
J2 = 0.000385;
J1 = 0.000271;
iG = 3.2;
iA = 4.1;
JM = 0.1322; % Motor, ...
cK = 1000;
MM = 90;
JA = J4+iA*iA*J2+iG*iA*iA*iA+J1 % Achswelle, ...
cA = 7300;
JF = 0.3426; % RAd, ...
JR = 1.0457; % Reifen, ...
cR = 31600;
% Systemmatrizen
J = [ JM 0 0 0;...
0 JA 0 0;...
0 0 JF 0;...
0 0 0 JR];
C = [cK (-cK*iG*iA) 0 0;...
(-iG*iA*cK) (-cK*iG*iG*iA*iA+cA) -cA 0;...
0 -cA cA+cR -cR;...
0 0 -cR cR];
% Lastverteilungsvektoren
p1 = [ 1;...
0;...
0;...
0 ]; % zu MM
% Matritzen für System- und Ausgangsgleichung & Modellaufbau
A = [ zeros(4,4) eye(4) ;...
-inv(J)*C -inv(J)*zeros(4,4) ]
B = [ zeros(4,1) ;...
inv(J)*p1 ]
Ca = [ 0 0 0 0 0 0 0 1 ] % phiR'
% phiM' phiM phiA' phiA phiF' phiF phiR' phiR
Da = [ 0 ]
% Create ‘h1’ as logical vector with conditions on intervals — NEW CODE REPLACING ‘for’ LOOP AND ‘if’ BLOCK
x = 0:0.003:1.6; % Define ‘x’
t = x; % Define ‘t’
h1 = (200*t).*(t < 0.4) + 80*(t >= 0.4); % Create Input Vector
sys = ss(A, B, Ca, Da)
figure (1)
hold on
bode(sys)
%LSIM
[y,t,x]=lsim(sys,h1,t);
figure(2) %Ausgabe nach Anregung
figure (2)
plot(t, y);
xlabel('Zeit [s]');
ylabel('Beschleunigung [m/s^2]');
title('Beschleunigung')
figure(3)
plot(t, h1)
grid
axis([xlim 0 100])
title('Input Signal')
xlabel('Time (s)')
ylabel('Amplitude')