DeathPox - 6 months ago 37

Python Question

In this program I've been working on with Python the goal is to take user inputs on a given initial velocity, angle, and how far away a structure is/how tall it is we're aiming for. I have been able to calculate how long it takes for something to reach a target, but I'm not sure why the final velocity (how fast it is going when it reaches the target) is coming up wrong.

`# User inputs`

velocity = float(input('Give me a velocity to fire at (in m/s): '))

angle = float(input('Give me an angle to fire at: '))

distance = float(input('Give me how far away you are from the

structure: '))

height = float(input('Give me the height of the structure (in meters):

'))

slingshot = 5 #Height of slingshot in meters

gravity = 9.8 #Earth gravity

# Converting angles to radians

angleRad = math.radians(angle)

# Computing our x and y coordinate

x = math.cos(angleRad)

y = math.sin(angleRad)

# Calculations

time = distance/(velocity * x)

vx = x

vy = y + (-9.8 * time)

finalVelocity = math.sqrt((vx ** 2) + (vy ** 2))

# Output of program

print('It takes your bird' , time , 'seconds to reach the structure')

print('Your velocity at the target distance is' , finalVelocity ,

'meters per second.')

Here is a sample input and what the expected output should be:

Input Velocity: 20

Input Angle: 40

Input Distance: 25

Input Height of Structure: 15

Time to reach structure: 1.63176 s

Final velocity: 15.6384 s

Time to reach structure: 1.63176

Final velocity: 15.36755

At first glance it would appear my program is very close, so I suspected a rounding error, but it is mere coincidence with the chosen numbers that they're close.

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

You miscalculated the horizontal and vertical components of the final velocity. You only used the cosine and sine of the angle, rather than the (magnitude of the) initial velocity times the cosine and sine, respectively. If you modify the following two lines of code, you will obtain the result you were looking for given the sample input you provided:

```
vx = velocity * x
vy = velocity * y - 9.8 * time
```

I rewrote your original code a bit and also computed the final height to check whether the structure was hit or not, so feel free to use it if needed:

```
import math
# User inputs
# v0 = float(input('Give me a velocity to fire at (in m/s): '))
# angle = float(input('Give me an angle to fire at: '))
# distance = float(input('Give me how far away you are from the structure: '))
# height = float(input('Give me the height of the structure (in meters):'))
# Test inputs
v0 = 20
angle = 40
distance = 25
height = 15
# Constants
slingshot = 5 # Height of slingshot in meters
g = 9.8 # Earth gravity
# Converting angle to radians
angleRad = math.radians(angle)
# Computing initial velocity components
vx0 = v0 * math.cos(angleRad)
vy0 = v0 * math.sin(angleRad)
# Computing time to travel horizontal distance
t_x = distance / vx0
# Computing final vertical velocity component
vy_final = vy0 - g * t_x
# Computing magnitude of final velocity
v_final = math.sqrt((vx0 ** 2) + (vy_final ** 2))
# Note: Horizontal component is constant
# Computing final height
y_final = vy0 * t_x - g / 2 * t_x ** 2
# Verify if t_x was computed correctly
# t_y1 = (vy0 + math.sqrt(vy0 ** 2 - 2 * g * y_final)) / g
# t_y2 = (vy0 - math.sqrt(vy0 ** 2 - 2 * g * y_final)) / g
# Output of program
print('It takes your bird', t_x, 'seconds to reach the structure.')
print('Your velocity at the target distance is', v_final,
'meters per second.')
print('\nFinal height: ', y_final)
print('Structure height:', height)
if y_final <= height:
print('\nYou hit the structure!')
```

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**