0

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
Rui
  • 1
  • `t` will like never get to `0` exactly due to round-off error (see [Is floating point math broken](https://stackoverflow.com/q/588004/4996248)). Thus there is no basis case that your code will actually reach. Replace the condition `t==0` by `t<=0`. In any event -- why use recursion at all? This sort of thing doesn't seem like a good use-case for it. – John Coleman Apr 03 '21 at 11:15
  • I have replaced the t==0 for t<=0, I have to have it because inside my function I have v(t-0.1). You actually helped me found my true error – Rui Apr 03 '21 at 11:32

2 Answers2

0

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

Engr1hmh
  • 41
  • 3
0

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 !

Rui
  • 1