Euler, Drag, and How Far Can You Throw a Ball?

Associated Files

  • detailed PDF on using Euler’s method to calculate trajectories and drag (air resistance).
  • Python script (ThrownBallEulerStudent.py – scroll down to see the script. You can copy and paste it into a Python compiler).

Description of scenario.  If a tennis ball is thrown through the air it will eventually hit the ground due to gravity.

If you can throw a tennis ball 12 meters/second (about 26.8 mph) how far can you throw it;  meaning how far away from you can you make it land?

Assume that when the ball leaves your hand it is at a height of 2 meters (about 6 feet). Your task is to find the best angle to throw the ball, so that it will land the furthest from you.

The Python script ThrownBallEulerStudent.py (shown below) solves this problem if we neglect drag (air resistance). The Python script outputs a graph of the ball’s trajectory and the solution:

Output from ThrownBallEulerStudent.py

Assuming drag = 0. If when you throw a tennis ball you release it at a height of 2 meters and a speed of 12 meters/second, and you want it to land furthest from you, you should throw the ball at an angle of 42 degrees: it will land about 16.6 meters away.

The PDF  Using Euler to Simulate a Thrown Ball – Student Version (click to open PDF)
explains how to solve this problem using Euler’s method, and gives suggestions on how to implement the algorithm in Python.  The PDF also discusses how to include drag (air resistance).

Your project is to solve this problem if we include drag (air resistance). With the help of Euler’s Method, write a short script (in Python) to find the ideal launch angle to throw the ball, so that it will result in the ball landing as far as possible from you. Also find that maximal distance and plot that trajectory. Make sure to include air resistance (drag) in your model. The height you launch the ball at is 2 meters and the speed is 12 meters/second. Don’t worry about rotational effects.
What to do? Read the PDF.  Run the Python code (below).  Once it is working, change the Python code so that it takes into account drag. Also change the wording of the plot’s title to be:  Your name. Ball trajectory. With drag.  Change the beginning part of the Python’s script’s output to say:   With drag.
What to hand in? Hand in one sheet of paper. Copy the graph (with your name in the title) and the solution from Python’s output and paste them into a single document (E.g. Word, OpenOffice, etc.). Print that out. Hand in the print out.

You can run the following python code (just copy and paste it):

Python code:  ThrownBallEulerStudent.py

# Written by Chris McCarthy July 2019 SIMIODE DEMARC
# Drag = 0 Student Version 
#===========================================  for online compilers
import matplotlib as mpl
mpl.use('Agg')
#===========================================  usual packages
import numpy as np
from matplotlib import pyplot as plt
plt.rcParams.update({'font.size': 22})
#============================================ constants
g = 9.8    # gravitation
m = 0.058  # mass tennis ball in kg
#============================================
class Ball:
    def __init__(self, x,y,vx,vy,t):
        self.x = x
        self.y = y
        self.vx = vx
        self.vy = vy
        self.t = t
    def update_ball(self, delta_t,g):
        self.x = self.x + delta_t*self.vx
        self.y = self.y + delta_t*self.vy
        self.vx = self.vx 
        self.vy = self.vy + delta_t*(-g )
        self.t = self.t + delta_t
#============================================ initial conditions
x0 = 0      # initial x position in meters
y0 = 2      # initial y position in meters
t0 = 0      # initial time in seconds
speed = 12  # initial speed of the ball in meters/sec        
#============================================ Delta t
dt = .001 # Delta t
#============================================ Delta t
xDistance = []   # store the horizontal distance ball travelled
Theta = []       # store the angle the ball is thrown at
#============================================ run Euler for theta in [0, 90]
for theta in range(0, 91):
    vx0 = speed*np.cos(theta * np.pi/180) # initial vx
    vy0 = speed*np.sin(theta * np.pi/180) # initial vy
    ball = Ball(x0 , y0,  vx0, vy0, t0)   # initialize ball object
    while 0 <= ball.y:                    # Euler Method applied to that ball
        ball.update_ball(dt, g)
    xDistance.append(ball.x)             # collect x value when ball hits ground 
    Theta.append(theta)                  # collect theta 
#============================================ find max x distance over theta, print it
maxpos = xDistance.index(max(xDistance))
#============================================= run Euler (again) for best theta
best_theta = Theta[maxpos]
best_vx0 = speed*np.cos(best_theta * np.pi/180) # initial vx
best_vy0 = speed*np.sin(best_theta * np.pi/180) # initial vy
best_ball = Ball(x0 , y0,  best_vx0, best_vy0, t0)   # initialize ball object
xvalues = [x0]
yvalues = [y0]
times =   [t0]
while 0 <= best_ball.y:                    # Euler Method applied to that ball
    best_ball.update_ball(dt, g)
    xvalues.append(best_ball.x)
    yvalues.append(best_ball.y)
    times.append(best_ball.t)
#============================================= 
print(' ')
print('Assuming drag = 0. If when you throw a tennis ball you release it at a height of', y0, 
       ' meters and a speed of', speed, 
       'meters/second, and you want it to land furthest from you,',
      'you should throw the ball at an angle of', Theta[maxpos], 
      ' degrees: it will land about', np.round(max(xDistance),1),'meters away.' )
#============================================= plot best trajectory
plt.plot(xvalues, yvalues, 'r-',linewidth=7.0)
plt.grid(linewidth='3', color='black')
plt.title('Prof. McCarthy. Ball Trajectory. Without Drag', fontsize = 18)
plt.savefig('NoDragGraph.png')