我正试图找到以下函数的最小值:
这是电话:
>>> optimize.fmin(residualLambdaMinimize, 0.01, args=(u, returnsMax, Param, residualLambdaExtended),
disp=False, full_output=True, xtol=0.00001, ftol = 0.0001)
Out[19]: (array([ 0.0104]), 0.49331109755304359, 10, 23, 0)
>>> residualLambdaMinimize(0.015, u, returnsMax, Param, residualLambdaExtended)
Out[22]: 0.46358005517761958
>>> residualLambdaMinimize(0.016, u, returnsMax, Param, residualLambdaExtended)
Out[23]: 0.42610470795409616
正如您所看到的,直接邻域中存在产生较小值的点。为什么我的解算器不考虑它们?
答案 0 :(得分:1)
这是一个可以帮助您调试情况的建议。
如果你添加类似的东西
data.append((x, result))
到residualLambdaMinimize
,您可以收集optimize.fmin
正在评估的所有点residualLambdaMinimize
:
data = []
def residualLambdaMinimize(x, u, returnsMax, Param, residualLambdaExtended):
result = ...
data.append((x, result))
return result
然后,如果您发布fmin
,我们可能会更好地了解data
正在做什么(并且可能会重现问题),而我们无需确切知道residualLambdaMinimize
的定义方式。< / p>
此外,您可以想象出&#34;路径&#34; fmin
正在尝试找到最低要求:
import numpy as np
import scipy.optimize as optimize
import matplotlib.pyplot as plt
data = []
def residualLambdaMinimize(x, u, returnsMax, Param, residualLambdaExtended):
result = (x-0.025)**2
data.append((x, result))
return result
u, returnsMax, Param, residualLambdaExtended = range(4)
retval = optimize.fmin(
residualLambdaMinimize, 0.01,
args=(u, returnsMax, Param, residualLambdaExtended),
disp=False, full_output=True, xtol=0.00001, ftol = 0.0001)
data = np.squeeze(data)
x, y = data.T
plt.plot(x, y)
plt.show()