首页 > 解决方案 > 将循环的先前值用于下一个循环

问题描述

我对编码非常陌生,目前正在尝试制作火箭飞行的模型。在需要计算火箭速度的模型中,因此我需要知道由火箭速度决定的空气阻力。所以我的问题是我正在尝试计算一个我需要该值本身的值。我对这个问题的解决方案是取之前的速度值来确定空气阻力并计算新的速度。我需要为我的 h(身高)做同样的事情

速度 = (( F_Rocket - ( F_Drag + F_Gravity ) / m ) t。

这是我用来计算速度的公式,我从

F_resulting = F_Rocket - (F_Drag + F_Gravity)

T0 = 288.15 #temperature needed for the air density
q1 = -0.0065 #constant
e = 2.718281828459
p0 = 101325 #base pressure
g = 9.80665 # gravity 
R = 287.00 # gas constant
G = 6.67259 * (10 ** -11) # gravity constant
m1 = 5.972 * (10 ** 24) #mass earth
m2 = 0.070 #mass rocket (mini plastic rocket)
rAarde = 6371000 #radius earth
Cw = 0.14
A = 0.00053913
F_Rocket = 0.22
t = 0 # this is time

while t <= 50: #the while loop is expressed in time
    T1 = T0 + q1 * (h - h0)
    print('T1 = ', T1, 'K')
    p1 = p0 * ((T1 / T0) ** ( -g / (q1 * R)))
    rho1 = p1 / (R * T1)
    F_Drag = 0.5 * A * Cw * rho1 * (v_old ** 2) # This v needs to be one from the previous loop, I will also need some starting point
    v_new = ((F_Rocket - (F_Drag + F_Gravity) / m) * t
    h_new = v_old * t
    F_Gravity = G * ((m1 * m2) / (rAarde + h_old))
    print(Drag1, 'N')
    print(rho1, 'Kg m^-3')
    print(p1, 'pa')
    t += 1;

我只需要像递归公式一样获取先前答案的值。如果有人可以向我解释如何进行编码,那就太好了。

标签: python

解决方案


在 之外初始化一个速度变量 ( vel) while loop。仅在计算后将其更新为新值F_Drag

vel = 0   # pick an appropriate starting value

while t <= 50:
   ....
   F_Drag = 0.5 * A * Cw * rho1 * (vel ** 2)        
   vel = ((F_Rocket - (F_Drag + F_Gravity) / m) * t
   ....

现在每次进入循环vel时都会是先前计算的值F_Drag,然后使用F_Rocket等式更新它。


推荐阅读