简体   繁体   中英

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. I want to be able to figure out how much time is spent above 100,000-meters. I have my trajectory code just fine (Runge-Kutta), and I can get up to 100,000-meters. 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.

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. 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. states[4,i] != 100000 is meant to get me up to that altitude, at least.

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. 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 100,000. 等于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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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