In: Computer Science
In python.
Projectile motion: Write a python program that will ask the user for
an initial height y0, initial velocity v, launch angle theta, and mass m.
Create two functions, one that will calculate max height
of the projectile, and one that will calculate the range. Ask the
user which one he/she would like to calculate, then present them with
the answer. (use kg, m and m/s)
CODE:
import math
def getMaxHeight(y0, v, theta):
#converting theta to radians
theta = (math.pi/180.0)*theta
#Max Height = vsin(theta)^2/2g, where g = 9.8 m/s^2
g = 9.8
#calculating the maxHeight
maxHeight = pow(v*math.sin(theta),2)/(2*g) + y0
#returns the maxHeight
return maxHeight
def getRange(y0, v, theta):
#converting theta to radians
theta = (math.pi/180.0)*theta
#Max Height = vsin(theta)^2/2g, where g = 9.8 m/s^2
g = 9.8
#finding the time of flight
#formula is after solving the quadratic equation
#-(0.5)gt^2 + v*cos(theta)t + y0
#after solving for t we get the time of flight
time1 = -(-v*math.sin(theta) + pow(pow(v*math.sin(theta),2) +
4*y0*4.9,0.5))/9.8
time2 = -(-v*math.sin(theta) - pow(pow(v*math.sin(theta),2) +
4*y0*4.9,0.5))/9.8
time = 0
if(time1<0):
time = time2
else:
time = time1
#time*velocity*cos(theta) we get the range
return v*math.cos(theta)*time
#asking the user inputs
y0 = float(input('Enter initial height in metres: '))
v = float(input('Enter the initial Velocity in m/s: '))
theta = float(input('Enter launch angle in degrees: '))
#asking choice from the user
choice = int(input('Enter\n1. to calculate max height\n2. to
calculate range: '))
if(choice == 1):
print('\nMax Height: '+str(round(getMaxHeight(y0,v,theta),2))+'
metres')
elif(choice == 2):
print('\nRange: '+str(round(getRange(y0,v,theta),2))+'
metres')
else:
print('Invalid Choice!')
______________________________________
CODE IMAGES:
__________________________________________________
OUTPUT:
_____________________________________________
Feel free to ask any questions in the comments section
Thank You!