且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

MATLAB曲线拟合,指数与线性

更新时间:2022-04-03 03:19:18

一切都与您期望的一样.问题在于您要拟合的函数不是很好的数据近似值.盯着曲线看,似乎曲线的指数部分渐近趋向于约16的值;反之亦然.但是您使用的函数最终将趋向于0温度.因此,拟合从22到16的零件将获得几乎线性的关系.为了说明这一点,我写了几行代码,它们与您拥有的数据点大致匹配-并显示了不同的函数(一个趋向于0,另一个趋向于16)如何为您提供曲线的完全不同的形状.第一个(您的原始函数)在T值22和16之间几乎是线性的-因此看起来像是线性拟合.

Things are behaving just as you are expecting. The problem is that the function you are trying to fit is not a very good approximation of the data. Eyeballing the curve, it seems that the exponential part of the curve tends asymptotically to a value around 16; but the function you are using will eventually tend to a temperature of 0. Thus, fitting to a part that's going from 22 to 16 will give you an almost linear relationship. To illustrate this I wrote a few lines of code that approximately match the data points you have - and that show how different functions (one that tends to 0, and another that tends to 16) will give you a very different shape of the curve. The first (your original function) is almost linear between T values of 22 and 16 - so it will look like the linear fit.

我建议您考虑要拟合的函数的正确"形状-使您选择特定形式的潜在物理原理是什么?正确处理是必不可少的...

I suggest that you think about the "right" shape of the function to fit - what is the underlying physics that makes you choose a particular form? Getting that right is essential...

这是代码:

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

这将导致以下绘图:

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

I conclude from this that the main reason your plots look so similar is that the function you are trying to fit is almost linear over the domain you are choosing - a different choice of function will be a better match.