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',
def equat(x):
    return [eval(eqi, {"x": x}) for eqi in eqn]
res = scipy.optimize.root(equat, x0=(0, 0))

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top