Why is it that if I adjust a curve with the curve fitting tool, it does not give me the same results as if I fit it without the tool?

2 views (last 30 days)
Hello, community,
I am trying to fit a series of points with a power function with and without the tool, using the same starting points and having different results. Maybe it is an obvious question but I would like to know why. Here are the two codes and the data attached:
function y = myfunction(x,a1,b1)
y=a1*x.^b1;
end
ft5 = fittype( 'myfunction(x,a1,c1)' )
f5 = fit( x_inter',y_inter', ft5,'StartPoint', [8.64593401687141e-71 16.7561500073752] )
plot( f5, x_inter, y_inter )
With the following results:
General model:
f5(x) = myfunction(x,a1,c1)
Coefficients (with 95% confidence bounds):
a1 = 3.368e-71 (-1.784e-70, 2.458e-70)
c1 = 16.76 (16.13, 17.38)
An this is the code generated by the tool:
%% Fit: 'untitled fit 1'.
[xData, yData] = prepareCurveData( x_inter, y_inter );
% Set up fittype and options.
ft = fittype( 'power1' );
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.Display = 'Off';
opts.StartPoint = [8.64593401687141e-71 16.7561500073752];
% Fit model to data.
[fitresult, gof] = fit( xData, yData, ft, opts );
% Plot fit with data.
figure( 'Name', 'untitled fit 1' );
h = plot( fitresult, xData, yData, 'predobs', 0.99 );
legend( h, 'y_inter vs. x_inter', 'untitled fit 1', 'Lower bounds (untitled fit 1)', 'Upper bounds (untitled fit 1)', 'Location', 'NorthEast', 'Interpreter', 'none' );
% Label axes
xlabel( 'x_inter', 'Interpreter', 'none' );
ylabel( 'y_inter', 'Interpreter', 'none' );
grid on
Getting the results:
General model Power1:
f(x) = a*x^b
Coefficients (with 95% confidence bounds):
a = 9.531e-154 (-3.249e-153, 5.155e-153)
b = 35.54 (35.1, 35.97)
Goodness of fit:
SSE: 1.796e+07
R-square: 0.9771
Adjusted R-square: 0.9771
RMSE: 94.82
Thank you!

Accepted Answer

Matt J
Matt J on 11 Dec 2021
Edited: Matt J on 11 Dec 2021
In the first case, fit() doesn't know anything about the structure of your curve model. The details are hidden inside myfunction().
In the second case, you are specifying one of the Toolbox's stock library of curve models. It therefore knows more about the curve and uses a different method to estimate the parameters, one that is specifically tailored to power1.
Because a different solution method is used, a different path is taken by the iterative solver, and a different solution can be reached, when the solution is non-unique. Here, when a=0 (as was the case in both versions), the solution for the b parameter is indeed non-unique.
  2 Comments
Angelavtc
Angelavtc on 11 Dec 2021
Edited: Angelavtc on 11 Dec 2021
Thanks @Matt J is there any way to call the curve fitting tool by regions? This is linked to my last question (https://fr.mathworks.com/matlabcentral/answers/1608110-how-can-i-find-where-to-split-a-piecewise-regression-and-find-the-parameters-involved-in-the-functio?s_tid=srchtitle= ). What I mean is, can I call the linear fitting tool when X is less than 23000 and the power fit when x is above 23000? I see that the curve fitting tool adjusts better to any curve. Thank you!

Sign in to comment.

More Answers (0)

Categories

Find more on Linear and Nonlinear Regression 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!