how can i make this animation faster in MATLAB?

50 次查看(过去 30 天)
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 个评论
Rik
Rik 2020-11-21
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?
Jason Robert
Jason Robert 2020-11-21
also is there a way you can help me make the code into cos^2 animation

请先登录,再进行评论。

采纳的回答

VBBV
VBBV 2020-11-21
编辑:VBBV 2020-11-21
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 个评论
VBBV
VBBV 2020-11-22
编辑:VBBV 2020-11-22
change this line in your program , i think it works for n = 1000 also
figure(3)
ball_bounce1= plot(x(1:500:end),y(1:500:end),'c','linewidth',3);
VBBV
VBBV 2020-11-25
You can also use fanimator function for animating circle. See the resource below

请先登录,再进行评论。

更多回答(2 个)

per isakson
per isakson 2020-11-22
编辑:per isakson 2020-11-22
"[...] 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

CHENG QIAN LAI
CHENG QIAN LAI 2020-11-26
编辑:CHENG QIAN LAI 2020-11-26
% 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

类别

Help CenterFile Exchange 中查找有关 Animation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by