Professional Documents
Culture Documents
Matlab Animation
Matlab Animation
i=i-1; %(this undoes the extra iteration at the end of the last while loop)
%use sx as a condition to terminate once 20m down the pitch
sx=s(k+i,1);
%set x distance at end of first portion of sim to init x distance at start o
f
%second portion of sim
x_dist = sx;
%x_dist is distance already travelled from first sim
%only simulate bounce if a fulltoss has not occured
if (~fulltoss)
while (sx<=20) %while less than 20m down pitch
%s = ut + 0.5at^2 for y
s(k+i, 2) = v_aft_impact(2)*t + (0.5)*(g)*(t^2);
%s = s0 + ut for x
s(k+i, 1) = x_dist + v_ball_abs(1)*t;
%update t and termination checking variable
t=t+k*dt;
sx=s(k+i,1);
%update matrix index
k=k+1;
end
end
%plot everything
plot(s(1:k+i-1,1), s(1:k+i-1,2), ':', 'LineWidth', 1.2);
title('Projection of a cricket ball');
xlabel('x coordinate');
ylabel('y coordinate');
bline=line('XData',s(1:k+i-1,1),'YData',s(1:k+i-1,2),'Color','r', ...
'Marker','o', 'MarkerFaceColor',[0.75 0 0],'MarkerSize',10);
%now animate the first portion before the bounce (up to breakpoint)
for j=1:(breakpoint-1)
set(bline,'XData',s(j,1),'YData',s(j,2))
drawnow
end
%only plot bounce if fulltoss has not occured
if (~fulltoss)
%animate the second portion after the bounce (after breakpoint)
for j=breakpoint:(k+i-1)
set(bline,'XData',s(j,1),'YData',s(j,2))
drawnow
end
end
%all done!
end