0% found this document useful (0 votes)
71 views

Matlab Animation

This MATLAB code simulates the trajectory of a cricket ball thrown by a bowler. It calculates the ball's velocity based on torque applied by the bowler's arm and releases angle. The code uses equations of motion to model the ball's path in two parts - before and after bouncing - tracking position over time. It plots the full trajectory and can animate the movement, skipping the bounce for a "full toss" delivery that does not hit the ground.

Uploaded by

Haseeb Rayhan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views

Matlab Animation

This MATLAB code simulates the trajectory of a cricket ball thrown by a bowler. It calculates the ball's velocity based on torque applied by the bowler's arm and releases angle. The code uses equations of motion to model the ball's path in two parts - before and after bouncing - tracking position over time. It plots the full trajectory and can animate the movement, skipping the bounce for a "full toss" delivery that does not hit the ground.

Uploaded by

Haseeb Rayhan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

function animation(torque, angle)

%set parameters with data given


e = 0.6;
g = -9.81;
s = -2.4;
I=0.64;
armlength = 0.8;
omega = sqrt((2*torque*225*(pi/180))/I);
%tangential velocity
lin_v = omega*armlength;
%all velocities below are in vectors
v_bowler = [10 0];
%ball velocity with respect to bowler
v_ball = [lin_v*cosd(angle) -lin_v*sind(angle)];
%absolute ball velocity
v_ball_abs = v_bowler + v_ball;
%using equations of motion to calculate v right before impact
v_bef_impact = [v_ball_abs(1) -sqrt(v_ball_abs(2)^2+2*g*s)];
%using coefficient of restitution, e, to find velocity after impact
v_aft_impact = [v_bef_impact(1) -v_bef_impact(2)*e];
%sy and sx are conditions to test when the bounce is occuring (init to zero
%here for convenience)
sy=0;
sx=0;
%large vector to store co-ords through simulation
s = zeros(1000, 2);
%iteration variable to keep track of how much of the vector we'll need
%to plot/animate at the end
i=1;
%start at t=0
t=0;
%small time step so that bounce is 'seamless', ie height reaches 0 almost
%exactly
dt=0.00005;
%calculation of co-ords before bounce occurs
while (sy>=0 && sx<=20)
%s = ut + 0.5at^2 for y
s(i, 2) = 2.4 + v_ball_abs(2)*t + (0.5)*(g)*(t^2);
%s = ut for x
s(i, 1) = v_ball_abs(1)*t;
%update x checking variable (end of pitch)
sx=s(i,1);
%update time
t=t+i*dt;
%set checking variable to current y to see if we have reached the
%ground (bounce)
sy=s(i,2);
%update vector index
i=i+1;
end
%this is a fulltoss if 20m is reached without a bounce
fulltoss=(~(sx<=20));
%smaller time step for after the bounce (increases simulation speed)
dt=0.0005;
%breakpoint in the simulation, seperating before and after bounce
breakpoint=i;
%set up new equations and parameters of motion for 2nd part of sim
t=0;
%new iteration variable k to restart time step from 0
k=0;

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

You might also like