正如我评论的那样:
由于您将数据绘制在对数图上,您是否也适合 log(y) 和 log(x)?由于您的 y 数据变化 5 或 6 个数量级,如果您不适合对数空间,则只有具有最高 y 值的 3 或 4 个数据点才重要。
显然,这暗示有点太微妙了。所以我会更直接:如果您在日志空间中绘图,则适合日志空间。
而且,您的模型很容易从和 NaN 生成复数negative**fraction,这无疑会导致您的所有拟合出现问题。总是打印出最佳拟合参数和协方差矩阵。
因此,您可能需要对参数施加限制(当然,我不知道您的模型是否正确,或者实际上是您认为“正确答案”使用的)。也许从这样的事情开始:
import matplotlib.pyplot as plt
from lmfit import Model
import numpy as np
# the data can be found at the end of this post.
xx, yy = np.genfromtxt('the_data', unpack=True)
# the BPL function
def bendp(x, A, x_bend, allo, alhi, c):
numer = A * x**-allo
denom = 1 + (x/x_bend)**(alhi-allo)
out = (numer/denom) + c
return np.log(out) ## <- TAKE THE LOG
mod = Model(bendp)
para = mod.make_params(A = 0.01, x_bend=1e-2, allo=1., alhi=2., c=1e-2)
# Note: your model is very sensitive # make sure A, x_bend, alhi, and c cannot be negative
para['A'].min = 0
para['x_bend'].min = 0
para['alhi'].min = 0
para['alhi'].max = 3
para['c'].min = 0
result = mod.fit(np.log(yy), para, x=xx) ## <- FIT THE LOG
print(result.fit_report())
plt.loglog(xx, yy, linewidth=0, marker='o', markersize=6)
plt.loglog(xx, np.exp(result.best_fit), 'r', linewidth=5)
plt.show()
希望有帮助...