fit custom equation to dataset
Show older comments
I have some different datasets, that I would like to fit the following equation to:
1/((A*((x)^(n)))+(B*(x))+C)
As an example I have the following dataset:
x = [0.1418 0.1655 0.1970 0.2309 0.2770 0.3194 0.3697 0.6509 0.7036 0.8242 1.5176 2.4467 3.7230 5.7430 9.5691 11.8164]'
y = [0.0478 0.0741 0.0916 0.1025 0.1064 0.1111 0.1107 0.0640 0.0680 0.0682 0.0401 0.0273 0.0197 0.0140 0.0091 0.0077]'
I have tried fitting it using the following approach. However, the initial guess seems to change the function a lot.
fitfun = fittype ( @(A,B,C,n,x) 1./((A.*((x).^(n)))+(B.*(x))+C)) ;
[fitted_curve] = fit(x,y,fitfun,'StartPoint',x0) ;
Is there a way to optimize the process, to provide a best fit estimate (for instance using least square method) such that the final function does not depend on the initial guess? I have many datasets so the equations should not be estimated by a subject opinion, but instead automatically based on a best fit.
2 Comments
x = [0.1418 0.1655 0.1970 0.2309 0.2770 0.3194 0.3697 0.6509...
0.7036 0.8242 1.5176 2.4467 3.7230 5.7430 9.5691 11.8164]';
y = [0.0478 0.0741 0.0916 0.1025 0.1064 0.1111 0.1107 0.0640...
0.0680 0.0682 0.0401 0.0273 0.0197 0.0140 0.0091 0.0077]';
fitfun = fittype ( @(A,B,C,n,x) 1./((A.*((x).^(n)))+(B.*(x))+C));
x01=[1,1,1,1];
x02=[10,10,10,10];
x03=[-1,-1,-1,-1];
[fitcurve1,gof1] = fit(x,y,fitfun,'StartPoint',x01);
fprintf('x01: sse=%.3f\n',gof1.sse)
[fitcurve2,gof2] = fit(x,y,fitfun,'StartPoint',x02);
fprintf('x02: sse=%.3f\n',gof2.sse)
[fitcurve3,gof3] = fit(x,y,fitfun,'StartPoint',x03);
fprintf('x03: sse=%.3f\n',gof3.sse)
The results above deomnstrate the sensitivity of the fit result to the starting point, as you correctly and originally noticed. The sum squared error is a lot lower, i.e. fit is a lot better, with x02 as the start point.
Alex Sha
on 20 Sep 2022
try to use the tools with the ability of global optimization, no need for guessing initial start-values

Sum Squared Error (SSE): 0.000270672266655144
Root of Mean Square Error (RMSE): 0.00411303010759057
Correlation Coef. (R): 0.993881014964114
R-Square: 0.987799471906097
Parameter Best Estimate
--------- -------------
a 0.00872467906005005
n -3.77763228775143
b 14.2286575620884
c 4.03993812724298
Accepted Answer
More Answers (0)
Categories
Find more on Get Started with Curve Fitting Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!