MATLAB:使用fittype的曲线拟合工具箱中的分段函数 [英] MATLAB: Piecewise function in curve fitting toolbox using fittype

查看:1632
本文介绍了MATLAB:使用fittype的曲线拟合工具箱中的分段函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先忽略红色拟合曲线.我想弄个蓝色数据点的曲线.我知道第一部分(在这种情况下最多y〜200)是线性的,然后是另一条曲线(两个对数曲线的组合,但也可以近似地近似),然后它在约250或255处饱和. :

Ignore the red fitted curve first. I'd like to get a curve to the blue datapoints. I know the first part (up to y~200 in this case) is linear, then a different curve (combination of two logarithmic curves but could also be approximated differently) and then it saturates at about 250 or 255. I tried it like this:

func = fittype('(x>=0 & x<=xTrans1).*(A*x+B)+(x>=xTrans1 & x<=xTrans2).*(C*x+D)+(x>=xTrans2).*E*255');
freg = fit(foundData(:,1), foundData(:,2), func);
plot(freg, foundData(:,1), foundData(:,2))

好吧,显然我的fittype可以改善,但是为什么它实际上是不好/错误的呢? 我尝试了另一个更简单的模型:

Okay obviously my fittype could be improved, but why is it actually THAT bad/wrong? I tried another simpler model:

func = fittype('(x>=0 & x<=xTrans1).*(A*x+B)+(x>=xTrans1).*(C*x+D)')
freg = fit(foundData(:,1), foundData(:,2), func);
plot(freg, foundData(:,1), foundData(:,2))

至少我希望有两个线性函数,而我得到的是:

At least I'd expect there two be two linear functions, and what I get is:

还是仅仅因为错误的拟合结果是错误的图才是错误的?

Or is it only the plot which is wrong because the output of the fit is:

 General model:
 f_fit(x) = (x>=0 & x<=xTrans1).*(A*x+B)+(x>=xTrans1).*(C*x+D)
 Coefficients (with 95% confidence bounds):
   A =      0.6491
   B =      0.7317
   C =   0.0007511
   D =       143.5
   xTrans1 =       0.547

至少可以产生良好的xTrans1(但在情节中我看不到)!

Which at least yields a good xTrans1 (but I can't see it in the plot)!

编辑 感谢您指出了更清晰的函数拟合方式,我尝试了以下操作(三个不同的线性函数,带有两个过渡点):

EDIT Thanks for pointing out the more clear way of programming the function to fit, I tried the following (three different linear functions with two transition points):

function y = singleRegression_ansatzfunktion(x,xtrans1,xtrans2,a,b,c,d,e,f)
y = zeros(size(x));

% 3 Geradengleichungen:
for i = 1:length(x)
    if x(i) < xtrans1
        y(i) = a + b.* x(i);
    elseif(x(i) < xtrans2)
        y(i) = c + d.* x(i);
    else
        y(i) = e + f.* x(i);
    end
end

这样称呼钳工:

freg = fit(foundData(:,1), foundData(:,2), 'singleRegression_ansatzfunktion(x,xtrans1,xtrans2,a,b,c,d,e,f)');
plot(freg, foundData(:,1), foundData(:,2))

结果:

 General model:
 f(x) = singleRegression_ansatzfunktion(x,xtrans1,xtrans2,a,b,c,d,e,f)
 Coefficients (with 95% confidence bounds):
   a =      0.7655
   b =      0.7952
   c =      0.1869
   d =      0.4898
   e =       159.2
   f =   0.0005512
   xtrans1 =      0.7094
   xtrans2 =      0.7547

!!!!奇怪!!!!

!!!!Strange!!!!

EDIT2 当不让MATLAB优化过渡点而是自己输入它们时,就像我不久之后在cftool中所做的那样(应该像调用fit一样,但可以更快地找出它),通过自定义公式:

EDIT2 When NOT letting MATLAB optimize the transition points but entering them myself like I shortly did in the cftool (should be the same like calling fit but was quicker to figure it out) via the custom equation:

(x>=0 & x<=2.9e4).*(A*x+B)+(x>2.9e4 & x<=1.3e5).*(B*x+D)+(x>1.3e5).*255

效果很好.我不知道为什么MATLAB无法自己完成此操作,但是好吧...结果到此为止:

It worked pretty well. I don't know why MATLAB can't do this on his own but okay... There you go now as a result:

所以至少我现在修复了它,但我仍然怀疑为什么MATLAB本身无法做到这一点.

So at least I fixed it now but I still remain in doubt why MATLAB simply couldn't do this itself.

推荐答案

您是否尝试过 fittype 文档页面(适合文件定义的曲线"示例),即,定义函数以适合文件以查看其是否有所不同?

Have you tried the approach in the fittype documentation page ("Fit Curve Defined by a File" example) i.e. define your function to fit in a file to see if it makes a difference?

我可以想到的另一种方法是将数据分成两个(或多个)不同的数据集,并对每个块进行两个单独的拟合(但是假设您知道过渡的先验点在或可以解决之前就可以解决).

The other approach I can think of would be to split your data in two (or more) different datasets and do two separate fits for each chunk (but that assumes you know a priori where the transition point(s) is/are or can work it/them out before fitting).

这篇关于MATLAB:使用fittype的曲线拟合工具箱中的分段函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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