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 に答える