Animations of several points

6 views (last 30 days)
Cheetezz
Cheetezz on 22 Mar 2016
Commented: Utkarsh Anand on 27 May 2020
I am trying to simulate the trajectories of a few particles in 2D on Matlab. I have the x- and y- coordinates of these particles as a function of time, which I store as matrix x and y. The column in both x and y corresponds to the time, while the row corresponds to the particle number: 1, 2, etc.
I know how to do the animation for one particle, but I am not sure how to customize the code for multiple particles' trajectories. Basically, my idea is that on the initial plot, I have 3 markers which correspond to the initial position of the particles, say particle A, B and C. Then, I would like to follow the movement of these 3 markers, and here is where I encountered the problem: I don't know how to sort the subsequent points according to the particle identity. For example, I want to specify the first point I plot in the second time point as particle A, second point as particle B and third point in particle C.
I have tried this but this will simulate the trajectory particle by particle, and then erase the data when the next particle is simulated. I would like to plot all of these trajectories on the same plot.
% for i = 1:nPart
% for ii = 1:length(x(i,:))
% pause(0.01)
% set(h, 'XData', x(i,1:ii), 'YData', y(i,1:ii));
% drawnow %// you can probably remove this line, as pause already calls drawnow
% hold all
% end
% end
Any idea will be greatly appreciated. Thank you!
  2 Comments
Image Analyst
Image Analyst on 22 Mar 2016
Do you just want to display the curves? Or do you want the individual data points to draw slowly, like it's an animation?
Cheetezz
Cheetezz on 23 Mar 2016
Individual data points to show up slowly, creating an animation of trajectories from starting point to the end point

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 22 Mar 2016
Try something like this:
y = rand(3, 20); % Generate random sample data.
x = rand(size(y, 1), size(y, 2));
% Now we have x and y sample data and we can begin.
% Extract into separate arrays
x1 = sort(x(1,:));
x2 = sort(x(2,:));
x3 = sort(x(3,:));
y1 = y(1,:);
y2 = y(2,:);
y3 = y(3,:);
for k = 1 : length(x1)
plot(x1(1:k), y1(1:k), 'r*-', 'LineWidth', 2);
xlim([min(x(:)), max(x(:))]);
ylim([min(y(:)), max(y(:))]);
grid on;
hold on;
plot(x2(1:k), y2(1:k), 'g*-', 'LineWidth', 2);
plot(x3(1:k), y3(1:k), 'b*-', 'LineWidth', 2);
hold off;
fprintf('Plotted points 1 through %d\n', k);
pause(0.8);
end
fprintf('Done!\n');
  4 Comments
Cheetezz
Cheetezz on 23 Mar 2016
I am still unclear about that. Won't that erase particle A's trajectory before the particle B's trajectory is drawn? I would like to plot all the particles simultaneously at a certain time point, and I thought that the use of for loop across the number of particles will just plot the animation sequentially rather than simultaneously. I may be thinking about this wrongly, but it would be great if you can provide me an example of what you thought would work!
Utkarsh Anand
Utkarsh Anand on 27 May 2020
You can use a structured approach to create 'n' empty cells; something like::
Particles = struct();
Cheers

Sign in to comment.

Categories

Find more on Animation 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!