用曲线拟合猜测曲线的精确演化 python

Guess precise evolution of curve with curve fittin python

我有数据点,可以提供有关物体温度随时间变化的信息。以下是绘制的这些数据点

我的目标是尽可能精确地拟合函数以找到未来温度的演变(我没有数据)并找到 "temperature limit"(最高温度)

现在我尝试用对数函数拟合函数,

def func_log(x, a, b, c, d):
    return a * np.log(b * (x+c)) + d
# ...
expected_coefs_log = [1, 0.3, 1, 1]    
popt, pcov = curve_fit(func_log, self.time, self.temp, expected_coefs_log)

但是正如您在第二张图片上看到的那样,结果并不精确 enough.Is 可以 "rotate" 拟合曲线向右移动吗?好像这个功能还可以,要是我能稍微旋转一下就好了...

如果这不可能,你知道我该如何解决这个问题吗?

正确的方法显然取决于您的数据和模型。然而,强制曲线成为特定形状的一种方法是在拟合过程中 utilize weights

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

#simulate the data
def func_exp(x, a, b, c, d):
    return a * (1 - b* np.exp(-c*x)) + d 

np.random.seed(123456789)
n=400
time_real = np.linspace(0, 5000, n)
temp_real = func_exp(time_real, 21,  0.7, 0.001, 63) + np.random.random(n)

n_measured = int(n*0.5) 
time_measured = time_real[:n_measured]
temp_measured = temp_real[:n_measured]

#curve fitting a logarithmic function on the data
def func_log(x, a, b, c, d):
    return a * np.log(b * (x+c)) + d

#without weights
expected_coefs_log = [3, 1, 1, 1]    
popt_noweight, pcov = curve_fit(func_log, time_measured, temp_measured, expected_coefs_log)
print(popt_noweight)

#artificial weights emphasizing points at a later time point
sigma_coefs_log = np.linspace(5, 0.01, len(time_measured))   
popt_weight, pcov = curve_fit(func_log, time_measured, temp_measured, p0=expected_coefs_log, sigma=sigma_coefs_log)
print(popt_weight)

#graphic representation
plt.scatter(time_real, temp_real, c="orange", marker=".", label="expected data")
plt.scatter(time_measured, temp_measured, color="red", marker=".", label="measured data")
plt.plot(time_real, func_log(time_real, *popt_noweight), color="blue", label="fit, no weight")
plt.plot(time_real, func_log(time_real, *popt_weight), color="green", label="fit, weight")
plt.legend()
plt.show()

示例输出:

但是,如果您期望一个平台(在您的问题中没有解释为什么您认为“想要的函数”应该是正确的),对数模型可能只是错误的函数类型,正如我们可以通过权衡看到的在现在不太适应数据的初始部分。

模型可能应该更像 Tf*(1-e^(-at)),其中 Tf 是平台。这适用于物体由于与另一个具有大热容量的 Tf 物体接触而改变温度。