How to solve a nonlinear equation in Sympy?

How to solve a nonlinear equation in SymPy, which has the form

y = P*x + Q + sqrt(S*x + T) 

where I know y(0) , y'(0) , y(c) , y'(c) . I want to find P , Q , S and T and represent y as a function of x .

I am very confused about the documentation. Please, help.

+5
source share
2 answers

NOTE. My symptom is hung in the original equation y = P*x + Q + sqrt(S*x + T) . I will use y = P*x + Q + x*x*(S*x + T) to demonstrate how the sympy solver works (when it works).

Strategy:

  • Express y as a function of other variables (x, P, Q, S, T)
  • Differentiate y
  • Configure 4 equations using known constants (0, c, y (0), y (c), y '(0), y' (c))
  • Use sympy solve
  • Print out every possible solution (if any)

Code:

 # Set up variables and equations x, y, P, Q, S, T, = sympy.symbols('xy PQS T') c, y_0, y_c, dy_0, dy_c = sympy.symbols('c y_0 y_c dy_0 dy_c') eq_y = P * x + Q + x * x * (S * x + T) eq_dy = eq_y.diff(x) # Set up simultaneous equations that sympy will solve equations = [ (y_0 - eq_y).subs(x, 0), (dy_0 - eq_dy).subs(x, 0), (y_c - eq_y).subs(x, c), (dy_c - eq_dy).subs(x, c) ] # Solve it for P, Q, S and T solution_set = sympy.solve(equations, P, Q, S, T, set = True) # Extract names, individual solutions and print everything names = solution_set[0] solutions = list(solution_set[1]) for k in range(len(solutions)): print('Solution #%d' % (k+1)) for k2, name in enumerate(names): print('\t%s: %s' % (name, solutions[k][k2]) ) 

Output:

 Solution #1 P: dy_0 Q: y_0 S: (c*(dy_0 + dy_c) + 2*y_0 - 2*y_c)/c**3 T: (-c*(2*dy_0 + dy_c) - 3*y_0 + 3*y_c)/c**2 

Now you can use one of these solutions and make another .subs(...) to get y as a function consisting only of your constants and x .

As for your original equation ... I wonder if anyone should post a bug report for sympy so they can improve it ... :)

+2
source

Now the solver has a problem in solving a system of equations having more sqrt . So, in the code below, first remove sqrt , and then solve the system of equations. Currently, the solver is not fast for these types of equations; it takes about 10 seconds to complete.

 P, Q, S, T, = symbols('PQS T') c, y_0, y_c, dy_0, dy_c = symbols('c y_0 y_c dy_0 dy_c') eq_y = (P*x + Q - y(x))**2 + S*x + T eq_dy = eq_y.diff(x) equations = [ (eq_y).subs([(x, 0), (y(0), y_0), (y(x).diff(x).subs(x, 0), dy_0)]), (eq_dy).subs([(x, 0), (y(0), y_0), (y(x).diff(x).subs(x, 0), dy_0)]), (eq_y).subs([(x, c), (y(c), y_c), (y(x).diff(x).subs(x, c), dy_c)]), (eq_dy).subs([(x, c), (y(c), y_c), (y(x).diff(x).subs(x, c), dy_c)]) ] solve(equations, P, Q, S, T) 

Answer:

  [(-(y_0 - y_c)/c, y_0, 0, 0), ((2*c*dy_0*dy_c + dy_0*y_0 - dy_0*y_c + dy_c*y_0 - dy_c*y_c)/(c*dy_0 + c*dy_c + 2*y_0 - 2*y_c), -(2*c**3*dy_0*dy_c**2 - c**2*dy_0**2*y_0 + 2*c**2*dy_0*dy_c*y_0 - 4*c**2*dy_0*dy_c*y_c + c**2*dy_c**2*y_0 - 2*c**2*dy_c**2*y_c - 2*c*dy_0*y_0**2 + 2*c*dy_0*y_c**2 - 4*c*dy_c*y_0*y_c + 4*c*dy_c*y_c**2 - 2*y_0**3 + 2*y_0**2*y_c + 2*y_0*y_c**2 - 2*y_c**3)/(c*dy_0 + c*dy_c + 2*y_0 - 2*y_c)**2, -4*(dy_0 - dy_c)*(c*dy_0 + y_0 - y_c)**2*(c*dy_c + y_0 - y_c)**2/(c*dy_0 + c*dy_c + 2*y_0 - 2*y_c)**3, -4*(c*dy_0 + y_0 - y_c)**2*(c*dy_c + y_0 - y_c)**4/(c*dy_0 + c*dy_c + 2*y_0 - 2*y_c)**4)] 

please cross-check the response.

+1
source

Source: https://habr.com/ru/post/1245814/


All Articles