MATLAB: How can i make this animation faster in MATLAB

animationMATLAB

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

Best Answer

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