Plotting lsim directly results in different plot when writing to variables first
9 views (last 30 days)
Show older comments
Hello, I have come across this issue when simulating a system.
lsim provides an output directly, which appears as expected with 'discrete steps'. But when I write the output of lsim to variables for later use, the plotting of those variables results in a smooth output. Is this expected? I have checked to see if plot is interpolating between the discrete points, there is a function 'stairs' which stops this interpolation, but I don't think that should be necessary. Is this due to using a discrete transfer function?
Edit. Just to clarify, I am expecting to see the same plot when using lsim(GVelD, u), or using [VelD, toutVD] = lsim(GVelD, u); plot(toutVD, VelD).
Ts = 0.07; % Sampling rate
GVelC = tf([6.33], [(1/(2*pi*0.54)) 1]) % S Domain TF
GVelD = c2d(GVelC, Ts, 'zoh') % Z Domain TF
% Generate a square wave for 7 cycles.
P=ones(1,20);
N=zeros(1,20);
PN=[P N];
u=[PN PN PN PN PN PN PN];
t = 0:Ts:Ts*length(u)-Ts;
%% Plot lsim directly
clf;
lsim(GVelD, u);
%% Or write lsim to variables first, then plot
clf;
[VelD, toutVD] = lsim(GVelD, u);
plot(toutVD, VelD);
2 Comments
Sulaymon Eshkabilov
on 1 Jun 2021
Yes, the stair type graph is due to discrete system (c2d) simulation. You can adjust it by changing your discrete time step.
If you plot just the continuous system tf (GVelC), then those stair type of jagged shapes won't be there.
Answers (1)
Paul
on 1 Jun 2021
plot() is a general purpose plotting function and has no way of knowing that the inputs to be plotted represent sample times and sample outputs from a discrete time system. It just plots the data.
On the other hand, lsim() knows that it's plotting the output of a discrete time system and plots the output appropriately.
See Also
Categories
Find more on Lighting, Transparency, and Shading 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!