2013-05-27 395 views
7

我有一个数据数组,当绘制时,看起来像这样。MATLAB曲线拟合,指数与线性

http://s12.postimg.org/7ja47a6b1/temp.jpg

我需要使用polyfit命令来大致确定1.72.3之间的最佳拟合指数的时间。我还必须比较这个指数拟合到一个简单的线性拟合。

我给出的等式Temp(t) = Temp0 * exp(-(t-t0)/tau),其中t0是对应于温度Temp0时间(I可以选择在何处开始我的曲线拟合,但是它必须大致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) 

我的指数拟合最终看起来像exponential fit。我的线性适合看起来像linear fit。 (几乎相同)。我做错了什么?如果两者适合如此相似?我听说circshift可能会有所帮助,但在阅读帮助文件后,我无法掌握该命令的适用性。

+0

来自@Amro的链接似乎已被The MathWorks打破。更新版本是[here](http://www.mathworks.com/help/stats/examples/curve-fitting-and-distribution-fitting.html)。 – horchler

+1

Thanks @horchler,这里是我前面提到的示例的更新链接:[通过转换为线性拟合非线性模型的陷阱](http://www.mathworks.com/help/stats/examples/pitfalls-in- fitting-nonlinear-models-by-transforming-to-linearity.html) – Amro

回答

4

事情表现就像你期待。问题是你试图适合的函数不是一个很好的数据近似值。目光看起来曲线,似乎曲线的指数部分渐近地趋于约16的值;但是你使用的函数最终会趋向于0的温度。因此,拟合一个从22到16的部分将会给你一个几乎线性的关系。为了说明这一点,我写了几行代码,这些代码大致与您拥有的数据点匹配 - 并显示不同的函数(倾向于0,倾向于16的另一个函数)会为您提供截然不同的曲线形状。第一个(你的原始函数)在22和16的T值之间几乎是线性的 - 所以它看起来像线性拟合。

我建议您考虑适合的函数的“正确”形状 - 使您选择特定形式的底层物理是什么?获得这种权利至关重要......

下面是代码:

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

这将导致以下情节:

enter image description here

我由此得出结论,主要的原因,你的情节看起来非常类似的是,该功能你试图拟合几乎是线性的,你选择的领域 - 功能的不同选择将是更好的匹配。

+1

非常感谢Floris。我感谢你的回应。我被告知要比较两种形式的曲线拟合,并确定符合数据“更好”的那种。我认为这些表格会更加清晰,如果没有,我开始怀疑我的代码。你的回答非常明确和信息丰富,我感谢你。 – scimaks

2

如果我理解正确,您在polyfit中使用的变量time和temp包含所有值(从1.5到2.5)。因此,在计算polyfit之前,您可能需要将时间和Temp的值限制为1.71至2.3(现在它正在计算从1.5到2.5的polyfit,因此为什么该线不与数据点对齐)。

p = polyfit(time, log(Temp), 1) 
+0

你是对的m_power!谢谢。我已经上传了新版本。指数和线性拟合看起来应该如此相似吗?再次感谢你! – scimaks

4

正如我在评论所提到的,有相对于拟合的非线性模型(无论是在最小二乘意义上的)拟合在对数空间中的线性模型之间的差。

统计工具箱中有一个不错的demo解释情况。我适应下面的代码:

%# sample data 
x = [5.72 4.22 5.72 3.59 5.04 2.66 5.02 3.11 0.13 2.26 ... 
    5.39 2.57 1.20 1.82 3.23 5.46 3.15 1.84 0.21 4.29 ... 
    4.61 0.36 3.76 1.59 1.87 3.14 2.45 5.36 3.44 3.41]'; 
y = [2.66 2.91 0.94 4.28 1.76 4.08 1.11 4.33 8.94 5.25 ... 
    0.02 3.88 6.43 4.08 4.90 1.33 3.63 5.49 7.23 0.88 ... 
    3.08 8.12 1.22 4.24 6.21 5.48 4.89 2.30 4.13 2.17]'; 

xx = linspace(min(x), max(x), 100); 

%# linear regression in log-space 
%#   y = p2 * exp(p1*x) 
%# => log(y) = log(p2) + p1*x 
p_exp = polyfit(x, log(y), 1); 
yy1 = exp(p_exp(2)) .* exp(xx .* p_exp(1)); 

%# linear regression 
p_lin = polyfit(x, y, 1); 
yy2 = polyval(p_lin, xx); 

%# non-linear regression (using previous result as initial coeff) 
f = @(p,x) p(2)*exp(p(1)*x); 
p_nonlin = nlinfit(x, y, f, [p_exp(1) exp(p_exp(2))]); 
yy3 = f(p_nonlin, xx); 

plot(x,y,'o', xx,yy1,'-', xx,yy2,'-', xx,yy3,'-') 
legend({'data points','linear in log-space','linear','non-linear'}) 

regression

0

使用

polyfit(x,y,n) 

功能在Matlab曲线拟合工具箱。