INTRODUCTION

When throwing a ball, we wonder how far it will fly and how high it will rise. Both distance and height depend on many factors, such as:

  • initial speed,
  • gravitational acceleration,
  • throw angle,
  • air resistance,
  • wind strength and direction.

If we omit many factors and adopt a fairly simplified model, we can conduct an experiment.

If we only consider:

  • acceleration due to gravity as gravitational constant $g=9,8 \frac{m}{s^2}$,
  • initial velocity (the speed at which the ball is thrown) in m/s,
  • throw angle in degrees

then we can:

  1. from the formula $s=\frac{v^2 \cdot sin 2\alpha}{g}$ set the throw distance,
  2. from the formula $h=\frac{v^2 \cdot sin^2 \alpha}{2g}$ determine the maximum height that the ball will reach.

where:

  • $v$ – initial speed
  • $\alpha$ – throw angle (in radians)
  • $g$ – gravitational constant – gravitational acceleration

MWe can simulate how the throw distance and height will change if we change the throw angle.

It is obvious that if:

  • the starting angle is small, both the height and the distance are small – we throw almost flat, so the ball will not rise high and will not fly far,
  • the initial angle is large (we throw almost vertically), then the height may be large, but the distance is small.

So how do you choose the angle so that the distance is the greatest?


PYTHON CODE

import math

g = 9.8  # acceleration due to gravity in m/s^2

def calculate_horizontal_distance(initial_velocity, angle):
    angle_rad = math.radians(angle)
    distance = (initial_velocity**2 * math.sin(2 * angle_rad)) / g
    return distance

def calculate_max_height(initial_velocity, angle):
    angle_rad = math.radians(angle)
    height = (initial_velocity**2 * math.sin(angle_rad)**2) / (2 * g)
    return height

initial_velocity = float(input("Enter the initial velocity in m/s: "))
distances = []
heights = []

for angle in range(5, 81, 5):
    distance = calculate_horizontal_distance(initial_velocity, angle)
    height = calculate_max_height(initial_velocity, angle)
    distances.append(distance)
    heights.append(height)

print("Angle (degrees)\tDistance (m)\tMax. Height (m)")
print("-----------------------------------------------")
for i in range(len(distances)):
    distance_format = '{:.2f}'.format(distances[i]).replace('.', ',')
    height_format = '{:.2f}'.format(heights[i]).replace('.', ',')
    print(f"{(i+1)*5}\t\t{distance_format}\t\t{height_format}")

⬆️⬆️⬆️ See in Google Colaboratory


HOW THE CODE WORKS?

  1. Import the necessary math module for mathematical operations.
  2. Define the value of g as 9.8, representing the acceleration due to gravity in meters per second squared.
  3. Define two functions:
  4. calculate_horizontal_distance(initial_velocity, angle): Calculates the horizontal distance of the projectile given the initial velocity and angle of projection. It uses the formula d = (v^2 * sin(2θ)) / g, where v is the initial velocity and θ is the angle in radians.
  5. calculate_max_height(initial_velocity, angle): Calculates the maximum height reached by the projectile given the initial velocity and angle of projection. It uses the formula h = (v^2 * sin(θ)^2) / (2 * g).
  6. Prompt the user to enter the initial velocity in meters per second.
  7. Initialize empty lists to store the calculated distances and heights.
  8. Iterate over angles from 5 to 80 (inclusive) in steps of 5:
  9. Calculate the distance and height for each angle using the defined functions.
  10. Append the calculated values to the respective lists.
  11. Print a table header to display the angle, distance, and maximum height.
  12. Iterate over the distances and heights lists using the range function and the len function to determine the number of elements:
  13. Format each distance and height value to two decimal places and replace the decimal point with a comma.
  14. Print each angle, distance, and height value in a formatted manner, using tabular spacing.
  15. The program ends, and the table of angles, distances, and maximum heights is displayed based on the user’s input of the initial velocity.