2014-02-24 20 views
0

問題是必須在重力不穩定的情況下進行拋射運動。所以位置s(t)= -0.5 g t2 + v0 t和g(s)= G∙ME /(RE + s)2。其中G,ME和RE都是常數。因此,新方程是s(t)= -0.5 g(s)t2 + v0 t。拋物線運動 - 重力隨高度變化

我得出一個假設,即每0.005秒的速度是恆定的,所以方程必須每0.005秒更新一次。所以s(t)= s(t-Δt)+ v(t)∙Δt其中v(t)= v(t-Δt)-g(s(t-Δt))∙Δt。

我的代碼現在的問題是

# Assigning Variables 
G = 6.6742*10**(-11) # Gravitational Constant 
M_e = 5.9736*10**(24) # Mass of Earth 
R_e = 6371000 # Radius of Earth 
t = float(input('Time (in seconds)')) # Asking user to input total time, t 
v_0 = float(input('initial velocity')) # Asking user to input initial velocity 
t_0 = .005 # Time before first recalculation 
g = 9.81 # initial gravity 

# Derivative of s(t) = s't = v(t) 
# v(t) = -g(s)*t+v_o 

while t != t_0: 
    s = v_0*t_0 
    g = (g*M_e)/(R_e+s)**2 
    v = -g*s*t_0+v_0 
    t_0=t_0+.005 
    if int(t_0) == t_0: 
     print'Gravity is {%f}, position is {%f}, and velocity is {%f} at time {%.0f}' %(g, s, v, t_0) 
print'Projectile has reached your time {%f}, with gravity {%f}, position {%f}, and velocity {%f}'%(t,g,s,v) 

我真的不知道我應該如何改變它,所以它會奏效。

所以我更新了它作爲我得到的建議。現在,當我運行它時,我的程序要求時間和初始速度和時間(以秒爲單位)。但它甚至不會產生輸出。

時間(單位:秒)5

初始velocity5

這就是結果的樣子時,我對兩個輸入5。

+1

它究竟如何「不起作用」?有錯誤嗎?如果是這樣,請包含*完整追蹤*。 –

回答

1

我已經給你的代碼添加了評論,以及一些改變,所以程序將運行(至少在2.7.6)。然而,雖然它會運行,它不會真正的工作。你應該看看你的函數s,g和v - 它們是不正確的。例如R_e * s不會給你距離地球中心的距離,因爲它的單位現在是米^ 2。

# Assigning Variables 
G = 6.6742*10**(-11) # Gravitational Constant 
M_e = 5.9736*10**(24) # Mass of Earth 
##### In your code the commas are making this a tuple, not an integer - it needs to be defined without commas. 
R_e = 6371000 # Radius of Earth 
t = float(input('Time (in seconds)')) 
v_0 = float(input('initial velocity')) 
t_0 = .005 
#You need to define an initial g 
g = 9.81 

while t != t_0: 
    ####In your code you had a typo here - t_o instead of t_0 
    s = v_0*t_0 
    ####If you don't initialise g, this function does not know what g is. 
    g = (g*M_e)/(R_e*s)**2 
    v = -g*s*t_0+v_0 
    t_0=t_0+.005 
    #####If you want to check if the type of t_0 is an integer, you need to use the type function. It will also never be an integer, as you are always adding a float to a float in the line above this. 
    if type(t_0) == int: 
     print('Gravity is {%f}, position is {%f}, and velocity is {%f} at time {%.0f}' %(g, s, v, t_0)) 
####No need for an if statement to tell if the while loop is finished - just put the print statement after the while loop. 
print('Projectile has reached your time {%f}, with gravity {%f}, position {%f}, and velocity {%f}'%(t,g,s,v)) 
+0

我不相信那部分是錯的,因爲那只是給出g的等式。距離地球中心的距離應該是平方的。謝謝你的幫助。 – user3345646

+0

是的 - 你有R_e * s,但是你再平方米,給予m^4。如果你想要距離地球中心的距離,它應該是R_e + s--那麼你就是平方米。 – rabs

+0

它應該是'R_e + s',但前提是你要射出彈丸。 – LeartS