【问题标题】:MATLAB curve-fitting, exponential vs linearMATLAB 曲线拟合,指数与线性
【发布时间】:2013-05-22 14:10:05
【问题描述】:

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

我需要使用polyfit 命令来确定大致介于1.72.3 之间的时间的最佳拟合指数。我还必须将这个 exponential 拟合与简单的 linear 拟合进行比较。

我得到了公式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)

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

【问题讨论】:

标签: matlab curve-fitting


【解决方案1】:

事情的表现与您预期的一样。问题是您尝试拟合的函数不是数据的非常好的近似值。观察曲线,曲线的指数部分似乎逐渐趋向于 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')

这导致以下情节:

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

【讨论】:

  • 非常感谢,弗洛里斯。感谢您的回复。我被告知要比较曲线拟合的两种形式,并确定一种“更好”地拟合数据的形式。我曾假设这些形式会更加清晰,但当它们不是时,我开始怀疑我的代码。您的回答非常清楚且内容丰富,非常感谢。
【解决方案2】:

正如我在 cmets 中提到的,在对数空间中拟合线性模型与拟合非线性模型(两者都在最小二乘意义上)之间存在差异。

统计工具箱中有一个很好的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'})

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

    • 你是对的 m_power!谢谢你。我已经上传了新的配合。指数拟合和线性拟合应该看起来如此相似吗?再次感谢您!
    【解决方案4】:

    使用

    polyfit(x,y,n)
    

    Matlab曲线拟合工具箱中的函数。

    【讨论】:

      猜你喜欢
      • 2015-03-22
      • 1970-01-01
      • 1970-01-01
      • 2020-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-13
      • 1970-01-01
      相关资源
      最近更新 更多