0

I am converting some Matlab code into python and have a problem with fminsearch. I wonder if anyone can help me.

method={'gausse' 'markov'};
subset=find(r>0 & r<=rMax);

a{1}=fminsearch('myfunction',[10 50],[],r(subset),cf(subset),'gausse');
a{2}=fminsearch('myfunction',[10 50],[],r(subset),cf(subset),'markov');

fit(1)=myfunction(a{1},r(subset),cf(subset),'gauss');
fit(2)=myfunction(a{2},r(subset),cf(subset),'markov');

I found similar function in python scipy.optimize.fmin but I don't know how to use it. What the do {} brackets mean in python? Thanks for the any help.

Blanka
  • 1
  • 1

1 Answers1

0

Here is an example of using fmin.

import numpy as np
import scipy.optimize as opt
import matplotlib.pyplot as plt
from matplotlib import cm

myfun = lambda x: -np.exp(-(x[0]-2)**2-(x[1]+1)**2)
X = np.arange(-5,5,0.01)
Y = np.arange(-3,3,0.01)
xopt = opt.fmin(myfun, [-3,2])
print(xopt)

myfun([2,-1]) #ANALYTIC SOLUTION
myfun(xopt) #NUMERICAL SOLUTION

# FOR PLOT
xx,yy = np.meshgrid(X,Y)
zz = -np.exp( -(xx-2)**2-(yy+1)**2)

fig = plt.figure()
ax = fig.gca(projection='3d')
surf = ax.plot_surface(xx,yy,zz,cmap=cm.jet)
plt.xlabel('X=x[0]')
plt.ylabel('Y=x[1]')
plt.zlabel('Z=f(X,Y)')
YKIM
  • 61
  • 2