我有一个函数,我试图最小化多个值。对于某些值,它成功终止,但对于其他值,错误
Warning: Maximum number of function evaluations has been exceeded.
是给出的错误。我不确定maxiter和maxfun的作用,以及如何增加或减少这些值以成功地达到最小值。我的理解是这些值是可选的,所以我不确定默认值是什么。
# create starting parameters, parameters equal to sin(x)
a = 1
k = 0
h = 0
wave_params = [a, k, h]
def wave_func(func_params):
"""This function calculates the difference between a sinewave (sin(x)) and raw_data (different sin wave)
This is the function that will be minimized by modulating a, b, k, and h parameters in order to minimize
the difference between curves."""
a = func_params[0]
b = 1
k = func_params[1]
h = func_params[2]
y_wave = a * np.sin((x_vals-h)/b) + k
error = np.sum((y_wave - raw_data) * (y_wave - raw_data))
return error
wave_optimized = scipy.optimize.fmin(wave_func, wave_params)
1条答案
按热度按时间btqmn9zl1#
您可以尝试使用scipy.optimize.minimize方法='Nelder-Mead' https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.minimize.html。
https://docs.scipy.org/doc/scipy/reference/optimize.minimize-neldermead.html#optimize-minimize-neldermead
那你就
或者您可以自定义搜索算法,如下所示:
我对fmin一无所知,但我猜它的行为非常相似。