我想使用具有常数因子的幂律来拟合我的 x 和 y 数据。我的幂律模型是 y(r) = F0 + F*(r)**alpha 其中 F0 是一个常数。我的代码是,
x = [0.015000000000000001, 0.024999999999999998, 0.034999999999999996, 0.044999999999999998, 0.055, 0.065000000000000002, 0.075000000000000011, 0.085000000000000006, 0.094999999999999987, 0.125, 0.17500000000000002, 0.22500000000000003, 0.27500000000000002]
y= [5.6283727993522774, 4.6240796612752799, 3.7366642904247769, 3.0668203445969828, 2.5751865553847577, 2.0815063796430979, 1.7152655187581032, 1.4686235817532258, 1.2501921057958358, 0.80178306738561222, 0.43372429238424598, 0.26012305284446235, 0.19396186239328625]
def func(x,m,c,c0):
return c0 + x**m * c
coeff,var=curve_fit(func,x,y)
print coeff
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.6/dist-packages/scipy/optimize/minpack.py", line 511, in curve_fit
raise RuntimeError(msg)
RuntimeError: Optimal parameters not found: Number of calls to function has reached maxfev = 800.
然后我改变了 maxfev=2000 然后它给了我错误的 coeff 值。如果我改变,我在 func 中的斜率 m 到 (-m) 那么它给了我正确的答案,但我的斜率将是负数。有没有办法克服这个问题?