0

I want to solve a systen of non-linear equations created by loops using root from scipy.optimize. I want to create the equations with one method and then solve them with another. I created the equations with sympy and want to solve them with scipy. My real code has too many loops and every time root from scipy iterates those loops.

This is a very simplified version of what I tried. I made the equations with sympy symbols and then I used lambdify to take the equations out of sympy but the solver sends me an error.

from scipy.optimize import root
from sympy import Symbol, lambdify

y = [Symbol('x%d' % i) for i in range(2)]

def ecuacion():

    global c,cakeo,mellado

    c = []
    cakeo = y[0] +1 + y[1]
    c.append(cakeo)
    mellado = y[1] + 4 + -y[0]
    c.append(mellado)
    print(c)
    return c

ecuacion()

f = lambdify(y, c, 'numpy')

def solver():

    Guess = [1,-1]
    sol = root(f, Guess,method='hybr', jac=False) # Entrega el resultado       
    print(sol.x)
    return sol

solver()

It sends me this error TypeError: root() got an unexpected keyword argument 'jac'. If I delete jac, it send sme this TypeError: unsupported operand type(s) for /: 'int' and 'list'

these are the tracebacks with jac

File "", line 1, in runfile('C:/Users/gian_/Documents/Irri-trickle/speed/i.py', wdir='C:/Users/gian_/Documents/Irri-trickle/speed')

File "C:\Users\gian_\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile execfile(filename, namespace)

File "C:\Users\gian_\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile exec(compile(f.read(), filename, 'exec'), namespace)

File "C:/Users/gian_/Documents/Irri-trickle/speed/i.py", line 54, in solver()

File "C:/Users/gian_/Documents/Irri-trickle/speed/i.py", line 49, in solver sol = root(f, Guess, method='krylov') # Entrega el resultado

TypeError: root() got an unexpected keyword argument 'jac'

File "", line 1, in runfile('C:/Users/gian_/Documents/Irri-trickle/speed/i.py', wdir='C:/Users/gian_/Documents/Irri-trickle/speed')

File "C:\Users\gian_\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile execfile(filename, namespace)

File "C:\Users\gian_\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile exec(compile(f.read(), filename, 'exec'), namespace)

File "C:/Users/gian_/Documents/Irri-trickle/speed/i.py", line 54, in solver()

File "C:/Users/gian_/Documents/Irri-trickle/speed/i.py", line 49, in solver sol = root(f, Guess, jac = False) # Entrega el resultado

TypeError: root() got an unexpected keyword argument 'jac'

JohanC
  • 71,591
  • 8
  • 33
  • 66
  • 1
    What error? If you expect real help, then give us real information about your problem. – hpaulj Nov 08 '19 at 18:57
  • Recent SO about `lambdify`: https://stackoverflow.com/questions/58738051/add-object-has-no-attribute-sinh-error-in-numerical-and-symbolic-expression; https://stackoverflow.com/questions/58648617/float-object-has-no-attribute-sin; https://stackoverflow.com/questions/58465018/why-sympy-lambdify-function-cannot-identify-numpy-sum-function-and-multiply-func – hpaulj Nov 08 '19 at 18:59
  • Add your `imports` and `sympy` setup to the question. Depending on the error we may want to run your case, and suggest working fixes. Without that answers are likely to contain bugs. – hpaulj Nov 08 '19 at 19:03
  • According the `root` docs, `jac=False` should be ok, but the default is `jac=None`, which I think means the same thing. We need the traceback as well, to better identify where the error occurs (it may be several layers down in the `root` call). – hpaulj Nov 08 '19 at 19:14

1 Answers1

1

The provided code also gave me an error: TypeError: _lambdifygenerated() missing 1 required positional argument: 'y1'. Replacing the declaration ofy by y = DeferredVector('y') solved the problem, as suggested in this post. Supposing you are using recent versions of sympy and scipy.

I don't understand the strange error message about jac, as you entered it just fine. Maybe you are inadvertently using root from another package?

Try importing it with an unambiguous name as in:

from scipy.optimize import root as scipy_root
from sympy import Symbol, lambdify, DeferredVector

y = DeferredVector('y')
...

def solver():
    guess = [1,-1]
    sol = scipy_root(f, guess, method='hybr', jac=False) # Entrega el resultado
    print(sol.x)
    return sol
JohanC
  • 71,591
  • 8
  • 33
  • 66