You are on page 1of 6

P ROGRAMMING I

P ROJECT 1, D UE D ATE M ONDAY, O CTOBER 12, 2015 - 11:59 PM

Projectile Motion

September 28, 2015

Years ago the Pakistani cricket team preferred cricket to betting and match-fixing. It is common knowledge that a cricket ball is a classical particle which obeys Newtons laws. However,
what is not common knowledge is the fact that the successs of the Pakistani cricket hinged
on their ability to predict the motion of a cricket ball using Newtonian mechanics. For that
purpose, the team used to have a resident physicist, who would lecture them on projectile
motion. In fact it is rumoured that after a heroic struggle with projectile motion, the great
Khan himself admitted that the only regret he has in his life is that he did not study physics
at Oxford 1 . Unfortunately, times changed and evil began to prevail and the ancient art of
Newtonian mechanics got lost. However, its practitioners of which you are one, who call
themselves Newtonians, did not die, but just went into hiding. To make matters worse, the
intellectual abilities of the the national cricket team have sunk to such a low level that there is
no hope of teaching the dunces anything about projectile motion. Your only hope is that you
can write a computer programme, which will help ordinary people gain some understanding about projectile motion. That would begin to change the intellectual environment of the
country so that a few centuries after you are dead, the cricket team would be chosen from a
population where Newtonian physics is common knowledge. It is a meagre hope, but that is
all you have.
1. So what happens when a cricket ball is hit? For the moment, model a cricket ball as a
particle that moves along a single dimension with some initial position u, some initial
velocity v, and some initial acceleration a. The equation for the position of the ball at
time t , given a, v, and u is u t = 1/2 at 2 + v t + u. Note that this denotes a first order
differential equation in time. Later, we can apply this equation to either the horizontal
1 To his credit, he never gave up till he became an expert in Newtonian mechanics

(x) component of ball motion, or the vertical (y) component of ball motion. Write a
procedure that takes as input values for a, v, u, and t and returns as output the position
of the ball at time t .

def position(a, v, u, t):


YOUR-CODE-HERE
Test your position code for at least the following cases:

>>>
0
>>>
20
>>>
60
>>>
>>>

position(0, 0, 0, 0)
position(0, 0, 20, 0)
position(0, 5, 10, 10)
position(2, 2, 2, 2)
position(5, 5, 5, 5)

In addition, you should add some test cases of your own to these to cover other boundary and typical conditions.
2. One of our goals is to determine how far a ball will travel in the air, if it is hit with
some initial velocity at some initial angle with respect to the ground. To do this, we
will need to know when the ball hits the ground, and for that well want to find when
the y coordinate of the balls position reaches zero. This can be discovered by finding
the roots of the y position equation, and selecting the one that is larger (later in time).
The proper tool for this is the quadratic formula. Given the coefficients of the quadratic
equation az 2 + bz + c = 0, write a procedure to find one of the roots (call this root1),
and another procedure to find the other root (call this root2).

def root1(a, b, c):


YOUR-CODE-HERE
def root2(a, b, c)
YOUR-CODE-HERE
You may notice that, depending on how you wrote your procedures, for some test cases
you get an error. For example, try root1(5, 3, 6). What happens? If you get an error,
which is likely if you wrote your code the straightforward way, figure out how to change
it so that your procedure returns a false value in those cases where there is not a valid
solution.
3. Given an initial upward velocity (in meters per second, or m/s) and initial elevation
or height (in meters, or m), write a procedure that computes how long the ball will
be in flight. Remember that gravity is a downward acceleration of 9.8m/s 2 . Note that
to solve this you will need a root of a quadratic equation. Try using root1, and using
root2. Only one of these solutions makes sense. Which one? And why? Use this to
create a correct version of the procedure below.

def time_to_impact(init_vert_velocity, elevation):


YOUR-CODE-HERE
In some cases, we may want to know how long it takes for the ball to drop to a particular
height, other than 0. Using your previous procedure as a template, write a procedure
that computes the time for the ball to reach a given target elevation.

def time_to_height(init_vert_velocity, elevation, target_elevation):


YOUR-CODE-HERE
4. Suppose the ball is hit with some velocity v, at a starting angle relative to the horizontal (in degrees), and from an initial elevation (in meters). We wish to compute the
distance in the horizontal direction the ball will travel by the time it lands. Remember
that some of the velocity vector goes into the x direction, and some into the y. Checking the Python manual, you will find procedures sin and cos in the math module. To
use these (which require angles in radians rather than degrees), you may also find the
procedure degree2radian useful. It is given below:

def degree2radian(deg):
return deg * pi / 180
Write a procedure travel_distance_simple that returns the lateral distance the ball
thrown with given velocity, angle, and initial elevation will travel before hitting the
ground.

def travel_distance_simple(elevation, velocity, angle):


YOUR-CODE-HERE
Try this out for some values. Using your code, determine the time to impact of a ball
hit at a height of 1 meter with an initial velocity of 45 m/sec, at angles of 0, 45 and
90 degrees (be sure to use radian units or degree units depending on how you provide
input to sin and cos, but remember that those procedures expect arguments in units
of radians). How far does the ball travel in each case? Notice the distance traveled in
S
meters for a ball hit at a 45 degree angle, with this bat speed. Seems incredible A
right? Well come back to this in a little bit.
5. Before we figure out why professional players dont normally hit 200 meter sixes, lets
first see if we can find out the optimal angle at which to launch a ball, in order to have
it travel the furthest. Write a procedure find_best_angle that takes as arguments an
initial elevation and an initial velocity, and which finds the best angle at which to hit
the ball to optimize distance traveled. You will probably want to write a function that
tries different angles between 0 and /2 radians, sampled every 0.01 radians (say) or
between 0 and 90 degrees, sampled every 1 degree (depending on whether your code
works in radians or degrees - either way be sure that you provide the right kind of unit
to your trigonometric functions).

def find_best_angle(velocity, elevation):


YOUR-CODE-HERE
Use this for same sample values of elevation and velocity. What conclusion can you
reach about the optimal angle of hitting?
6. We noticed that the distances traveled by the ball seemed incredible, this is because in
the real world the air friction(drag) slows down the ball. The magnitude f of the air
drag force is approximately proportional to the square of the projectiles speed relative
to the air:
f = Dv2
q
where v = v x 2 + v y 2 . The value of the constant D is given by C A/2 where C is the
drag coefficient (about 0.5 for a cricket ball); is the density of air (about 1.1 kg /m 3 for
Lahore); A is the cross-sectional area of the cricket ball (radius is about 0.035 m).
The direction of ~
f is opposite the direction of the ~
v (given by the unit vector v = ~
v /v),
so we can write
~
f = D v 2 v = D v 2~
v /v = D v~
v
therefore the components of ~
f are
f x = D v v x

f y = D v v y

So how does drag affect motion? Newtons equation basically says that the movement
of the ball will be governed by:
d r ag + g r avi t y = mass accel er at i on
or in other words the force on the ball is equal to the sum of two force acting on it. The
drag force and the force of gravity. So in vector notation we can write it as
D v~
v +~
g = m~
a
where m is the mass of the ball, ~
g = 9.8 m/s 2 2 However this is a vector equation and
we can break it up into two parts. We already know the x and y components of the drag
force and gravity on acts in the y component. This gives us the following two equations
D v v x = ma x

(0.1)

D v v y 9.8m = ma y

(0.2)

We know that for a cricket ball m = 0.16kg . How can we compute the distance traveled by the ball, but taking into account this drag effect? If we were mathematicians we

2 Assuming that the y-coordinate points upwards away from the surface of the earth.

would have four coupled differential equations to solve, but since we are computer scientists we are going to solve them numerically. We have the following approximations:
x = v x t
y = v y t
ax =
ay =

v x
t
v y
t

Substituting the values for a x and a y in equations 0.1 and 0.2 we get
x = v x t
y = v y t
1q 2
v x + v 2y v x t
v x =
m
1q 2

v y =
v x + v 2y v y g t
m
We also have some initial conditions on these parameters
x t =0 = 0
y t =0 = h
v x t =0 = V cos
v y t =0 = V sin
where is the angle of the initial hit with respect to the ground, V is the initial speed,
and h is the initial height of the ball. To find the distance traveled, we need to integrate
these equations. That is, starting with the initial values, we want to move forward a
small step in time (say 0.01 seconds), and compute the change in x and y, given the
current estimates for velocity. This will gives us the new values of x and y. Similarly,
we want to compute the change in v x and v y , and thus, the new values for v x and v y .
We can keep recursively estimating these values until the value for y drops below 0,
in which case the value for x tells us the distance traveled. You can assume that the a
batsmen can hit the ball in the range of 35m/s to about 50m/s.
Based on this idea, write a procedure called integrate, which performs this computation. Using this, write a procedure called travel-distance, which given an initial
elevation, an initial magnitude for the velocity, and an initial angle of launch, computes
the distance traveled while accounting for drag. Use this to determine
a) Whether it is easier to hit a six at deep point versus a six at long on (for a six at deep
point assume that initial elevation is 2 m and the initial angle is 10 degrees and
the distance traveled is 60 m, whereas for a six at long on assume that the initial
elevation is 0.5 m, initial angle is 45 degrees and the distance to the boundary is
75 m)?

b) How easy is it to hit a six out of Gaddafi stadium (radius roughly 130 m)? For what
range of angles will the ball land outside the stadium?
c) How do your answers to parts (a) and (b) change, if cricket is being played at a
stadium with a = 1.4 kg /m 3 .

You might also like