弹丸运动 - 重力随高度变化

时间:2014-02-24 09:36:30

标签: python recurring projectile

问题是必须在重力不恒定的情况下进行抛射运动。所以位置s(t)= -0.5 g t2 + v0 t和g(s)= G∙ME /(RE + s)2。其中G,ME和RE都是常量。因此,新方程是s(t)= -0.5g(s)t2 + v0t。

我假设每个.005秒的速度都是常数,因此等式必须每隔.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

初始速度5

当我为两者输入5时,结果如何。

1 个答案:

答案 0 :(得分: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))