简体   繁体   中英

Python, perhaps an Infinite Loop

I am new to python so I am sure this is just a rocky mistake, When I try to run the program It just keeps running and doesn't solve anything so I believe that I have made an infinite loop but I am not sure how.

So my objective is to determine a "me" so my max-height "h" becomes 3000, I have defined my functions "h" and "v" When it enters the loop I want it to compute h and v for the first 20 seconds and then checks if my max-height "Apogeu" as got to 3000, if not I will increase my "me" and try it again.

I also had to increase my recursion limit because it was saying that it was being passed, I have tried to put prints inside the loop to check if they appeared but nothing has shown, I have been trying to figure out what was the problem but I couldn't. If you could point me in the right direction it would be a huge help.

import sys
import math
import numpy as np

"""
    Input
""" 

ISP    = 130    #[s]
t_burn = 3.0    #[s]
g0     = 9.81   #[m/s^2]
mf     = 12.0   #[Kg]
D_R    = 0.12   #[m]
C_D    = 0.4
rho    = 1.225
A      = math.pi*D_R**2/4
me     = 0.1

sys.setrecursionlimit(10**4)

"""
    Variables
"""
def v(t):
    
    if t==0.0:
        return 0 
    
    elif t<=t_burn:
        return (ISP*g0*np.log(m0/(m0-me*t))-g0*t-C_D*0.5*A*v(t-0.01)**2*\
                np.log(m0-me*t)/me)
        
    elif t>t_burn:
        return (v(t_burn)-g0*t-C_D*0.5*A*v(t-0.01)**2/mf*t)
    
def h(t):
    
    if t==0.0:
        return 0
    
    elif t<=t_burn:
        return (ISP*g0/me*((m0-me*t)*np.log((m0-me*t)/m0)+me*t)-g0*0.5*t**2-\
                C_D*0.5*A*v(t-0.01)**2/me*((t-m0/me)*np.log(m0-me*t)+m0/me-t))
                            
    elif t>t_burn:
        return (h(t_burn) + v(t_burn)*t -0.5*g0*t**2-\
                C_D*0.5*A*v(t-0.01)**2*t**2*0.5/mf)
    
"""
    Resolution
"""

Apogeu = 0
while (Apogeu<3000):
    
    m0   = mf + me
    t    = 0.0
    
    while (t<=20):
        
        v(t)
        h(t)
        t=t+0.1
        
        
    Apogeu=max(h)
    print("Apogeu=",Apogeu,"m")
    me = me + 0.1

This loop condition while (Apogeu<3000): may always be true and give you an infinite loop So check print("Apogeu=",Apogeu,"m") have any value greater than 3000

Because of one comment, I actually found out that the error was inside one of my functions I had "v(t-0.01)" and it had to be "v(t-0.1)" because that's my increasing rate of dt. Thank you guys !

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