83368

MATLAB: Piecewise function in curve fitting toolbox using fittype

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

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

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

<hr>

<strong>EDIT</strong> 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

Calling the fitter like that:

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

Resulting in:

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!!!!

<hr>

<strong>EDIT2</strong> 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

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:

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

Answer1:

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

Recommend

  • Confidence Interval for a Seaborn Boxplot
  • Prediction of 'mlm' linear model object from `lm()`
  • How to understand F-test based lmfit confidence intervals
  • R arules, mine only rules from specific column
  • Matlab and mechanics (mostly physics)
  • Hash function to matrix
  • Why can't pass only 1 coulmn to glmnet when it is possible in glm function in R?
  • How to extract the p-value for the slope from an ols object in R
  • Drawing a polygon over the entire map
  • JsonConvert.DeserializeObject, Index was outside the bounds of the array
  • Drag and reorder - UICollectionview with sections
  • Creating My Symmetric Key in C#
  • WP7 difficulties binding data to listbox itemssource - won't refresh
  • Generating anchors with PyYAML.dump()?
  • What is the best way to debug Bootstrap.groovy?
  • Using HTML/CSS for UI in XNA?
  • How to set current CultureUI via XAML binding
  • Should I use composite primary keys in Grails?
  • PyQt4 application on Windows is crashing on exit
  • sudo easy_install for SimpleITK not that easy
  • If I am creating a simple client server application in IntelliJ, how should this work?
  • crash in __tcf_0
  • How to add closing tag for canvas in three js rendered Canvas?
  • Examples of how to a STS in .Net 4.5 using WCF
  • there is no graph with tensorboard
  • CSS bleed-through with cfinput type=“datefield”
  • R Split data.frame using a column that represents and on/off switch
  • Can I programmatically choose the Android layout folder?
  • Not able to aggregate on nested fields in elasticsearch
  • Unable to install Git-core+svn by MacPorts
  • Eloquent paginate function in Slim 3 project using twig
  • Django simple Captcha “No module named fields” error
  • Could not find rake using whenever rails
  • 'TypeError' while using NSGA2 to solve Multi-objective prob. from pyopt-sparse in OpenMDAO
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?
  • Bitwise OR returns boolean when one of operands is nil
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Binding checkboxes to object values in AngularJs
  • How can I use `wmic` in a Windows PE script?