2017-09-23 53 views
0

在這個程序中,我一直在與Python合作,目標是在給定的初始速度,角度以及結構距離多遠/我們'重新瞄準。我已經能夠計算出達到某個目標需要多長時間,但我不知道爲什麼最終速度(它到達目標時的速度有多快)出現錯誤。在目標距離確定最終速度

# 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.') 

下面是一個示例輸入和預期輸出應該是什麼:

輸入速度:20 輸入角度:40 輸入距離:結構25 輸入身高:15

預期產量

達到結構的時間:1.63176 s

末速度:15.6384小號

我的程序的輸出

時間達到結構:1.63176

末速度:15.36755

乍一看它會出現在我的計劃是非常關閉,所以我懷疑是一個舍入誤差,但它與選擇的數字很接近。

+2

你能與輸入 –

回答

3

您錯誤計算了最終速度的horizontal and vertical components。你只使用角度的餘弦和正弦,而不是初始速度(的幅度)乘以餘弦和正弦。如果你修改的代碼如下兩行,你會得到你要找的結果給你提供的樣品輸入:

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

我改寫了你原來的代碼位,而且還計算出最終的高度,以檢查是否結構被擊中與否,因此,如有需要隨時使用它:

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_structure = float(input('Give me the height of the structure (in meters):')) 

# Test inputs 
v0 = 20 
angle = 40 
distance = 25 
height_structure = 15 

# Constants 
height_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 = height_slingshot + 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_structure) 
if 0. <= y_final <= height_structure: 
    print('\nYou hit the structure!') 
elif y_final < 0: 
    print('\nYou missed. Not far enough!') 
else: 
    print('\nYou missed. Too far!') 
+0

這種運作良好,我想知道,如果你能指點我哪裏錯了沿後預期的輸出?是否我沒有正確計算速度y分量? – DeathPox

+0

我現在遇到的唯一問題是如果使用非常低的值,它仍然會說它達到目標結構......最終。我認爲這是因爲我沒有把關於低於0的if語句置於某處。我如何在我的代碼中實現這一點,以便基本上有一個「地」使速度停止並變爲0?如果你給它一個1的速度並以1的角度發射,距離爲10000,高度爲1,你可以得到這個結果:達到結構需要1000152.32秒 你的速度是9801492.7米每秒 – DeathPox

+0

我添加了一個if語句檢查彈丸是否仍在地面上(> 0)或撞地(= 0),即高度函數是正值還是爲零。我還添加了初始高度來計算我之前忘記添加的最終高度。 –