Custom Fit Sometimes Work Sometimes Doesn't

7 views (last 30 days)
Hello-
I'm trying to find two fitting coefficients of a custom fit function.
Fitting works sometimes with a nice fit,
but sometimes it doesn't work at all with the same code and data and the fitting result is just absurd.
Can anyone help and teach me why this happens and how I can fix it?
Here is my code of the fitting part.
V and beta are given constant. I'm finding fitting coefficients gamma and n with the given data of Survival (y value) and Tsup_del (x value).
Any comments would be appreciated.
Thanks!!
% Find gamma and n from the survival function
g = fittype( @(gamma, n, x) exp(-V/beta*gamma/(n+1)*x.^(n+1)) ); %define fitting function
myfit = fit(Tsup_del,Survival,g); %fit with data
gamma = myfit.gamma;
n = myfit.n;
Survival_fit = exp(-V/beta*gamma/(n+1).*Tsup_del.^(n+1));
figure()
plot(Tsup_del,Survival,'s')
hold on
plot(Tsup_del,Survival_fit,'-')
title('Survival function fitting')
xlabel(['Relative Supercooling Temperature, \DeltaT_{sup} [' char(176) 'C]'])
ylabel('Survival Function, \chi')
legend('data','fit')
txt = texlabel(['gamma = ' num2str(gamma)]);
text(17,0.5,txt)
txt2 = texlabel(['n = ' num2str(n)]);
text(17,0.4,txt2)

Accepted Answer

Cris LaPierre
Cris LaPierre on 13 Nov 2021
Edited: Cris LaPierre on 13 Nov 2021
The initial guess or starting oint for the coefficients often is a random guess by default. I would suggest trying to specify a startpoint for the coefficients. These often do not have to be particularly close to the final coefficients. What it does is create a fixed starting point that can help avoid getting incorrect fits or inconsistent results.
Here's how I might modify your fit code. Try adjusting the startpoint values until your code's output is consistent.
myfit = fit(Tsup_del,Survival,g,'StartPoint',[0.1 10]);
See this example on the fit documentation page.
  1 Comment
Youngsup Song
Youngsup Song on 13 Nov 2021
thanks much! Setting a startpoint resolved the issue!!

Sign in to comment.

More Answers (1)

the cyclist
the cyclist on 12 Nov 2021
You say this is the "same code and data". So, what is different? Or are you literally running everything in exactly the same way, and you sometimes get the poor fit?
If it is the latter, then I would guess that the fitting algorithm has a random element to it (perhaps in the guess of the initial parameters). Sometimes, the fitting routine will then get stuck in a local minimum that seems like the best it can do, but it does not find the true global minimum.
Can you upload the data in a MAT file? You can use the paperclip icon in the INSERT section of the toolbar.
  1 Comment
Youngsup Song
Youngsup Song on 13 Nov 2021
yes, I was running everything in excatly the same way.
the problem was the starting point! Cris' script worked perfectly.
thanks!!

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by