简体   繁体   English

在Python中穿越海拔

[英]Crossing an altitude in Python

I am trying to run a Python script that calculates the trajectory of an object from the ground up above 100,000-meters then back below 100,000-meters. 我正在尝试运行一个Python脚本,该脚本从地面开始计算对象的轨迹,高于100,000米,然后返回至100,000米以下。 I want to be able to figure out how much time is spent above 100,000-meters. 我想知道在100,000米以上要花费多少时间。 I have my trajectory code just fine (Runge-Kutta), and I can get up to 100,000-meters. 我的轨迹代码很好(Runge-Kutta),并且可以达到100,000米。 However, I cannot figure out the right Python algorithm to keep going up to max altitude and start coming down to below 100,000-meters. 但是,我无法找出合适的Python算法来保持最高海拔并开始下降到100,000米以下。

Here is what I have: 这是我所拥有的:

while (states[4,(i - 1)] >= 100000 and states[4,i] <= 100000 or states[4,i] != 100000):

I'm ending up in an infinite loop, though. 不过,我最终陷入无限循环。 states[4,i] is the altitude. states[4,i]是海拔。 My thinking is that if the previous ( i - 1 ) altitude is above 100,000-meters and the current ( i ) altitude is below 100,000-meters, I want it to exit the while loop. 我的想法是,如果以前的( i - 1 )海拔高度超过100,000米,而当前( i )海拔高度低于100,000米,我希望它退出while循环。 states[4,i] != 100000 is meant to get me up to that altitude, at least. states[4,i] != 100000至少可以使我达到那个高度。

Thoughts? 有什么想法吗?

I believe the issue you have here is that you need to keep track of when you cross the altitude. 我相信您在这里遇到的问题是,您需要跟踪越过海拔高度的时间。 Why not just have a variable set to false before your loop, then true when you hit >= 100000, and add that to your while. 为什么不将变量在循环前设置为false,然后在命中> = 100000时将其设置为true并将其添加到您的while中。 Also, those conditionals are for leaving your loop, so you should negate it. 此外,这些条件是用于退出循环的,因此您应该对其进行否定。 Eg 例如

reachedHeight = False
while (not (reachedHeight and states[4,(i - 1)] >= 100000 and states[4,i] <= 100000)):
  if (not reachedHeight and states[4,i] >= 100000):
    reachedHeight = True
  ...

Could also be a conditional with a break inside your loop. 也可能是循环内有中断的条件。

The while loop you have above will continue looping unless states[4,i] equals exactly 100,000. 上面的while循环将继续循环,除非states[4,i] 恰好等于100,000。

False and False or True == True

This is likely why you are experiencing the infinite loop. 这很可能就是您遇到无限循环的原因。 You may want to remove the third case and if necessary, perform other checks inside the loop. 您可能需要删除第三种情况,并在必要时在循环内执行其他检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM