how can i make this animation faster in MATLAB?

14 ビュー (過去 30 日間)
Jason Robert
Jason Robert 2020 年 11 月 21 日
編集済み: CHENG QIAN LAI 2020 年 11 月 26 日
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 Center および File ExchangeAnimation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by