Having trouble plotting in real-time

Hi, I am new to MATLAB (student) and I have a school assignment where I have to plot a Euler-approximation in real-time but I am having a bit of trouble. My real-time plot seems to just draw a straight line for whatever reason but if I plot it normally it draws my curves just fine.
Here is my code:
% Euler's Method
% Initial conditions and setup
g = 25;
h = 0.1; % step size
M = 10; % Number of cars
x = 0:h:40; % the range of x (time)
n = numel(x); % the number of timesteps
posMat = zeros(M,n); % Matrix that holds positions of each car (car = row, time = column)
for i=1:M
posMat(i,1) = 10*i; % the initial positions for the cars
end
for t=2:(n-1)
% Calculate position of all cars EXCEPT car M (using the first
% equation)
for c=1:(M-1)
f = carFunc(posMat(c+1,t) - posMat(c,t));
posMat(c,t+1) = posMat(c,t) + h * f;
end
% Calculate positon of car M (first car in queue) here:
% x_M^(n+1) = x_M^n + h*g
posMat(M,t+1) = posMat(M,t) + h*g;
end
hold on
for i=1:n
plot(zeros(M,1),posMat(:,i),'r*');
axis([-1 1 0 1000])
drawnow
pause(h)
end
hold off
function output = carFunc(x)
if (x >= 75)
output = 25;
else
output = x/3;
end
end
With this code I just get a plot with a straight red line that looks like this:
The plotting code
hold on
for i=1:n
plot(zeros(M,1),posMat(:,i),'r*');
axis([-1 1 0 1000])
drawnow
pause(h)
end
Was provided to me by the teachers and it SHOULD work... but clearly doesn't! What am I doing wrong?
My own (non-realtime) plotting code
hold on
for i=1:M
plot(x,posMat(i,:))
end
hold off
works just fine though.
The resulting plot from that code looks like this:
Help? Thanks!

2 Comments

plot(zeros(M,1),posMat(:,i),'r*');
will always have zero as the X value. You must have transcribed something incorrectly from what was provided or there is just a mistake in the code as you were given.
@dpb
The original code piece given to me was:
for n=1:Nsteps
plot(x(n,:),zeros(1,Ncars),'r*');
axis([0 1000 -1 1])
drawnow
pause(h)
end
Where 'x' is a matrix where the rows represent the timesteps and the columns represent the cars but my matrix 'posMat' was the other way around (car = row, timestep = column) so I switched the parameter positions in the plot function and switched the indexes to match my matrix structure. Problem is, I have no idea what the variable 'Ncars' is supposed to be. It is never explained in the task. I just assumed that it was the number of cars I am calculating for (AKA 'M').

Sign in to comment.

 Accepted Answer

Look at the code that creates the vertical line plots.
plot(zeros(M,1),posMat(:,i),'r*');
All the X coordinates of the points being plotted are 0. There's no way that can give you anything but a vertical line.
FYI you might find the animatedline function useful.

3 Comments

How would you have solved this?
You haven't shown the text of the homework assignment so I'm not certain what you're allowed/expected/forbidden to use (often professors put limitations on what they expect students to use, some of which are silly limitations like "no built-in functions") but I'd probably put a call to animatedline before the start of the main loop and use addpoints (possibly with a call to drawnow with one of its input arguments) to add points to that animatedline inside the loop.
I'd probably also use more descriptive names for the variables, to make the code easier to read and understand. Compare:
h = 0.1; % step size
M = 10; % Number of cars
x = 0:h:40; % the range of x (time)
n = numel(x); % the number of timesteps
posMat = zeros(M,n); % Matrix that holds positions of each car (car = row, time = column)
and:
stepSize = 0.1;
numCars = 10;
raceTimes = 0:stepSize:40;
numTimes = numel(raceTimes);
carPositions = zeros(numCars,numTimes);
Even without comments, can you tell the purpose of each of the variables in the second code segment? The variable names are longer, but tab completion makes it so you usually don't have to type the full names.
If your assignment requires you to use specific variable names, you could always use descriptive names in your code and assign to the required variable names at the end. That would probably be a technically correct solution to the assignment: the best kind of correct ;)
posMat = carPositions;
@StevenLord
Yeah, you're right I should change my variable names to something more readable haha!
Just for the record I am allowed to use ANYTHING that MATLAB offers. No limitations have been set as far as I am aware.

Sign in to comment.

More Answers (0)

Categories

Find more on Graphics Performance in Help Center and File Exchange

Products

Release

R2018b

Asked:

on 12 Nov 2018

Commented:

on 12 Nov 2018

Community Treasure Hunt

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

Start Hunting!