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

初速度5

这就是我为两者输入 5 时的结果。

4

1 回答 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))
于 2014-02-24T09:55:07.877 回答