Why am I receiving three outputs for this lsim function?

4 views (last 30 days)
k1=1;
A=[0 1 0 0 ;-k1 -0.4 k1 0.4 ;0 0 0 1; 0.25 0.1 -0.25 -1.35];
B=[5;0;0;0];
C=[1 0 0 0];
D=0;
x0 = C;
t = 0:0.1:50;
u = 0*t;
[y,x1]=lsim(A,B,C,D,u,t,x0);
plot(t,x1);
title('Displacement - k=0.2 Varying b');
xlabel('Time(s)');
ylabel('Displacement')
legend('M1','Cart')

Answers (2)

Star Strider
Star Strider on 30 Oct 2022
Edited: Star Strider on 31 Oct 2022
You’re actually receiving four, one for each state.
However if you create a system object out of the matrices first, you only get one output —
k1=1;
A=[0 1 0 0 ;-k1 -0.4 k1 0.4 ;0 0 0 1; 0.25 0.1 -0.25 -1.35];
B=[5;0;0;0];
C=[1 0 0 0];
D=0;
sys = ss(A,B,C,D)
sys = A = x1 x2 x3 x4 x1 0 1 0 0 x2 -1 -0.4 1 0.4 x3 0 0 0 1 x4 0.25 0.1 -0.25 -1.35 B = u1 x1 5 x2 0 x3 0 x4 0 C = x1 x2 x3 x4 y1 1 0 0 0 D = u1 y1 0 Continuous-time state-space model.
x0 = C;
t = 0:0.1:50;
u = 0*t;
[y,x1]=lsim(sys,u,t,x0);
figure
plot(t,x1);
title('Displacement - k=0.2 Varying b');
xlabel('Time(s)');
ylabel('Displacement')
legend('M1','Cart')
Warning: Ignoring extra legend entries.
figure
stepplot(sys)
grid
NOTE — Your lsim call inputs a ramp function, and the system reacts accordingly.
EDIT — (31 Oct 2022 at :00:18)
There is no indication in the documentation that lsim is obsolete, is going to be deprecated, or is not recommended. It first appeared in R2012a.
.
  1 Comment
Paul
Paul on 31 Oct 2022
lsim is not obsolete. That use of lsim with the A,B,C,D inputs is obsolete. That use is not shown as an option on the linked doc page, and the actual function lsim that is used in that case resides in the ctrlobsolete directory. As shown above, it still works.
As shown on the linked doc page, the CST version of lsim predates R2006a. The version of lsim that takes the A,B,C,D inputs (and num,den for that matter) for continuous-time systems (dlsim was used for discrete-time systems) goes back to a very, very early, if not the very first, release of the Control System Toolbox. According to the old help text it was authored by John Little himself. Curently, that old version of lsim just forms the ss (or tf) object and dispatches to the lsim used for lti objects..

Sign in to comment.


Paul
Paul on 30 Oct 2022
Hi Hunter
k1=1;
A=[0 1 0 0 ;-k1 -0.4 k1 0.4 ;0 0 0 1; 0.25 0.1 -0.25 -1.35];
B=[5;0;0;0];
C=[1 0 0 0];
D=0;
x0 = C;
t = 0:0.1:50;
u = 0*t;
This use of lsim is obsolete, but it still works
which lsim(A,B,C,D,u,t,x0)
/MATLAB/toolbox/control/ctrlobsolete/lsim.m
[y1,x1] = lsim(A,B,C,D,u,t,x0);
Here, y is the output of the sytem and x1 the state vector of the system in response to the initial condition x0 with input being zero (0*t). The system has four states, hence the plot command shows four curves.
figure
plot(t,x1);
If the model is encapsulated in an ss object
sys = ss(A,B,C,D);
the same results can be obtained as
[y2,tout,x2] = lsim(sys,u,t,x0);
where tout will just be a copy of t and the state vector, x2, is the third output of lsim
figure
plot(t,x2)
In accordance with the model, the output is simply the first state, the plot of which matches the blue curve above
figure
plot(t,y2)
ylim([-0.8 1])

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by