MATLAB Answers

how can i make this animation faster in MATLAB?

4 views (last 30 days)
Jason Robert
Jason Robert on 21 Nov 2020
Edited: CHENG QIAN LAI on 26 Nov 2020
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 Comments

Rik
Rik on 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?
Jason Robert
Jason Robert on 21 Nov 2020
also is there a way you can help me make the code into cos^2 animation

Sign in to comment.

Accepted Answer

VBBV
VBBV on 21 Nov 2020
Edited: VBBV on 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 Comments

Show 9 older comments
Jason Robert
Jason Robert on 22 Nov 2020
What do i put in the while loop. U said "while...." what do i put there
VBBV
VBBV on 22 Nov 2020
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 on 25 Nov 2020
You can also use fanimator function for animating circle. See the resource below

Sign in to comment.

More Answers (2)

per isakson
per isakson on 22 Nov 2020
Edited: per isakson on 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 Comments

Sign in to comment.


CHENG QIAN LAI
CHENG QIAN LAI on 26 Nov 2020
Edited: CHENG QIAN LAI on 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 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!