Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 弹丸运动-重力随高度变化_Python_Recurring_Projectile - Fatal编程技术网

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

Python 弹丸运动-重力随高度变化,python,recurring,projectile,Python,Recurring,Projectile,问题是必须在重力不恒定的情况下进行抛射运动。所以位置s(t)=-0.5gt2+v0t和g(s)=g∙我/(RE+s)2。其中G、ME和RE都是常数。因此,新方程为s(t)=-0.5g(s)t2+v0t 我假设每0.005秒的速度是恒定的,所以方程必须每0.005秒更新一次。所以s(t)=s(t-∆t) +v(t)∙∆其中v(t)=v(t-∆t) -g(s(t)-∆t) )∙ ∆t 我现在的代码是 # Assigning Variables G = 6.6742*10**(-11) # Gravi

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

我假设每0.005秒的速度是恒定的,所以方程必须每0.005秒更新一次。所以s(t)=s(t-∆t) +v(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时的结果

我已经在您的代码中添加了注释以及一些更改,以便程序能够运行(至少在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))

它究竟如何“不起作用”?有错误吗?如果是的话,包括完整的回溯。我不认为这部分是错误的,因为这只是给出g的方程式。距地心的距离应该是平方。谢谢你的帮助rabs。是的,你有R_e*s,但是你再次平方,给m^4。如果你想知道距地球中心的距离,它应该是R_e+s,然后你将其平方。它应该是
R_e+s
,但只有当你发射你的射弹时。是的,他现在似乎只使用一个轴。t_0等于一个整数,我想说一个数字。大约一百万,等等。另一种说法是1/.005次,所以每第200次我都要打印出响应。你知道我怎么做吗?