sympysolvernonlinear-equation

Why sympy.solve doen't work with these 12 variables nonlinear equation system?


I am trying to solve a 12 nonlinear equation system with SymPy.

import sympy

q0, q1, q2, q3, q4, q5, q6, q7, q8, q9, q10, q11 = symbols('q0, q1, q2, q3, q4, q5, q6, q7, q8, q9, q10, q11')

unitary_energy_condition = q0**2 + q1**2 + q2**2 + q3**2 + q4**2 + q5**2 + q6**2 + q7**2 + q8**2 + q9**2 + q10**2 + q11**2 - 1

symlet_equation1 = q0 + q1 + q2 + q3 + q4 + q5 + q6 + q7 + q8 + q9 + q10 + q11
symlet_equation2 = -5*q0 - 4*q1 - 3*q2 - 2*q3 - q4 + q6 + 2*q7 + 3*q8 + 4*q9 + 5*q10 + 6*q11
symlet_equation3 = 25*q0 + 16*q1 + 9*q2 + 4*q3 + q4 + q6 + 4*q7 + 9*q8 + 16*q9 + 25*q10 + 36*q11
symlet_equation4 = -125*q0 - 64*q1 - 27*q2 - 8*q3 - q4 + q6 + 8*q7 + 27*q8 + 64*q9 + 125*q10 + 216*q11

orthogonality_equation1 = q0*q2 + q10*q8 + q11*q9 + q1*q3 + q2*q4 + q3*q5 + q4*q6 + q5*q7 + q6*q8 + q7*q9

orthogonality_equation2 = q0*q4 + q10*q6 + q11*q7 + q1*q5 + q2*q6 + q3*q7 + q4*q8 + q5*q9

orthogonality_equation3 = q0*q6 + q10*q4 + q11*q5 + q1*q7 + q2*q8 + q3*q9

orthogonality_equation4 = q0*q8 + q10*q2 + q11*q3 + q1*q9

orthogonality_equation5 = q0*q10 + q11*q1

matching_condition_1 = 0.01*q0 + 0.01*q10 + 0.01*q11 + 0.02*q1 + 0.01*q2 + 0.05*q3 + 0.23*q4 + 0.62*q5 + 0.9*q6 + 0.98*q7 + 0.88*q8 + 0.02*q9
matching_condition_2 = 0.01*q0 + 0.02*q10 + 0.01*q11 + 0.01*q1 + 0.02*q2 + 0.01*q3 + 0.05*q4 + 0.23*q5 + 0.62*q6 + 0.9*q7 + 0.98*q8 + 0.88*q9

ans = sympy.solve([unitary_energy_condition, symlet_equation1, symlet_equation2, symlet_equation3, 
                    symlet_equation4, orthogonality_equation1, orthogonality_equation2, 
                    orthogonality_equation3, orthogonality_equation4, orthogonality_equation5,
                    matching_condition_1, matching_condition_2], 
                    [q0, q1, q2, q3, q4, q5, q6, q7, q8, q9, q10, q11])

The method stays iterating without giving a solution.

I try to solve this with the sympy.nsolve and sympy.nonlinsolve, but the method had the same behavior.

The solution is q = {−0.0069890396, −0.0047282445, −0.0162466459, 0.0210266730, 0.0760407388, 0.2581313042, −0.8041341124, 0.5207860961, 0.0419502235, −0.0847506178, 0.0022720543, −0.0033584299}.

Please, could you give me some ideas? Thanks.


Solution

  • Neither solve nor nonlinsolve will be able to give an analytical solution. (If you even solve the 6 linear equations and substitute their solution into the others you will end up with 6 quadratic equations.) nsolve is your workhorse here. Let nsol be the numerical solutions that you think you know. Increase the maximum number of steps beyond the default of 50 and...

    >>> v = symbols('q0:12')
    >>> nsolve(eqs,v,nsol,maxsteps=100)
    Matrix([
    [-0.00715129674956314],
    [-0.00485533492547843],
    [ -0.0158609428322271],
    [  0.0217286784338786],
    [  0.0758952123585306],
    [   0.256657212877231],
    [  -0.804001455002201],
    [   0.521753111347145],
    [  0.0416427748854584],
    [ -0.0846877567207525],
    [ 0.00236892615345501],
    [-0.00348912982547515]])
    

    Is this the only solution? Try a random guess for the values to see:

    >>> from random import random
    >>> g = [random()/100 for i in v]
    >>> nsolve(eqs,v,g)  # you will see different results...
    Matrix([
    [ -0.0949854541042071],
    [   0.114582081154371],
    [   0.352304082958864],
    [   -0.77819094969228],
    [   0.483184234276383],
    [-5.32308960699164e-5],
    [ -0.0835476301110434],
    [ -0.0570280844309315],
    [  0.0611054599749443],
    [  0.0226639000576377],
    [ -0.0109539118083934],
    [-0.00908049737927473]])
    

    You will have to provide an initial guess that is in the vicinity of what you expect for the solution.