我在试着适应一个物质模型(卡罗定律)。一般来说,数据看起来很好,但是用curve_fit
获得正确的模型数据和参数是不可能的(至少对我来说)。我试着设定合理的起始值等
import numpy as np
import matplotlib.pyplot as plt
## Y-DATA
eta = np.array([7128.67, 6814, 6490, 6135.67, 5951.67,
5753.67, 5350, 4929.33, 4499.33,4068.67, 3641.33,
3225.33, 2827.33, 2451, 2104.67, 1788, 1503, 1251.33,
1032.33, 434.199, 271.707, 134.532, 75.7034, 40.9144, 21.7112, 14.9206, 9.29772])
##X-DATA
gamma = np.array([0.1, 0.1426, 0.2034, 0.29, 0.4135, 0.5897, 0.8409, 1.199,
1.71, 2.438, 3.477, 4.959, 7.071, 10.08, 14.38, 20.5,
29.24, 41.7, 59.46, 135.438, 279.707, 772.93,
1709.91, 3734.32, 8082.32, 12665.8, 22353.3])
carreaulaw = lambda x, eta_0, lam, a, n: eta_0 / (1 + (lam * x)**a)**((n-1)/a)
popt, pcov = sp.optimize.curve_fit(carreaulaw, gamma, eta, p0=[8000, 3000, 0.8, 0.1])
print(popt)
x = np.linspace(gamma.min(), gamma.max(), 500)
fig = plt.figure()
diagram = fig.add_axes([0.1, 0.1, 0.8, 0.8])
diagram.set_xlabel(r"$log\ \. \gamma_{true}\ (s^{-1})$", fontsize = 12)
diagram.set_ylabel(r"$log\ \eta_{true}\ (Pa*s)$",fontsize = 12)
#diagram.set_xscale("log")
#diagram.set_yscale("log")
diagram.plot(gamma, eta, "r*")
diagram.plot(x, carreaulaw(x, popt[0], popt[1], popt[2], popt[3]), "g-")
我经常收到错误:RuntimeWarning: invalid value encountered in power
。我已经试过很多变奏曲了,现在很难接受。在
如果我不给出任何起始值,我得到:
^{pr2}$以下是日志刻度上的数据图像:
我真的不知道我哪里错了!数据看起来很好,这就是为什么我永远不会用完maxfev
。在
您需要做的就是传递
curve_fit
中的边界。当没有定义边界时,可以进行非实数运算,比如(在您的例子中)负数的浮点求幂。在边界被简单地定义为两个具有上下限的列表/元组的列表:
输出:
这是一个使用数据和公式的图形拟合器。此示例代码使用scipy的差分进化遗传算法来确定curve_fit()的初始参数估计。这个scipy模块使用拉丁超立方体算法来确保对参数空间的彻底搜索,这需要搜索范围。找到参数的范围要比单个值容易得多,在这里,我尝试了不同的边界,直到拟合看起来对我来说还可以。你应该检查一下我使用的界限,看看它们是否合理。在
相关问题 更多 >
编程相关推荐