sci*_*aks 7 matlab curve-fitting
我有一组数据,在绘制时,看起来像这样.

我需要使用该polyfit命令来确定大致在1.7和之间的时间的最佳拟合指数2.3.我还必须将这种指数拟合与简单的线性拟合进行比较.
我给出了等式Temp(t) = Temp0 * exp(-(t-t0)/tau),其中t0是与温度相对应的时间Temp0(我可以选择从哪里开始曲线拟合,但它必须限制在大约介于1.7和2.3之间的区域).这是我的尝试.
% Arbitrarily defined starting point
t0 = 1.71;
%Exponential fit
p = polyfit(time, log(Temp), 1)
tau = -1./p(1)
Temp0 = exp(p(2))
tm = 1.8:0.01:2.3;
Temp_t = Temp0*exp(-(tm)/tau);
plot(time, Temp, tm, Temp_t)
figure(2)
%Linear fit
p2 = polyfit(time, Temp, 1);
Temp_p = p2(1)*tm + p2(2);
plot(time, Temp, tm, Temp_p)
Run Code Online (Sandbox Code Playgroud)
我的指数最终看起来像
.我的线性拟合看起来像
.(几乎相同).我做错了什么?两者是否应该如此相似?我被告知circshift可能有所帮助,但在阅读帮助文件后我无法掌握命令的适用性.
事情的发展正如你所期望的那样。问题在于您尝试拟合的函数并不是数据的很好近似值。观察曲线,曲线的指数部分似乎渐近趋于 16 左右的值;但您使用的函数最终会趋向于 0 的温度。因此,拟合从 22 到 16 的部分将为您提供几乎线性的关系。为了说明这一点,我编写了几行与您拥有的数据点大致匹配的代码 - 并显示了不同的函数(一个趋于 0,另一个趋于 16)将如何为您提供非常不同的曲线形状。第一个(您的原始函数)在 T 值 22 和 16 之间几乎是线性的 - 所以它看起来像线性拟合。
我建议您考虑要拟合的函数的“正确”形状 - 使您选择特定形式的基本物理原理是什么?正确对待这一点至关重要......
这是代码:
time = linspace(1.5, 2.5, 200);
t0 = 1.7;
t1 = 2.3;
tau = 2.0;
% define three sections of the function:
s1 = find(time < t0);
s2 = find(time >= t0 & time < t1);
s3 = find(time > 2.3);
% compute a shape for the function in each section:
tData(s1) = 28 - 50*(time(s1)-1.5).^2;
tData(s2) = 22*exp(-(time(s2)-t0)/tau);
tData(s3) = tData(s2(end)) + (s3 - s3(1))*12 / numel(s3);
figure
plot(time, tData)
% modify the equation slightly: assume equilibrium temperature is 16
% with a bit of effort one could fit for this as a second parameter
Teq = 16;
tData2 = tData;
tau2 = tau / 8; % decay more strongly to get down to approx the same value by t1
tData2(s2) = (22 - Teq) * exp( - (time(s2) - t0) / tau2) + Teq;
tData2(s3) = tData2(s2(end)) + (s3 - s3(1))*12 / numel(s3);
hold on;
plot(time, tData2, 'r')
Run Code Online (Sandbox Code Playgroud)
结果如下图:

我由此得出结论,您的图看起来如此相似的主要原因是您尝试拟合的函数在您选择的域上几乎是线性的 - 不同的函数选择将是更好的匹配。