Question

In: Computer Science

Red, our Angry Bird friend, will be shot from a catapult again. This time, you’ve been...

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.

Solutions

Expert Solution

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.

  1. xTanTheta calculates the x tan(theta)
  2. gxSquare calculates the g x^2
  3. v0SquareCosSquareTheta calculates the 2 v0^2 * cos^2(theta)

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.


Related Solutions

The Red Cross honors and recognizesits best volunteers from time to time. One of the...
The Red Cross honors and recognizes its best volunteers from time to time. One of the Red Cross offices has received 12 nominations for the next group of 4 volunteers to be recognized. Seven of these 12 nominated volunteers are females. If Red Cross office decides to randomly select 4 names out of these 12 nominated volunteers, find the probability that of these 4 volunteers exactly 3 are females.
For the past several years, you’ve been purchasing a product from a supplier at a high-volume...
For the past several years, you’ve been purchasing a product from a supplier at a high-volume cost and reselling the product at a lower price than your customers could buy it. However, you’d like to improve the product but the manufacturer isn’t interested in doing this. You are trying to determine if it would make sense to buy the equipment and make it yourself with better quality. Construct two decision trees (one for each scenario), including expected money value, for...
It’s 2014. You’ve been working for 4 years. You’re thinking about getting an MBA (full time...
It’s 2014. You’ve been working for 4 years. You’re thinking about getting an MBA (full time two years). • Current Salary: $80,000 per year • Expected Salary: $110,000 per year • Cost of an MBA: $100,000 How many years to take to pay back the cost after the graduation? Assume 8% of annual rate.
Suppose you know that the amount of time it takes your friend Susan to get from...
Suppose you know that the amount of time it takes your friend Susan to get from her residence to class averages  50 minutes, with a standard deviation of 55 minutes. What proportion of Susan's trips to class would take more than  50 minutes? . Enter your answers accurate to two decimal places. What proportion of her class would take more than  50 minutes? What proportion of Susan's trips to class would take less than 40 minutes? What proportion of Susan's trips to class...
At no time in our history have we been more medicated as a society than we...
At no time in our history have we been more medicated as a society than we are today. Yet one study has shown that 75% of pediatricians are NOT confident in diagnosing ADD/ADHD, let alone psychiatric conditions. Meanwhile, access to specialized services, especially for children, are practically nonexistent. Chapter 7 discusses several cognitive and psychiatric conditions, many of which are often treated with medication. Pretend you are the parent of a child with one of these conditions. You would want...
Demand for copper from the Red Dog mine is steady over time, at p = 1000...
Demand for copper from the Red Dog mine is steady over time, at p = 1000 - q for both this year and next year (where p is the price in dollars per ton and q is the number of tons). Your marginal cost of mining and marketing copper is constant at $100 per ton, and your discount rate is r = 0.05. You can price like a monopolist but your available supply is only S = 600 tons that...
The data You have been given data from red maple and white oak trees from a...
The data You have been given data from red maple and white oak trees from a national forest dominated by mixed hardwood stands.  The following variables for each tree are included Variable Description treecode Tree label location Eastern or western location in the forest Species Species codes (MR= red maple, OW=white oak) Dbh Diameter at breast height (1.3 m) in cm Height Height in m treecode,location,species,dbh,height 1-W-7,eastern,MR,46.8,39.2 1-N-1,eastern,MR,47.1,37.4 1-E-4,eastern,MR,60.6,41.1 2-M-2,eastern,MR,50.1,26.9 3-E-4,eastern,MR,16.4,25.2 3-M-6,eastern,MR,29.4,27.3 3-S-4,eastern,MR,33.3,38 3-N-5,eastern,MR,37.4,22.5 3-N-6,eastern,MR,36.4,25 4-M-3,eastern,MR,7.2,3.9 4-M-2,eastern,MR,8,6.1 5-E-3,eastern,MR,19.6,14.5 7-N-5,eastern,MR,41.6,34.1 8-W-5,eastern,MR,31.7,31 8-S-7,eastern,MR,39.4,31.2...
Your friend and colleague has been working for about a year since graduating from university. He...
Your friend and colleague has been working for about a year since graduating from university. He has come to you for advice on his saving and spending habits. You have accumulated the following information on his savings and spending that has occurred over the past year: Saving/Spending Amount $ Salary received over the last year, net of income tax 45,000 Rent and utilities paid 16,600 Car expenses paid 4,800 Credit card debt at the start of the year 1,000 Food,...
It has been argued that in our capitalist and consumerist society we are alienated from love...
It has been argued that in our capitalist and consumerist society we are alienated from love of others and God. We have become selfish individuals who are lonely. Do you agree with this position? Explain.
Think of a time when you've been part of a task group that strayed from the...
Think of a time when you've been part of a task group that strayed from the goal path. What counteractive statement could you have made that might have brought it back on track?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT