How do I plot N arrays at the same time as a animation?

I want to know the best way to animate several arrays into one plot, at the same time.
The example I'm working on is a closed box of balls, where I already have the arrays created. However the animation becomes very bad and choppy when there's more than one ball in animation, so I wanted to know if there's a better way of writing the code, or a more effiecent way to animate the arrays.
for i = 1:N_max
for j = 1:n
ball(j) = plot(x(j,i+1),y(j,i+1),'ok','MarkerFaceColor','k');
set(ball,'MarkerSize',diameter)
pause(1e-3)
if i~=N_max
reset(ball(j))
end
end
end

Answers (1)

I am not certain what you want to do. There are several functions to produce animated plots in MATLAB. One such is drawnow, some related ones are refreshdata, and there are others.
Example —
[Xs,Ys,Zs] = sphere;
figure
for k1 = 1:0.03:3.2
Xp = Xs + cos(k1*2*pi);
Yp = Ys + sin(k1*2*pi);
surf(Xp, Yp, Zs)
axis equal
axis([-3 3 -3 3 -3 3])
text(-2.1, -1.8, 'Revolving Sphere')
% view(0,90)
grid on
refreshdata
drawnow update
end
hold off
This does not directly address the particle animation in your Question (since I cannot run your code), however it shows what is possible.

1 Comment

Okey, I didn't want to add to much code to my question, but maybe it is necessary. So this is the code I have now (which still lacks on many fronts I my add).
As you can now see, at least for me, the plot is quite bad with n > 1.
%%
clc; clear all; close all;
dt = 1e-2;
N_max = 500;
n = 3;
diameter = 7; %Markersize
x = zeros(1,N_max);
y = zeros(1,N_max);
vx = zeros(1,N_max);
vy = zeros(1,N_max);
for i = 1:n
% Initial values
x0(i,1) = randi([-2000,2000])/1000; % gives value between -2 and 2
y0(i,1) = randi([-2000,2000])/1000;
vx0(i,1) = randi([-5000,5000])/1000;
vy0(i,1) = randi([-5000,5000])/1000;
x(i,1) = x0(i,1);
vx(i,1) = vx0(i,1);
y(i,1) = y0(i,1);
vy(i,1) = vy0(i,1);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Plot the box
figure;hold on;
square_length = 2;
axis([-square_length square_length -square_length square_length])
axis off
axis square
cornersX = [-square_length, square_length];
cornersY = [-square_length square_length];
for j = 1:2
plot([cornersX(j),cornersX(j)],[cornersY(1),cornersY(2)],'-k'); % | |
plot([cornersX(1),cornersX(2)],[cornersY(j),cornersY(j)],'-k'); % _ _
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Define the corners
corner = [square_length-1e-3, square_length-1e-3];
for j = 1:n
for i=1:N_max
x(j,i+1) = x(j,i) + vx(j,i)*dt;
y(j,i+1) = y(j,i) + vy(j,i)*dt;
vx(j,i+1) = vx(j,i);
vy(j,i+1) = vy(j,i);
position = [x(j,i+1),y(j,i+1)];
if norm(position) >= norm([corner(1),corner(2)])-dt % if it hits a corner
vy(j,i+1) = -vy(j,i);
vx(j,i+1) = -vx(j,i);
elseif abs(y(j,i+1)) >= square_length - dt % if it hits horizontal wall
vy(j,i+1) = -vy(j,i);
elseif abs(x(j,i+1)) >= square_length - dt % if it hits vertical wall
vx(j,i+1) = -vx(j,i);
end
end
end
% Time for plot
for i = 1:N_max
for j = 1:n
ball(j) = plot(x(j,i+1),y(j,i+1),'ok','MarkerFaceColor','k');
set(ball,'MarkerSize',diameter)
pause(1e-3)
if i~=N_max
reset(ball(j))
end
end
end

Sign in to comment.

Categories

Asked:

on 3 Feb 2020

Commented:

on 4 Feb 2020

Community Treasure Hunt

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

Start Hunting!