Python Forum

Full Version: Nested functions. Equation equal to zero works in a wrong way
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi there! The following block of code works, but the results are presented as the list of formulas. Before I have included lines 18-22 (lines relate to equation equl to zero I have to include), the calculation took around 20-30 minutes and the result was just nice - all the possible variations of variables values clearly presented. After I added lines mentioned, I see that the line 21 is performed for hours and the result looks as the variables of the equation remain symbols and the calculation cannot be performed. I get not variables with numeric values, but a list with formulas. So I see that the iteration through values for these variables (which received a symbolic type) is not going. How can I solve this problem? Help me please

About my purpose: For each of the variables with underlining, I have set a range of possible values, within which the values are sorted with a set step. In this block of code, I am trying to take into account the equations where are unknown variables in order to reduce the number of possible values. I use nested functions. After the last function, the final combinations of values (concrete numbers) for variables should be output.

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_)
    #print(f"y00_ = {y00_}, y10_ = {y10_}, y20_ = {y20_}, y30_ = {y30_}")
    
    for y50_ in np.arange(y50[0], y50[1], step):
      #print(y50_)
      
      for y60_ in np.arange(y60[1], y60[0], step):
        tn0_ = funct_tn(y50_, y60_)
        #print(y60_)
        
        for y70_ in np.arange(y70[1], y70[0], step):
          y80_ = funct_labt(y30_, y60_, y70_, y50_)
          function_gam(et0, y30_, ph0b, y50_, y60_, y70_, y80_)
          ht0, et0, y30_, ph0b, y50_, y60_, y70_, y80_  = symbols('ht0 et0 y30_ ph0b y50_ y60_ y70_ y80_')
          equation = Eq(ht0, (y50_ * et0 * cos(y30_) / sqrt(ph0b) + y60_ * -et0 * sin(y30_) / sqrt(ph0b) + y70_ * sqrt(1 / ph0b) * et0 * sin(y30_) + y80_ * (sqrt(1 / ph0b) * (1 + et0 * cos(y30_)) * (1 + et0 * cos(y30_))**2) / ph0b**2))
          #Use sympy.subs() method
          ham0 = solve(equation.subs(ht0, 0))
          print(ham0) #do I need this line?
          
          for y90_ in np.arange(y90[1], y90[0], step):
            function_propulsion(y40, y50_, y60_, y90_, ptb, mqb)
            function_deo(y40, y50_, y60_, y90_, ptb, mqb, pes)

    print(f"y00_ = {y00_}, y10_ = {y10_}, y20_ = {y20_}, y30_ = {y30_}, y50_ = {y50_}, y60_ = {y60_}, y70_ = {y70_}, y80_ = {y80_}, y90_ = {y90_}")
Why are you using symbols if you don't want symbolic results? You can substitute numerical values for your symbols (look at .subs and N).

I think this could win a code obfuscation contest.
(Feb-21-2022, 11:10 PM)deanhystad Wrote: [ -> ]Why are you using symbols if you don't want symbolic results? You can substitute numerical values for your symbols (look at .subs and N).

I think this could win a code obfuscation contest.

I am a beginner in Python. I found that the equation equal to zero can be included only using symbolic type. How I can change lines 18-22, so that I get results I want?
What is this equation you are solving?
(Feb-22-2022, 04:33 AM)deanhystad Wrote: [ -> ]What is this equation you are solving?

This is a quadratic equation (hamiltonian) which is equal to zero in moments of time t0 and tf. For now, I try to include it for start time (t0).

equation = Eq(ht0, (y50_ * et0 * cos(y30_) / sqrt(ph0b) + y60_ * -et0 * sin(y30_) / sqrt(ph0b) + y70_ * sqrt(1 / ph0b) * et0 * sin(y30_) + y80_ * (sqrt(1 / ph0b) * (1 + et0 * cos(y30_)) * (1 + et0 * cos(y30_))**2) / ph0b**2))
(Feb-22-2022, 04:33 AM)deanhystad Wrote: [ -> ]What is this equation you are solving?

Line 17 presents the function for the same equation:

function_gam(et0, y30_, ph0b, y50_, y60_, y70_, y80_)
It is included as:
def function_gam(et, y3, phb, y5, y6, y7, y8):
  ht = y5 * et * cos(y3) / sqrt(phb) + y6 * -et * sin(y3) / sqrt(phb) + y7 * sqrt(1 / phb) * et * sin(y3) + y8 * (sqrt(1 / phb) * (1 + et * cos(y3)) * (1 + et * cos(y3))**2) / phb**2
  return ht
I have solved the issue through fsolve (scipy.optimize). Program works