Python Forum

Full Version: Nested functions: calculation is not performed
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi! Below is my code performed as nested functions. The iterations go not through all lines but only within lines 14-16. Thus, I receive no results. I need lines 36-50 to be performed.

I would be so happy if you tell me what is the problem, because I cannot find it due to my microscopic experience in this field. Sad

y30 = [-0.593, -0.628]
y40 = 5000
y50 = [-0.05, -0.07]
y60 = [1.1, 1.6]
y70 = [1.2, 1.7]
y90 = [0, 1]
tf = 7
t0 = 0
hr = 0.00001
step = -0.01
for y30_ in np.arange(y30[0], y30[1], step):
    y00_ = function_vr(ph0b, et0, y30_)
    y10_ = function_vn(ph0b, et0, y30_)
    y20_ = function_o(ph0, et0, y30_)
    
    for y50_ in np.arange(y50[0], y50[1], step):
      
      for y60_ in np.arange(y60[1], y60[0], step):
        
        for y70_ in np.arange(y70[1], y70[0], step):
          y80_ = funct_labt(y30_, y60_, y70_, y50_)
          
          for y90_ in np.arange(y90[1], y90[0], step):
            ksi0_ = function_propulsion(y40, y50_, y60_, y90_, ptb, mqb)
            deo0_ = function_deo(y40, y50_, y60_, y90_, ptb, mqb, pes)
              
            def fr(t, yay, phb, et, ptb, mqb, pes):
                y0, y1, y2, y3, y4, y5, y6, y7, y8, y9 = yay
                dy0dt = ptb / y4 * y5 / sqrt(y5**2 + y6**2) * function_deo(y4, y5, y6, y9, ptb, mqb, pes) - 1 / y2**2 + y1**2 / y2
                dy1dt = ptb / y4 * y6 / sqrt(y5**2 + y6**2) * function_deo(y4, y5, y6, y9, ptb, mqb, pes) - y0 * y1 / y2
                dy2dt = sqrt(1 / phb) * et * sin(y3)
                dy3dt = (sqrt(1 / phb) * (1 + et * cos(y3))) / (phb / (1 + et * np.cos(y3)))
                dy4dt = -mqb * function_deo(y4, y5, y6, y9, ptb, mqb, pes)
                dy5dt = y6 * y1 / y2 - y7
                dy6dt = -y5 * 2 * y1 / y2 + y6 * y0 / y2 - y8 * 1 / y2
                dy7dt = y5 * (-2 / y2**3 + y1**2 / y2**2) + y6 * (-y0 * y1) / y2 + y8 * y1 / y2**2
                dy8dt = 0
                dy9dt = ptb / y4**2 * (sqrt(y5**2 + y6**2)) * function_deo(y4, y5, y6, y9, ptb, mqb, pes)
                return dy0dt, dy1dt, dy2dt, dy3dt, dy4dt, dy5dt, dy6dt, dy7dt, dy8dt, dy9dt
              
                step = 0.01
                t0, tf = 0, 7
                yay0 = y00_, y10_, y20_, y30_, y40, y50_, y60_, y70_, y80_, y90_
                soln = solve_ivp(fr, (t0, tf), yay0, method='Radau',  args=(ph0b, et0, ptb, mqb, pes))
                print(soln.nfev, 'evaluations required.')
                print(f"y0_(t0, tf), y1_(t0, tf), y2_(t0, tf), y3_(t0, tf), y4_(t0, tf), y5_(t0, tf), y6_(t0, tf), y7_(t0, tf), y8_(t0, tf), y9_(t0, tf), ksi_(t0, tf) = {ksi0_(t0, tf)}, deo0_(t0, tf) = {deo0_(t0, tf)}")
                 
                for it in range(12):
                  plt.plot(soln.t, soln.y[0], label='[Vr]')
                  plt.plot(soln.t, soln.y[1, 11], label='[Vn]')
                  plt.plot(soln.t, soln.y[2, 12], label='[r]')
                  plt.plot(soln.t, soln.y[3, 13], label='[betta]')
                  plt.plot(soln.t, soln.y[4, 14], label='[mt]')
                  plt.plot(soln.t, soln.y[5, 15], label='[l_Vr]')
                  plt.plot(soln.t, soln.y[6, 16], label='[l_Vn]')
                  plt.plot(soln.t, soln.y[7, 17], label='[l_r]')
                  plt.plot(soln.t, soln.y[8, 18], label='[l_betta]')
                  plt.plot(soln.t, soln.y[9, 19], label='[l_mt]')
                  plt.plot(function_deo(y4, y5, y6, y9, ptb, mqb, pes), label='[deo]')
Why are you declaring a function in a loop?
(Feb-24-2022, 03:48 PM)deanhystad Wrote: [ -> ]Why are you declaring a function in a loop?

In a loop, I calculate variables' values for time's moment t0. Then, these values are used in the function "fr" (these make up the vector of initial conditions y0 which is nessessary for solve_ivp - line 35).

If this is a wrong strategy, how should the block be built?
But why is the function declaration inside the loop? Functions are created before the code runs. Each time the function is called it will use the current value of arguments passed and any global variables it might use.
(Feb-24-2022, 04:36 PM)deanhystad Wrote: [ -> ]But why is the function declaration inside the loop? Functions are created before the code runs. Each time the function is called it will use the current value of arguments passed and any global variables it might use.

Thank you, I will make changes