指数拟合神经质。乐观点曲线拟合不工作

2024-04-29 18:51:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用神经质。乐观点在下面的简单示例here之后,曲线拟合以适应某些数据的指数。在

脚本运行时没有错误,但是适合性很差。当我在曲线拟合的每一步查看popt的输出时,它似乎没有很好地从初始参数跳到一系列1.0,尽管它似乎使第3个参数回到了一个适当的值:

92.0 0.01 28.0
1.0 1.0 1.0
1.0 1.0 1.0
1.0 1.0 1.0
1.00012207031 1.0 1.0
1.0 1.00012207031 1.0
1.0 1.0 1.00012207031
1.0 1.0 44.3112882656
1.00012207031 1.0 44.3112882656
1.0 1.00012207031 44.3112882656
1.0 1.0 44.3166973584
1.0 1.0 44.3112896048
1.0 1.0 44.3112882656

我不确定是什么原因导致了这种情况,除了可能模型与数据不太吻合,尽管我强烈怀疑它应该(物理就是物理)。有人有什么想法吗?我把我的(非常简单的)脚本贴在下面。谢谢。在

^{pr2}$

另外,下面是我试图拟合的数据:

100 124
130 120
135 112
140 105
145 99
150 92
155 82
160 75
165 70
170 65
175 60
180 56
185 55
190 52
195 49
200 45
205 44
210 40
215 39
220 37
225 35

Tags: 数据模型脚本示例参数here错误物理
1条回答
网友
1楼 · 发布于 2024-04-29 18:51:38

我清理了您的代码,并将时间从0开始,为了使它们正常工作,我不止一次地使用指数函数:

import matplotlib.pyplot as plt
import numpy as np
from scipy.optimize import curve_fit

time, temp = np.loadtxt('test.txt', unpack=True)

# Newton cooling law fitting
time -= time.min()
def TEMP_FIT(t, T0, k, Troom):
    print T0, k, Troom
    return T0 * np.exp(-k*t) + Troom

popt, pcov = curve_fit(TEMP_FIT, time, temp)

# Plotting
plt.figure()
plt.plot(time, temp, 'bo ',label='Heater off', alpha=0.5)
plt.plot(time, TEMP_FIT(time, *popt), label='Newton Cooling Law Fit')
plt.xlim(-25, 250)
plt.xlabel('Time (min)')
plt.ylabel('Temperature ($^\circ$C)')
ax = plt.gca()
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
plt.legend(fontsize=8)
plt.savefig('test.png', bbox_inches='tight')

结果是:

enter image description here

移除样本的第一个点:

enter image description here

相关问题 更多 >