how to set initial guesses in Curve fitting to avoid the local minimum?

78 views (last 30 days)
Hi...
I am currently facing a problem with initial guesses for parameters in curve fitting toolbox...
I obtained the code from Curve fitting toolbox --> Generate code
here the code uses the nonlinear least square method with a trust-region algorithm...
but every time I put the different initial condition the solution changes (maybe due to a local minimum)...
so how to provide the best initial guesses for avoiding it...or any iterative method
and is there any different approach for getting the solution which will give me the minimum RMSE
  2 Comments
Matt J
Matt J on 3 Oct 2019
If you show us the model function, we may be able to suggest something.
KUNAL PATIL
KUNAL PATIL on 3 Oct 2019
V=A+B*(1-exp(-T/C))+D*(1-exp(-T/E))
where V=volt, T=Time
Parameters to be determined..A, B, C, D, E..
Lower Bound=[0 0 0 0 0] and Upper Bound=[inf inf inf inf inf]
please go through the attached file for data...
Thanking in advance... :)

Sign in to comment.

Accepted Answer

John D'Errico
John D'Errico on 3 Oct 2019
Edited: John D'Errico on 3 Oct 2019
As Alex says, it is not easy, and no perfect answer exists.
The model you have posed is a difficult one to estimate, because it is a sum of exponentials. They are classiclly terribly difficult to estimate. That means you need good data, sufficient quantity of data, good starting values, and a model that can reasonably fit your data. Lack any of those things, and you can have problems.
There is simply no magic way to find good starting values. Period. You can use global solvers. But even global solvers have their own issues. And a global solver will not work when you have a serious probem with your model/data, as I explain below.
Given all that, there are things you can do. My suggestion would have been to use a tool like my fminspleas (found on the file exchange, I could add a link, but that is of no value here, because it will fail in this case too.) Why?
fminspleas reduces the search space in the model you pose to a 2 dimesnional one, instead of 5 dimensions. So, normally, I'd be writing an answer that explains how to use fminsplewas here. But I won't. Why not? Because no tool will fit that model well to that data. No set of starting values will be sufficient.
So first, let me look at the data you provide.
plot(T,V,'.')
My immediate feeling is you have no chance in hell of fitting that model to this data! Yes, you have lots of data. Yes, you have nice smooth data.
The problem is in that your model simply does not have the required shape to fit that data. This means that regardless of your starting values, you won't get a good fit. Just wanting something to work does not mean it will.
Hmm. Why do I claim that your model will not fit that data? The upper end of that curve is essentially PERFECTLY linear, but NOT asymptotic to a constant. Your model predicts that the function will asymptote to a constant. (Think about it. What happens when T --> inf in your model? But what does your data say?)
Let me plot the first derivative of your data.
You need to see that the first derivative, beyond roughly T = 200 or 300, is CONSTANT, but non-zero. The function is not constant, but the derivative is so. That means your model is inconsistent with your data. NO set of starting values will produce a model that fits your data (given that model.) It matters not how hard you push, it will fail.
I could probably suggest other models that will succeed, of course. But not that one. For example, your data looks almost like an arc of a hyperbola, which can be asymptotic to a straight line in each direction. The bottom end does not seem to be approaching a straight line though. Really, in the end, I'd just suggest a spline is a good choice to fit that data.
If I absolutely had to find a nonlinear model that will fit your data, I MIGHT do this.
First, recognize that the data is linear after say T == 300. Tht will give me very good starting values for two terms in the model.
K = T > 300;
polyfit(T(K),V(K),1)
ans =
1.7771e-06 4.0136
Next, I need a model that has a nonlinear part of the right shape for early stages in T, but that goes to zero for larege T. A good example of such a term is a modified exponential like this:
exp(-t/c4 - (t/c5)^3)
where both c4 and c5 are positive numbers. They should be something on the vague order of 100 or so.
ft = fittype('a + b*t + c*exp(-t/d - (t/e)^3)','independent','t')
ft =
General model:
ft(a,b,c,d,e,t) = a + b*t + c*exp(-t/d - (t/e)^3)
mdl = fit(T,V,ft,'start',[4.0136 1.777e-6, -.00001 100 50])
mdl =
General model:
mdl(t) = a + b*t + c*exp(-t/d - (t/e)^3)
Coefficients (with 95% confidence bounds):
a = 4.013 (4.013, 4.013)
b = 2.628e-06 (2.571e-06, 2.685e-06)
c = -0.007357 (-0.007378, -0.007337)
d = 30.27 (30.18, 30.36)
e = 301.5 (193.7, 409.3)
In fact, that does not do too badly, but it misses the slope at the upper end. So a better choice might be to force the constant and slope term to be as polyfit gies me.
P1 = polyfit(T(K),V(K),1)
P1 =
1.77708306027154e-06 4.01357813821169
ft = fittype(@(c,d,e,t) P1(2) + P1(1)*t + c*exp(-t/d - (t/e).^3),'indep','t')
ft =
General model:
ft(c,d,e,t) = P1(2)+P1(1)*t+c*exp(-t/d-(t/e).^3)
mdl = fit(T,V,ft,'start',[-.00001 100 50])
mdl =
General model:
mdl(t) = P1(2)+P1(1)*t+c*exp(-t/d-(t/e).^3)
Coefficients (with 95% confidence bounds):
c = -0.007421 (-0.007422, -0.00742)
d = 33.01 (33, 33.02)
e = 1122 (477.2, 1767)
plot(mdl)
hold on
plot(T,V,'.')
untitled.jpg
That seems to nail things pretty well. The model has ABSOLUTELY NO physical meaning however. A spline would have been more accurate and as meaningful.
  2 Comments
has azz
has azz on 19 Jul 2020
Hi @John could you please show us how to introduce the function fminspleas when it is useful, thanks
John D'Errico
John D'Errico on 19 Jul 2020
Are you asking for the link to fminspleas? It is here:
Download the file, unzip it, then put the resulting directory on your search path.

Sign in to comment.

More Answers (1)

Alex Sha
Alex Sha on 3 Oct 2019
Experience + luck, no any other perfect way to set initial guesses.
Another suggestion is to use some solvers which adopte global optimization algorithms, like Lingo, Baron, 1stOpt, in those solvers, guesses of initial start values are no longer needed.

Community Treasure Hunt

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

Start Hunting!