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'