During the moving random points, I don't see animation, instead I see re scattering?

2 views (last 30 days)
Please, would you tell me why the random points is not moving, they just re-scatter in each time step, while I am trying to animate thm or make them moving continously. if you have any help I would appreciat it , thanks in advance
npts=2;center=[0 0];radius=1000;
% Initial direction/velocity of the points
velocity = 28.8/3.6;
% Create random starting locations within the circle
Totaltime =10;
direction = rand(npts, 1) * 2 *pi;
for Timestep=1:1:Totaltime
theta = rand(npts, 1) * 2*pi;
g = 0.5 * radius + 0.5 * radius * rand(npts,1);
X_x=center(1)+g.*cos(theta);
Y_y=center(2)+g.*sin(theta);
XY = [X_x ,Y_y];
DX = [cos(direction(:)) .* velocity,sin(direction(:)) .* velocity];
XYnew = XY + DX;
% Plot the dots as black markers
hdots = plot(XYnew(:,1), XYnew(:,1),'Marker', '.','Color', 'k','LineStyle', 'none','MarkerSize', 12);
hold on
axis equal
% Plot the circle as a reference
t = linspace(0, 2*pi, 100);
plot(radius * cos(t) + center(1),radius * sin(t) + center(2))
% Update the dot plot to reflect n ew locations
set(hdots, 'XData', XY(:,1), 'YData', XY(:,2))
% Force a r edraw
drawnow
pause (1)
end

Accepted Answer

Walter Roberson
Walter Roberson on 2 Jun 2022
Your code builds new random points each time step, and directly jump the old points to the new position.
In order to have them visibly move to new positions, you would need to generate intermediate positions and move the points to through those positions.
  3 Comments
Walter Roberson
Walter Roberson on 3 Jun 2022
Every major step you generate notably different coordinates (for example, current location of people sampled every ten minutes... when they might be in a car or elevator, so there might be fair jumps in coordinates.)
Your current code generates the new coordinates, and then sets the graphics to be the new coordinates directly.
If your sampling period was small compared to the expected maximum change, then the method you use would work to animate the positions.
But your random movement is large compared to the time step, and your current code would make visible jumps instead of smooth jumps.
You have two options:
  • you can reduce the maximum movement per time step so everything becomes smooth. If you do this, it would probably be a good idea to model a remembered velocity so that you can get points moving in straight lines instead of brownian motion
  • or, you could generate the jumps like you do, but then inside the time loop have another loop that draws each point moving a portion of the way between the old locations and the new locations. For example divide the change in coordinates by 10 and take 10 graphic steps moving 1/10 of the way each time.
omar th
omar th on 8 Jun 2022
first, Thank you so much for your reply. regards to my question I have used While loop instead of for loop and now the points are moving in one shot, meaning each point moves each one second. the thing that I am trying to do is iterating the the while loop and try to compare the previous state with the current state. for example if i wanted to obtain X inside the loop I want compare X(i)=current state with X(i-1)=past state
thanks in advance
npts=1;center=[0 0];radius=1000;
npts2=1;center2=[0 0];radius2=1000;
% Initial direction/velocity of the points
velocity = 28.8/3.6;
velocity2 = 28.8/3.6;
theta = rand(npts, 1) * 2*pi;
g = 0.5 * radius + 0.5 * radius * rand(npts,1);
X_x=center(1)+g.*cos(theta);
Y_y=center(2)+g.*sin(theta);
XY = [X_x ,Y_y];
theta2 = rand(npts2, 1) * 2*pi;
g2 = 0.5 * radius2 + 0.5 * radius2 * rand(npts2,1);
X_x2=center2(1)+g2.*cos(theta2);
Y_y2=center2(2)+g.*sin(theta2);
XY2 = [X_x2 ,Y_y2];
hfig = figure('Color', 'w');
hax = axes('Parent', hfig);
hdots(1) = plot(XY(1,1),XY(1,2),'Parent', hax,'Marker', '.','Color', 'k','LineStyle', 'none','MarkerSize', 10);
hold(hax, 'on')
axis(hax, 'equal')
hdots(2) = plot(XY2(1,1),XY2(1,2),'Parent', hax,'Marker', '.','Color', 'r','LineStyle', 'none','MarkerSize', 10);
hold(hax, 'on')
axis(hax, 'equal')
% Plot the circl e as a reference
t = linspace(0, 2*pi, 100);
plot(radius * cos(t) + center(1),radius * sin(t) + center(2))
while all(ishghandle(hdots)) %Timestep=1:1:Totaltime
direction2 = rand(npts, 1) * 2 *pi;
direction = rand(npts, 1) * 2 *pi;
[XY2, direction2] = step(XY2, direction2, velocity2, radius2, center2);
% Plot the dots as black markers
[XY, direction] = step(XY, direction, velocity, radius, center);
% Update the dot plot to reflect n ew locations
set(hdots(2), 'XData', XY2(1,1), 'YData', XY2(1,2))
set(hdots(1), 'XData', XY(1,1), 'YData', XY(1,2))
% Force a r edraw
drawnow
%pause (1)
end

Sign in to comment.

More Answers (0)

Categories

Find more on Discrete Data Plots in Help Center and File Exchange

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!