how can i make this animation faster in MATLAB?
23 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
r = 1;
hmax = 28;
n = 1000;
h = linspace(0,hmax,n);
t = linspace(0,360,n);
k = 0;
pt = 1/6000;
x = zeros(length(h),length(t));
vx = x;
y = x;
vy = y;
a = r+h;
b = r+hmax/2*(cosd(t).^2)+hmax/2;
for i = 1:n
x(i,:) = a(i)+r.*cosd(t);
y(i,:) = b(i)+r.*sind(t);
vx(i,:) = r+r.*cosd(t);
vy(i,:) = r+h.*sind(t);
end
figure(3)
ball_bounce1= plot(x,y,'c');
axis([-1 31 -1 31])
grid on
while k < 10
if rem(k,2) == 0
for i = 1:n
set(ball_bounce1,'XData',x(i,:),'YData',y(i,:));
pause(pt)
end
end
if k > 10
break
end
end
2 Commenti
Rik
il 21 Nov 2020
I'm not sure that is possible. You could replace the pause with drawnow, but that shouldn't meaningfully impact code performance. Seeing your code, I expect only lowering the requirements or increasing hardware will work. Do you need every iteration?
Risposta accettata
VBBV
il 21 Nov 2020
Modificato: VBBV
il 21 Nov 2020
hmax = 25; % max height
n = 100; % make this value smaller
h = linspace(0,hmax,n);
t = linspace(0,360,n);
k = 0;
pt = 1/6000; %
...
figure(3)
ball_bounce1= plot(x,y,'c','linewidth',3);
Use the values above and try again
12 Commenti
VBBV
il 25 Nov 2020
You can also use fanimator function for animating circle. See the resource below
Più risposte (2)
per isakson
il 22 Nov 2020
Modificato: per isakson
il 22 Nov 2020
"[...] to make a ball bounce back and forth between to walls" This is as fast as it gets - I think (with m-code)
%%
x=1;y=1;
ball_bounce1= plot(x,y,'o');
ball_bounce1.MarkerSize = 24;
axis([-1 31 -1 31])
grid on
N = 400;
for jj = 1 : N
set( ball_bounce1,'XData',jj*30/N, 'YData',jj*30/N );
drawnow
end
0 Commenti
CHENG QIAN LAI
il 26 Nov 2020
Modificato: CHENG QIAN LAI
il 26 Nov 2020
% If you draw x,y ,you will get 1000 Line objects.
% (ball_bounce1=1000 Line obj)
ball_bounce1= plot(x,y,'c');
numel( ball_bounce1 )
% Set all line objects(ball_bounce1) to same position, this will slow down the program.
set(ball_bounce1,'XData',x(i,:),'YData',y(i,:));
r = 1;
hmax = 28;
n = 1000;
h = linspace(0,hmax,n);
t = linspace(0,360,n);
phi = linspace(0,360,50); % Reduce array elements
k = 0;
pt = 1/6000;
x = zeros(length(h),length(phi));
vx = x;
y = x;
vy = y;
a = r+h;
b = r+hmax/2*(cosd(t).^2)+hmax/2;
x = a'+ r.*cosd(phi);
y = b'+ r.*sind(phi);
%for i = 1:n
%x(i,:) = a(i)+r.*cosd(phi);
%y(i,:) = b(i)+r.*sind(phi);
%vx(i,:) = r+r.*cosd(t);
%vy(i,:) = r+h.*sind(t);
%end
figure;
ball_bounce1= plot(0,0,'c'); % ball_bounce1 = 1x1 Line
line(a,b);
axis([-1 31 -1 31])
grid on
step=1;
k=1;
while k < 10
for i = 1:step:n % You can try step = 2
set(ball_bounce1,'XData',x(i,:),'YData',y(i,:));
pause(pt) % or drawnow
end
k=k+1;
end
0 Commenti
Vedere anche
Categorie
Scopri di più su Animation in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!