how can i convert a string to an equation with list comprehensions?

When you use eval, you shold determine the scope of the variables used. The following code worked:

import scipy
from scipy import optimize

#second code:
eqn = ['x[0]**2 + x[0]*x[1]-10',
       'x[1]+3*x[0]*x[1]**2-57']
def equat(x):
    return [eval(eqi, {"x": x}) for eqi in eqn]
res = scipy.optimize.root(equat, x0=(0, 0))
res.x

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top