Python scipy optimize fmin from matlab fminsearch error -
i converting matlab function handle python , receiving error (valueerror: setting array element sequence.) in python. i'm pretty new python sorry if there obvious error.
in matlab:
p = [1 1; 6 1; 6 5] fh = @(x) sqrt(sum((ones(3,1)*x - p).^2, 2)) [x,fval] = fminsearch(@(x) max(fh(x)),[0 0]) in python:
p = np.matrix([[1, 1],[ 6, 1],[ 6, 5]]) fh = lambda x:np.sqrt(sum(np.power((np.ones((3,1))*x - p),2),axis = 0)) xopt = scipy.optimize.fmin(func=fh,x0 = np.matrix([0, 0])) the code works in matlab not in python thanks.
in matlab code, fminsearch minimizing max of fh(x). in python code, therefore, func passed fmin should max of fh well:
import numpy np scipy import optimize p = np.array([[1, 1],[ 6, 1],[ 6, 5]]) def fh(x): return np.max(np.sqrt(np.sum((x - p)**2, axis=1))) xopt = optimize.fmin(func=fh, x0=np.array([0, 0])) print(xopt) yields
optimization terminated successfully. current function value: 3.201562 iterations: 117 function evaluations: 222 [ 3.50007127 2.99991092]
Comments
Post a Comment