2

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

我的目标是尽可能精确地拟合一个函数,以找到未来温度的演变(我没有数据)并找到“温度限制”(最高温度)

在此处输入图像描述

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

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)

但正如您在第二张图片中看到的那样,结果不够精确。是否可以将拟合曲线“旋转”到右侧?似乎这个功能可以适合,只要我能稍微旋转一下......

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

4

2 回答 2

1

正确的方法显然取决于您的数据和模型。然而,将曲线强制为某种形状的一种方法是在拟合过程中使用权重:

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()

样本输出: 在此处输入图像描述

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

于 2020-12-27T16:22:56.473 回答
1

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

于 2021-05-18T13:53:42.853 回答