필터 지우기
필터 지우기

how can i make this animation faster in MATLAB?

조회 수: 23 (최근 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 CenterFile Exchange에서 Animation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by