I am getting an error while using lsim
26 views (last 30 days)
Show older comments
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)
[yc,xc]=lsim(Ac,Bc,Cc,Dc,disturbance,t,x0);
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;
Kc=(1/(rho*Cdc*Arc*(u0+uw)));Tc=mc*Kc;
wc0=mc*g*(fc*sin(Theta)-cos(Theta))*ThetaPrime; Fc=(u0/Kc);
% Lead vehicle parameters (typical):
ml=1500.0;
Cdl=0.6;
Arl=1.95;
fl=0.015;
Kl=(1/(rho*Cdl*Arl*(u0+uw)));
Tl=ml*Kl;
Fl=(u0/Kl);
wl0=(ml*g*(fl*sin(Theta)-cos(Theta))*ThetaPrime);
t=0:0.1:20;
U0=Fc*ones(size(t)); % Nominal control force
wl= -800*(1+0.01*t); % Ramp function
wc=wc0*ones(size(t));
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];
Ba=[0;Kc/Tc;0;0];
% Controller design:
pc=[roots([1 2*0.9*0.4 0.4^2]); -1.08; -1.18];
K=place(Aa,Ba,pc);
% 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);
r=30.0*ones(size(t));
disturbance=[r U0+wc wl]; xc0=[30 u0 0 -(u0/Kc+K(1)*30+K(2)*u0)/K(4) u0];
[yc,xc]=lsim(Ac,Bc,Cc,Dc,disturbance,t,xc0);
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!
0 Comments
Answers (1)
Jack
on 26 Mar 2023
Edited: Jack
on 26 Mar 2023
Hi,
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.
See Also
Categories
Find more on Classical Control Design in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!