我无法使这个箱车适应工作... 我收到了"OptimizeWarning: Covariance of the parameters could not be estimated category=OptimizeWarning)",并且输出的系数没有超出起始猜测值的改进。
import numpy as np
from scipy.optimize import curve_fit
def box(x, *p):
height, center, width = p
return height*(center-width/2 < x)*(x < center+width/2)
x = np.linspace(-5,5)
y = (-2.5<x)*(x<2.5) + np.random.random(len(x))*.1
coeff, var_matrix = curve_fit(box, x, y, p0=[1,0,2])
输出系数为[1.04499699,0.,2.],请注意第三个系数甚至没有改变。
我怀疑这种函数形式对curve_fit使用的levenberg-marquardt算法不利,这有点让人烦恼,因为我喜欢这个函数。在Mathematica中,指定蒙特卡罗优化将是微不足道的。