Search code examples
pythonmatrixsympyscientific-computing

Using Sympy: TypeError: '>=' not supported between instances of 'NoneType' and 'int'


So, I have this code below:

from sympy import Symbol, solve, nsolve

x1 = Symbol('x1')
x2 = Symbol('x2')
w1 = Symbol('w1')
w2 = Symbol('w2')

eq1 = w1 + w2
eq2 = (w1 * x1) + (w2 * x2)
eq3 = (w1 * x1**2) + (w2 * x2**2)
eq4 = (w1 * x1**3) + (w2 * x2**3)

print(nsolve((eq1, eq2, eq3, eq4), (x1, x2, w1, w2), (2, 0, 2/3, 0)))

For this question:

enter image description here

Which gives me this as a response:

 x = findroot(f, x0, J=J, **kwargs)
  File "C:\Users\Sabri\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\mpmath\calculus\optimization.py", line 969, in findroot
    for x, error in iterations:
  File "C:\Users\Sabri\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\mpmath\calculus\optimization.py", line 660, in __iter__
    s = self.ctx.lu_solve(Jx, fxn)
  File "C:\Users\Sabri\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\mpmath\matrices\linalg.py", line 226, in lu_solve
    A, p = ctx.LU_decomp(A)
  File "C:\Users\Sabri\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\mpmath\matrices\linalg.py", line 142, in LU_decomp
    ctx.swap_row(A, j, p[j])
  File "C:\Users\Sabri\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\mpmath\matrices\matrices.py", line 876, in swap_row
    A[i,k], A[j,k] = A[j,k], A[i,k]
  File "C:\Users\Sabri\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\mpmath\matrices\matrices.py", line 490, in __getitem__
    if key[0] >= self.__rows or key[1] >= self.__cols:
TypeError: '>=' not supported between instances of 'NoneType' and 'int'

Following the documentation here:

https://docs.sympy.org/latest/modules/solvers/solvers.html

It's not very clear what is causing the issue, except one of the variables may be None. On Google, a lot of the issues with a similar error are explicitly shown, which is not the case here. Any suggestions?

Edit: I get this answer:

enter image description here

Using this code:

from scipy.optimize import fsolve


def func(p):
    x1, x2, w1, w2 = p
    return (w1 + w2, (w1 * x1) + (w2 * x2), (w1 * x1**2) + (w2 * x2**2), (w1 * x1**3) + (w2 * x2**3))

x1, x2, w1, w2 = fsolve(func, (2, 0, 2/3, 0))

print(x1, x2, w1, w2)

So the code should return a result, but I'm not sure why it doesn't work for Sympy. Thanks!


Solution

    1. When use nsolve(), the RHS are all zeros. Non-zeros terms on RHS are moved (2, 0, 2/3, 0) to LHS.
    2. The third argument to nsolve() is the initial guess close to the solution.
      An educated guess is (x1,x2,w1,w2) = (-1,1,1,1) in the interval [-1,1] with equal weights.
      I tried a few other guesses. some of them caused the same error.

    Output:

    w1 + w2 - 2
    w1*x1 + w2*x2
    w1*x1**2 + w2*x2**2 - 0.666666666666667
    w1*x1**3 + w2*x2**3
    Matrix([[-0.577350269189626], [0.577350269189626], [1.00000000000000], [1.00000000000000]])
    

    The results agree with the n=2 case on table in Wikipedia.

    Code:

    from sympy import Symbol, solve, nsolve
    
    x1 = Symbol('x1')
    x2 = Symbol('x2')
    w1 = Symbol('w1')
    w2 = Symbol('w2')
    
    eq1 = w1 + w2 - 2
    eq2 = (w1 * x1) + (w2 * x2) - 0
    eq3 = (w1 * x1**2) + (w2 * x2**2) - 2 / 3
    eq4 = (w1 * x1**3) + (w2 * x2**3) - 0
    
    print(eq1, eq2, eq3, eq4, sep='\n')
    print(nsolve((eq1, eq2, eq3, eq4), (x1, x2, w1, w2), (-1, 1, 0.5, 0.5)))