In: Computer Science
write a python program that will search through a range of launch angles (this will require a loop statement)
the target is 500 meters away and the projectile was launched at 100m/s, the initial height of the projectile can be between 2 and 30 meters
the print statement will state the initial launch angle required to hit the target from the projectile height.
***explain each step or a rating will not be given***
CODE:
import math
#function to get the range of a projectile
#height = passing the initial height of the projectile
#v = initial velocity
#theta = angle at which the projectile is fired
def getRange(height, 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 we get 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*height*4.9,0.5))/9.8
time2 = -(-v*math.sin(theta) - pow(pow(v*math.sin(theta),2) +
4*height*4.9,0.5))/9.8
time = 0
#using the time which is positive
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 to enter the initial height of the
projectile
height = float(input('Enter the initial height of the projectile
between 2m to 30m: '))
#velocity = 100m/s
v = 100
#targetRange
targetRange = 500.0
theta = 0.0
#running a loop
while(True):
#if the range of the current projectile is greater than equal to
targetRange
if(getRange(height, v, theta) >= targetRange):
#we have our answer, and the angle is printed. The loop
breaks
print('The projectile hits the target at angle {} degrees when
fired from initial height {} meters'
.format(str(round(theta,2)), height))
break
#after each loop the angle is incremented by 0.1
theta += 0.1
___________________________________
CODE IMAGES AND OUTPUT:
_________________________________________________
Feel free to ask any questions in the comments section
Thank You!