I am getting an error while using lsim

43 views (last 30 days)
Sharoon Tariq
Sharoon Tariq on 26 Mar 2023
Commented: Sharoon Tariq on 26 Mar 2023
I have some code im working with thats from a textbook and for some reason it gives me the following error while using lsim;
Error using DynamicSystem/lsim
When simulating the response to a specific input signal, the input data U must be a matrix with as many rows as
samples in the time vector T, and as many columns as input channels.
Error in lsim (line 114)
[yout,t1,x] = lsim(sys,u,t,x0);
Error in ACC (line 46)
Here is the code in question;
% Ex12_5.m
g=9.81; uw=0.0; u0=30.0; rho =1.202;
Theta=0.0; ThetaPrime=0.0;
% Controlled vehicle parameters:
mc=1000.0; Cdc=0.5; Arc=1.5; fc=0.015;
wc0=mc*g*(fc*sin(Theta)-cos(Theta))*ThetaPrime; Fc=(u0/Kc);
% Lead vehicle parameters (typical):
U0=Fc*ones(size(t)); % Nominal control force
wl= -800*(1+0.01*t); % Ramp function
disturbance=[U0 wc wl]; %#ok<NASGU>
% 4-state system for controller-design:
Aa=[0 -1 0 0;
0 -1/Tc 0 0;
1 0 0 0;
0 0 1 0];
% Controller design:
pc=[roots([1 2*0.9*0.4 0.4^2]); -1.08; -1.18];
% Closed-loop simulation (5 states, keep track of vl (x5)):
Ac=[0 -1 0 0 1;
-K(1)*Kc/Tc -(1+K(2)*Kc)/Tc -K(3)*Kc/Tc -K(4)*Kc/Tc 0;
1 0 0 0 0;
0 0 1 0 0;
0 0 0 0 -1/Tl];
Bc=[0 0 0;
0 Kc/Tc 0;
-1 0 0;
0 0 0;
0 0 Kl/Tl];
% outputs: x1 (range), vc and vl
Cc=[1 0 0 0 0;0 1 0 0 0; 0 0 0 0 1]; Dc=zeros(3,3);
disturbance=[r U0+wc wl]; xc0=[30 u0 0 -(u0/Kc+K(1)*30+K(2)*u0)/K(4) u0];
subplot(211), plot(t,yc(:,1)); title('Range');
xlabel('Time (sec)'); grid;
subplot(212), plot(t,yc(:,3), 'r',t,yc(:,2),'-.b');
title('Vehicle speed (m/sec)');
xlabel('Time (sec)'); grid;
legend('Vl’, ‘Vc'); pause;
clf, subplot(211)
u= U0-K(1)*xc(:,1)-K(2)*xc(:,2)-K(3)*xc(:,3)-K(4)*xc(:,4);
plot(t, u'); title('Control Force (N)');
xlabel('Time (sec)'); grid
Dont have much experience with lsim so any help would be appreciated!

Answers (1)

Jack on 26 Mar 2023
Edited: Jack on 26 Mar 2023
The error message indicates that the input data U provided to the lsim function is not in the correct format. Specifically, it should be a matrix with as many rows as samples in the time vector t, and as many columns as input channels.
Looking at the code, the input data disturbance is created as a concatenation of three vectors [r U0+wc wl]. However, the size of U0 is (1, numel(t)), while the size of wc and wl is (1, length(t)). Therefore, the concatenation of these three vectors results in a matrix of size (numel(t), 3), which is not compatible with the lsim function.
To fix the issue, you can modify the creation of the disturbance matrix to ensure that U0, wc, and wl are all the same length. One way to do this is to modify the creation of U0 to use the length function instead of numel, so that it has the same length as wc and wl:
U0=Fc*ones(length(t),1); % Nominal control force
Then, you can concatenate U0, wc, and wl directly to create disturbance:
disturbance=[r U0+wc wl];
With these modifications, the input data disturbance should have the correct format and the lsim function should work correctly.
  1 Comment
Sharoon Tariq
Sharoon Tariq on 26 Mar 2023
Hi Jack,
Thanks for your response, after making the changes i get the following message;
Error using horzcat
Dimensions of arrays being concatenated are not consistent.
Error in ACC (line 21)
disturbance=[U0 wc wl]; %#ok<NASGU>
I tried changing disturbance to what you suggested but received the same message.

Sign in to comment.




Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!