In: Computer Science
Red, our Angry Bird friend, will be shot from a catapult again. This time, you’ve been given the equation that projects his trajectory based on his initial velocity (v0 ), his angle of launch (?), and the acceleration due to gravity (g). y = (xtan(theta)) - (gx^2) / (2v0^2 * cos^2(theta))
You’ve been informed that the wall he must knock down is 202.3 m away.
a. Take user input for Red’s initial velocity (for testing, start with 45 m/s).
b. For the input velocity, at what angle must your catapult be set to reach the wall at a height of 1 m (+/- 0.1m)? Use Earth’s gravity. Start at 0 degrees, and increase the angle until the required condition is met. Not all initial velocities will have a solution; in these cases, tell the user there is no solution for Red.
c. Plot his trajectory. Do not create a plot if there is no solution for the velocity the user input.
d. This was on Earth, what if we move to Mars?
e. Report to the user the angle you calculated and the height of impact for both Earth and Mars.
PYTHON
angle isnt given. trajectory equation that includes angle, initial velo, gravity is given. The minimum angle needed to hit the wall one meter above the ground must be found, i believe through iteration. I just dont know how.
Let's handle the tasks one part at a time. Tere are total of 5 parts to this problem.
a & d)
This part is simple. We are just expected to take the user inputs. Over here I have also included the code required for Part d where we let the user choose between the gravity of Earth and Mars. The code is as follows:
v0 = float(input("Initial Velocity: "))
x = float(input("Distance of the wall: "))
loc = input("Where are we?\n\t1) If Earth enter 1\n\t2) If Mars enter 2\n")
if loc == '1':
g = 9.8
elif loc == '2':
g = 3.7
b )
Part b is the main part of the problem. I will first be sharing the code and then explaining the steps involved.
import math
flag = False
for deg in range(0,90):
rad = deg * math.pi / 180
xTanTheta = x * math.tan(rad)
gxSquare = g * (x**2)
v0SquareCosSquareTheta = 2 * (v0**2) * (math.cos(rad)**2)
y = xTanTheta - (gxSquare / v0SquareCosSquareTheta)
if 0.9 <= y <= 1.1:
flag = True
print("Degree:",deg)
break
if flag == False:
print("There is no solution")
Import library math for mathematical functions.
flag is to determine if we have a solution to the inputs we have provided or not.
The for loop iterates through the value 0 to 90 which are the values in degree.
rad is the value of the angle in radian which is calculated by multiplying the degree by pi divided by 180.
Now, I have divided the entire equation into smaller parts. This makes it easy to look and understand the equation.
The above divided parts of the equation are brought together and stored in the variable y where y is equal to, xTanTheta - (gxSquare / v0SquareCosSquareTheta).
Now as we have calculated the y (height) for our angle (deg), we check if it reaches the wall at a height of 1 m (+/- 0.1m). If it did we set the flag as True, as we do have a solution to the equation and break the loop as we don't need to iterate any further.
Now we check for flag to determine if we have a solution or not. If we do not have a solution then print it out to the user
c and e)
Part c is related to plotting. For this we will need two libraries, matplotlib and numpy
import matplotlib.pyplot as plt
import numpy as np
if flag:
initial_array = np.arange(int(x)+2)
rad = deg * math.pi / 180
vecXTanTheta = initial_array * math.tan(rad)
vec_gxSquare = g * (initial_array**2)
v0SquareCosSquareTheta = 2 * (v0**2) * (math.cos(rad)**2)
vec_Y = vecXTanTheta - (vec_gxSquare / v0SquareCosSquareTheta)
plt.figure(figsize=(20,8))
plt.title("Trajectory Plot")
plt.xlabel("Distance")
plt.ylabel("Height")
plt.plot(vec_Y)
plt.show()
print("Angle of launch: ", deg, "Degree")
print("Height of impact: ", y, "meters")
If we have a solution, that is, we have flag is equal to True then we will execute the code.
Here we have to plot the trajectory. For plotting the trajectory we need multiple points with their x and corresponding y coordinates.
We will use NumPy to set the points for x-axis. We will simply create a NumPy array, initial_array with points starting from 0 to the distance of the wall plus 1.
Variable rad is again the conversion of degree that we have obtained to radian.
vecXTanTheta is the vectorized form xTanTheta. Here instead of a single variable x we use the NumPy array initial_array.
vec_gxSquare is the vectorized form of gxSquare.
As v0SquareCosSquareTheta does not involve x we don't have to vectorize it.
Now we calculate the y (height) at each and every point of x for our degree deg. We store the value in the variable vec_Y.
In order to plot we will be using matplotlib.pyplot as plt. It is extremely easy to print NumPy vectors using matplotlib. We simply have to call plt.plot(vec_Y) and this will plot the trajectory for us.
After that we complete Part e by printing the value of degree and point of impact to the user.
I hope this resolves your doubt. If any question do let me know. Regards.