2017-09-23 3 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秒

最終速度:15.6384秒

私のプログラムの出力:構造に到達するための

時間:1.63176

最終速度:15.36755

一見私のプログラムは非常にあると思われます私は丸め誤差の疑いがありましたが、近くにある選択された数字との偶然の一致に過ぎません。

+2

は、あなたが入力 –

答えて

3

最終速度のhorizontal and vertical componentsを誤って計算しました。コサインと正弦の初期速度の倍率ではなく、角の余弦と正弦を使用しました。あなたは次の2つのコード行を変更した場合、あなたが探していた結果を得るでしょう、あなたが提供されているサンプル入力与えられた:私はあなたの元のコードビットを書き直しましたし、またかどうかを確認するために、最終的な高さを計算し

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

とともに期待される出力を投稿することができこれは、私が間違っていたところに私を指すことができる場合、私は疑問に思って、よく働きますか?ベロシティ成分を正しく把握していないのは私の問題でしたか? – DeathPox

+0

私が今実行している唯一の問題は、非常に低い値を使用しても、まだターゲット構造に到達したと言われています。私はこれがifステートメントをどこか0以下に置くことがないからだと思います。どうすればこのコードを実装して、速度を停止させて0になる「地面」があるのでしょうか?速度を1にし、距離が10000で高さが1の場合、構造体に到達するまでには1000152.32秒かかります 速度は9801492.7メートル/秒です – DeathPox

+0

if文を追加しました発射体がまだ地上より高い(> 0)か地面に当たる(= 0)か、すなわち高さ関数が正かゼロかをチェックする。私はまた、私が前に追加したことを忘れた最終的な高さの計算に初期の高さを加えました。 –

関連する問題