Python Forum
Nested functions. Equation equal to zero works in a wrong way
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Nested functions. Equation equal to zero works in a wrong way
#1
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_}")
Reply
#2
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.
alexfrol86 likes this post
Reply
#3
(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?
Reply
#4
What is this equation you are solving?
Reply
#5
(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))
Reply
#6
(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
Reply
#7
I have solved the issue through fsolve (scipy.optimize). Program works
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Am I wrong or is Udemy wrong? String Slicing! Mavoz 3 2,575 Nov-05-2022, 11:33 AM
Last Post: Mavoz
  is there equal syntax to "dir /s /b" kucingkembar 2 1,001 Aug-16-2022, 08:26 AM
Last Post: kucingkembar
  Nested functions: calculation is not performed alexfrol86 4 1,670 Feb-24-2022, 05:32 PM
Last Post: alexfrol86
  Solving equation equal to zero: How to resolve the syntax error? alexfrol86 3 1,987 Feb-21-2022, 08:58 AM
Last Post: deanhystad
  Can a variable equal 2 things? Extra 4 1,510 Jan-18-2022, 09:21 PM
Last Post: Extra
  Nested Python functions (Dan Bader's book) Drone4four 4 2,585 Jun-26-2021, 07:54 AM
Last Post: ndc85430
  Getting parent variables in nested functions wallgraffiti 1 2,147 Jan-30-2021, 03:53 PM
Last Post: buran
  Not equal a dictionary key value bazcurtis 2 1,941 Dec-11-2019, 11:15 PM
Last Post: bazcurtis
  python gives wrong string length and wrong character thienson30 2 3,025 Oct-15-2019, 08:54 PM
Last Post: Gribouillis
  Working on nested functions boxerboy1168 2 2,609 Dec-28-2018, 07:54 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020