MATLAB曲线拟合,指数与线性 [英] MATLAB curve-fitting, exponential vs linear

查看:133
本文介绍了MATLAB曲线拟合,指数与线性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据数组,绘制时看起来像这样.

I have an array of data which, when plotted, looks like this.

我需要使用polyfit命令来确定大约在1.72.3之间的时间的最佳拟合指数.我还必须将此指数拟合与简单的 linear 拟合进行比较.

I need to use the polyfit command to determine the best fitting exponential for the time roughly between 1.7 and 2.3. I must also compare this exponential fit to a simple linear fit.

我给了方程式Temp(t) = Temp0 * exp(-(t-t0)/tau),其中t0是对应于温度Temp0的时间(我可以选择从哪里开始曲线拟合,但是必须将其限制在1.7至2.3).这是我的尝试.

I'm given the equation Temp(t) = Temp0 * exp(-(t-t0)/tau), where t0 is the time corresponding to temperature Temp0 (I can select where to begin my curve-fitting, but it must be confined to the area roughly between 1.7 and 2.3). Here is my attempt.

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

我的指数拟合最终看起来像.我的线性拟合看起来像. (实际上是相同的).我做错了什么?两者的拟合度应该如此相似吗?有人告诉我circshift可能会有所帮助,但在阅读帮助文件后我无法掌握该命令的适用性.

My exponential fit ends up looking like . My linear fit looks like . (virtually identical). What am I doing incorrectly? Should the two fits be so similar? I am told that circshift may help, but I couldn't grasp the applicability of the command after reading the help file.

推荐答案

一切都与您期望的一样.问题在于您要拟合的函数不是很好的数据近似值.盯着曲线看,似乎曲线的指数部分渐近趋向于约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.

这篇关于MATLAB曲线拟合,指数与线性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆