Is fit() deterministic when StartPoint is specified?

Hi! Running the same script on the same data I got slightly different coefficients. Since StartPoint is always specified, I expected deterministic results. Is this expected behavior, and what could cause it? Here following the snippet I use:
% Power-law fit (log-log space)
ft = fittype('log(a) + n*log(x)', 'dependent', 'y', 'independent', 'x');
[fitresult, gof] = fit(x', log(y)', ft, 'Startpoint', [1 -1]);
% Exponential fit with weights
w = 1./y.^2;
ft = fittype('a*exp(b*x) + c', 'independent', 'x');
[fitresult, gof] = fit(x', y', ft, 'Startpoint', [110 -0.03 2], 'weights', w);
If useful: MATLAB Version: 24.2.0.2923080 (R2024b) Update 6

3 Commenti

John D'Errico
John D'Errico circa 14 ore fa
Modificato: John D'Errico circa 14 ore fa
If you have a specific example that yields varying results, please show it with the data, so we can go more deeply.
The example you show has two TOTALLY different models to be fit, one with weights, one not, so I'm not sure what your examples signify.
Torsten
Torsten circa 13 ore fa
Modificato: Torsten circa 13 ore fa
Specifying "log(a)" in the first model can become dangerous if "a" gets negative. Better use "a+n*log(x)' as fittype.
Matt J
Matt J circa 11 ore fa
Modificato: Matt J circa 11 ore fa
Not sure why you don't use the built-in fittypes 'poly1' and 'exp2'
% Power-law fit (log-log space)
[fitresult, gof] = fit(log(x)', log(y)', 'poly1');
% Exponential fit with weights
w = 1./y.^2;
lb=[-inf,-inf,-inf,0];
[fitresult, gof] = fit(x', y', 'exp2', Weights=w,Lower=lb, Upper=-lb);

Accedi per commentare.

Risposte (2)

If the start point is specified, then yes, FIT should be deterministic. At that point there is no reason for any pseudo-random numbers to be involved. Two calls will be perfectly reflected, down to the LSB.
In the case of purely linear models, and FIT knows them to be linear, then FIT wil use the standard linear algebraic methods. These are perfectly deterministic, as no starting values are ever employed.
In the case of a nonlinear model, then again, the algorithms employed by FIT are well understood, and IF you fully specify the starting values, then FIT should be deterministic.
HOWEVER!!!!!!! I can see at least one caveat in that claim. There may be floating point issues. For exmple, if you run the same problem on two different machines, and they have a different number of cores, AND there was reason for the computations to become multi-threaded, then I would expect to see floating point trash appear due to a different order of computations, spread out between a different number of cores. And since it can often be the case that a nonlinear estimation is ill-posed, then that floating point trash will become amplified in your result.
Of course, there may be other issues. If FIT could be forced to use a tool like GA as the solver, then all bets are off. So I did a quick check through the options for fit. Algorithms like the interior point method and Levenberg-Marquardt are both going to be deterministic. I see nothing in the various options that would allow any variation, except for the case I have already mentioned. (I may have missed something of course, but I don't think so.)
For example:
x = randn(20,1);
y = 1 + 3*exp(x/2) + randn(size(x))/10;
w = 1./y.^2;
ft = fittype('a*exp(b*x) + c', 'independent', 'x');
format long g
[fitresult, gof] = fit(x, y, ft, 'Startpoint', [1.5 1.5 1.5], 'weights', w)
fitresult =
General model: fitresult(x) = a*exp(b*x) + c Coefficients (with 95% confidence bounds): a = 3.43 (2.752, 4.109) b = 0.4483 (0.3618, 0.5348) c = 0.5798 (-0.02791, 1.188)
gof = struct with fields:
sse: 0.0219464870804581 rsquare: 0.990953141232891 dfe: 17 adjrsquare: 0.989888804907349 rmse: 0.035930068581374
abc1 = [fitresult.a,fitresult.b,fitresult.c]
abc1 = 1×3
3.43034210384008 0.448305660223591 0.579825732594435
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
[fitresult, gof] = fit(x, y, ft, 'Startpoint', [1.5 1.5 1.5], 'weights', w)
fitresult =
General model: fitresult(x) = a*exp(b*x) + c Coefficients (with 95% confidence bounds): a = 3.43 (2.752, 4.109) b = 0.4483 (0.3618, 0.5348) c = 0.5798 (-0.02791, 1.188)
gof = struct with fields:
sse: 0.0219464870804581 rsquare: 0.990953141232891 dfe: 17 adjrsquare: 0.989888804907349 rmse: 0.035930068581374
abc2 = [fitresult.a,fitresult.b,fitresult.c]
abc2 = 1×3
3.43034210384008 0.448305660223591 0.579825732594435
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
isequal(abc1,abc2)
ans = logical
1
As expected, the two results are identical down to the LSB.
So if you have an example where this fails to be true, then you need to show it, AND provide the data.
dpb
dpb circa 15 ore fa
The "Algorithms" section of fit notes that "If the fit type expression input is a character vector, string scalar, or anonymous function, then the toolbox uses a nonlinear fitting algorithm to fit the model to data."
"If the fit type expression input is a cell array or string array of terms, then the toolbox uses a linear fitting algorithm to fit the model to data."
Since you've given the char() vector description the nonlinear fitting techniques will be called and they are reandomized so the starting points will be somewhat different.

1 Commento

Torsten
Torsten circa 14 ore fa
Modificato: Torsten circa 14 ore fa
so the starting points will be somewhat different.
Somewhat different from the starting points specified by the user ? Is this also documented somewhere ?

Accedi per commentare.

Categorie

Richiesto:

Sim
il 16 Mar 2026 alle 20:09

Modificato:

circa 22 ore fa

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by