Really hope this the right place to ask this question.
So I’ve been given a few task. The task i have an issue with currently is split into 2 problems.
The first part (1) is to determine the max. Height of an object that is thrown in respect to the horizontal plane.
I am adding the expected solutions at the end.
(1) The Question:
Write a function that calculates the maximum height of a projectile launched at an initial velocity and angle (in degrees) with respect to the horizontal plane: max_height(v0, angle_deg)
Honestly for the (1) part, i am just confused as to why my equation won´t give me the right solutions. Or what other equation is there, as i can only use v0 and angle_def as parameters.
The (2) part, wants me to find the optimal angle.
(2) The Question:
Write a function find_optimal_angle(v0) that uses a loop to find the angle that gives the maximum height for a fixed initial velocity. Consider only integer angles (in degrees) in the loop, from 0° to 90°. Return the angle in degrees and the corresponding maximum height (return optimal_angle, max_h).
Won´t lie haven yet to put much thought into task 2, as i need task 1 for it. Yet i am somewhat stumped by it too. I’ve tried to imagine, how i would do the comparison for it.
Prolly would save all max. Heights in a list, and the use a for-loop to check for the biggest value.
Expected solutions:
# SELF-CHECK
v0 = 20 # m/s, initial velocity
# should be 3.641 m
print(round(max_height(v0, 25), 3))
# does this angle make sense?
angle, height = find_optimal_angle(v0)
print(angle, "degrees with a height of", height, "m")
Honestly, i am hoping someone could show me the correct solution or way for Task 1, while only giving me some hints for task 2. Please.
I hope my question is understandable, as english isn’t my fist language. Thank you for any suggestions or answers.
(1)
My Code:
def max_height(v0, angle_deg):
t_s = (v0*math.sin(angle_deg))/g # Time for the max Height for y.
#t = (2*v0 * math.sin(angle_deg))/g #Zeit Flugbahn. # Time for the max Height for x
m_h = (math.pow(v0,2)*math.pow(math.sin(2*angle_deg),2))/(2*g) # Height on the y-axis. y0 = 0 to a point y.
#m_h = (math.pow(v0,2)*math.sin(2*angle_deg))/g # Height on the x-axis. x0=0 to a point x.
return m_h
I used the equation for the "oblique throw" (it’s the translation Google gave me for "schräger Wurf".
And the expected answer is 3.641m, yet i always get 1.403m.
(2)
My Code (so far):
def find_optimal_angel(v0):
for i in range(0,91):
opt_angle = max_height(v0, i)
So far i am just trying to loop through the given degrees and save the max_heights, i intend to define "opt_angle" as a empty list (outside the loop) and than use a second loop to compare the elements in the list until i have the biggest value.
>Solution :
For problem (1), the formula you’re using for calculating the maximum height of a projectile is incorrect. The correct formula for calculating the maximum height can be derived from the equations of motion. Here’s the corrected version of your max_height function:
import math
g = 9.8 # acceleration due to gravity (m/s^2)
def max_height(v0, angle_deg):
angle_rad = math.radians(angle_deg) # convert angle from degrees to radians
t = (2 * v0 * math.sin(angle_rad)) / g # total time of flight
max_h = (v0 ** 2 * math.sin(angle_rad) ** 2) / (2 * g) # maximum height
return max_h
In this code, math.radians is used to convert the angle from degrees to radians before performing calculations involving trigonometric functions. The formula (2 * v0 * math.sin(angle_rad)) / g calculates the total time of flight, and (v0 ** 2 * math.sin(angle_rad) ** 2) / (2 * g) calculates the maximum height.
For problem (2), your approach of looping through the angles and saving the maximum heights in a list is a good start. To find the angle that gives the maximum height, you can iterate through the list of maximum heights and compare them to find the maximum value. Here’s an example implementation:
def find_optimal_angle(v0):
max_heights = [] # list to store maximum heights
for angle_deg in range(0, 91):
height = max_height(v0, angle_deg)
max_heights.append(height)
max_height_index = max(range(len(max_heights)), key=lambda i: max_heights[i])
optimal_angle = max_height_index
max_h = max_heights[max_height_index]
return optimal_angle, max_h
In this code, the loop iterates through angles from 0 to 90 degrees. For each angle, the max_height function is called to calculate the maximum height, and the height is added to the max_heights list. After the loop, the max function is used with key=lambda i: max_heights[i] to find the index of the maximum height in the list. The optimal angle is the same as the index, and the corresponding maximum height is obtained from the max_heights list.
You can now test both functions using the expected solutions provided:
v0 = 20 # m/s, initial velocity
# Test max_height function
angle = 25 # degrees
print(round(max_height(v0, angle), 3)) # Output: 3.641
# Test find_optimal_angle function
optimal_angle, max_h = find_optimal_angle(v0)
print(optimal_angle, "degrees with a height of", max_h, "m")
I hope this helps you solve both problems.