python - 将循环的先前值用于下一个循环
问题描述
我对编码非常陌生,目前正在尝试制作火箭飞行的模型。在需要计算火箭速度的模型中,因此我需要知道由火箭速度决定的空气阻力。所以我的问题是我正在尝试计算一个我需要该值本身的值。我对这个问题的解决方案是取之前的速度值来确定空气阻力并计算新的速度。我需要为我的 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;
我只需要像递归公式一样获取先前答案的值。如果有人可以向我解释如何进行编码,那就太好了。
解决方案
在 之外初始化一个速度变量 ( 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
等式更新它。
推荐阅读
- javascript - 处于状态的打字稿默认输入元素
- javascript - nextjs 中的链接不起作用
- flutter - 如何在flutter中运行后台服务
- javascript - 用户状态在 onMembersAdded 函数中显示为空
- python - 需要一些关于在 python 中加速 HTTP 请求的建议
- javascript - 响应数据未定义由节点 js 服务器中的 XMLHttpRequest 发送
- amazon-web-services - 有没有比使用两个全局二级索引 (GSI) 更好的方法来模拟这种访问模式?
- javascript - 反应dataTable数据不更新
- javascript - 在 Dropzone 的 JS 函数中设置 vue 变量
- elasticsearch - elasticsearch中的日志聚合