How to estimate parameter values from the nonlinear function

4 views (last 30 days)
Dear all,
I would like to get some parameter values from the nonlinear function. I added the code below, and I got some errors. I would like to know how to fix this. And if I want to get all positive values, then I would like to know which command I need to add.
Thank you very much in advance.
Sincerely yours,
J1
Time = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16];
Energy = [530,556,599,644,675,684,719,757,815,865,891,937,943,944,977,1011];
Capital = [0.9315,0.9272,0.9245,0.9231,0.9270,0.9243,0.9189,0.9115,0.9012,0.9075,0.9089,0.9113,0.9128,0.9087,0.9071,0.9051];
Emission = [1835,1918,2063,2213,2311,2326,2457,2582,2790,2940,3029,3179,3166,3163,3294,3350];
ds = table(Time',Energy',Capital',Emission');
modelfun = @(b,x) b(1).*exp(-b(2)*x(:,1))-b(3)*x(:,3).^b(4).*x(:,2);
pguess = [0.02,0.2,0.01,0.02];
mdl = fitnlm(ds,modelfun,pguess)
  2 Comments
Star Strider
Star Strider on 2 Sep 2018
When I run your code, it throws this error:
Error using nlinfit>checkFunVals (line 649)
The function you provided as the MODELFUN input has returned Inf or NaN values.
You must solve that problem.
Also, when you post code (and if you want other people to be able to read it easily), highlight it and then click on the [{}Code] button. (I did that for you this time.)
Redwood
Redwood on 2 Sep 2018
Dear Star,
Could you tell me how to fix this problem in more details?
Thank you very much for your help.
Sincerely yours,
J1

Sign in to comment.

Answers (2)

Star Strider
Star Strider on 2 Sep 2018
‘Could you tell me how to fix this problem in more details?’
One way is to scale ‘Energy’ or ‘Emission’ or both of them (as I did here) by some appropriate value (I chose 1E-3), then do the regression and scale the parameters appropriately afterwards:
Time = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16];
Energy = [530,556,599,644,675,684,719,757,815,865,891,937,943,944,977,1011]*1E-3;
Capital = [0.9315,0.9272,0.9245,0.9231,0.9270,0.9243,0.9189,0.9115,0.9012,0.9075,0.9089,0.9113,0.9128,0.9087,0.9071,0.9051];
Emission = [1835,1918,2063,2213,2311,2326,2457,2582,2790,2940,3029,3179,3166,3163,3294,3350]*1E-3;
ds = table(Time',Energy',Capital',Emission');
modelfun = @(b,x) b(1).*exp(-b(2)*x(:,1))-b(3)*x(:,3).^b(4).*x(:,2);
pguess = [0.02,0.2,0.01,0.02];
mdl = fitnlm(ds,modelfun,pguess)
CI = coefCI(mdl) % Parameter Confidence Intervals
producing:
mdl =
Nonlinear regression model:
Var4 ~ b1*exp( - b2*Var1) - b3*Var3^b4*Var2
Estimated Coefficients:
Estimate SE tStat pValue
_________ _________ _______ __________
b1 1.8787 0.047118 39.872 8.1224e-16
b2 -0.039205 0.0022691 -17.278 7.7382e-11
b3 -0.089595 0 -Inf 0
b4 551.86 0 Inf 0
Number of observations: 16, Error degrees of freedom: 14
Root Mean Squared Error: 0.109
R-Squared: 0.958, Adjusted R-Squared 0.955
F-statistic vs. constant model: 319, p-value = 4.96e-11
CI =
1.7776e+000 1.9798e+000
-44.0717e-003 -34.3381e-003
NaN NaN
NaN NaN
The NaN confidence intervals mean that the parameters are highely significantly different from zero (in the context of the other statistics on them).
Experiment to get the result you want.
‘Thank you very much for your help.’
My pleasure.
  4 Comments
Redwood
Redwood on 3 Sep 2018
Dear Star,
I would like to get all positive b values.
Do you know how to get them?
Sincerely yours,
J1
Star Strider
Star Strider on 3 Sep 2018
You will have the use the Optimization Toolbox lsqcurvefit (link) function instead of fitnlm.

Sign in to comment.


John D'Errico
John D'Errico on 3 Sep 2018
Edited: John D'Errico on 3 Sep 2018
The simplest way to apply positivity constraints on the parameters is to use a tool that will allow you to enforce them. That might be lsqcurvefit or lsqnonlin, from the optimization toolbox.
It appears that fitnlm or nlinfit, its cousin from the stats toolbox, do not allow bound constraints on the parameters. Without looking, I think that fit (curvefitting toolbox) does not give you parameter constraints either.
So, if you have the optimization toolbox, just use it.
If not, then your needs can be met by a transformation of the parameters. Squaring them, for example, is a simple way to ensure the result is always positive. The problem is, this is not always trivial to explain to someone. So you could also use my fminpleas, tool, found on the file exchange.
Next, you need to provide intelligent starting values for the parameters. Exponents ALWAYS seem to cause problems, since they are so sensitive to small deviations.
Finally, if the parameter seriously wants to go negative, that is often a good indication that either you chose terrible starting values, OR that your model is simply incorrect, OR that your data is simply inadequate to estimate the model you have posed. Which of the above scenarios holds is difficult to know for sure without my doing some analysis.
At the very least, my gut tells me that your data is inadequate. Why? Because I've never seen a novice user of optimization/estimation tools provide good, sufficient data. As well, it appears that your independent variables are HIGHLY correlated. Sorry, but how can I say nicely that your data is probably pure crap? The odds are good that estimating 4 parameters from this data will be highly suspect. But what do I know? ;-)
Sadly, I don't have the time right now to go into some depth. But some testing on my part shows that your data does not seem to support positive coefficients on all 4 of those parameters. As well, your highly correlated data makes it impossible to know if the problem is just terribly poor data, or a terribly poor choice of model, inadequate to fit that data.

Categories

Find more on Descriptive Statistics 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!