from scipy import optimize
import sympy as sp
x,y=sp.symbols("x y")
def spoptimize(f,x):
jac=sp.Matrix([f]).jacobian(sp.Matrix(x))
nf=sp.lambdify(x,f,"numpy")
njac=sp.lambdify(x,jac,"numpy")
vnf=lambda x:nf(*x)
vnjac=lambda x:njac(*x)[0]
opt=optimize.minimize(vnf,[0]*len(x),method="CG",jac=vnjac)
return opt.x
spoptimize((x-1)**2+(y-4)**2,[x,y])
import sympy as sp
x,y=sp.symbols("x y")
def spoptimize(f,x):
jac=sp.Matrix([f]).jacobian(sp.Matrix(x))
nf=sp.lambdify(x,f,"numpy")
njac=sp.lambdify(x,jac,"numpy")
vnf=lambda x:nf(*x)
vnjac=lambda x:njac(*x)[0]
opt=optimize.minimize(vnf,[0]*len(x),method="CG",jac=vnjac)
return opt.x
spoptimize((x-1)**2+(y-4)**2,[x,y])
No comments:
Post a Comment