fitnlm giving Inf or NaN values
11 views (last 30 days)
Show older comments
I am trying to use fitnlm to fit a range of equations to data. It works fine for a power relationship, e.g.
modelfun = @(b,x) b(1)*x.^b(2);
beta0 = [1 1];
X=[1694 3000 3464];
Y=[0.556167635253459 1 1.13624749173126];
mdl = fitnlm(X,Y,modelfun,beta0);
But I am getting errors for an exponential:
modelfun = @(b,x) b(1)*exp(b(2)*x);
beta0 = [1 1];
X=[1694 3000 3464];
Y=[0.556167635253459 1 1.13624749173126];
mdl = fitnlm(X,Y,modelfun,beta0);
%But I get this error
% Error using nlinfit>checkFunVals (line 649)
% The function you provided as the MODELFUN input has returned Inf or NaN values.
%
% Error in nlinfit (line 251)
% if funValCheck && ~isfinite(sse), checkFunVals(r); end
%
% Error in NonLinearModel/fitter (line 1127)
% nlinfit(X,y,F,b0,opts,wtargs{:},errormodelargs{:});
%
% Error in classreg.regr.FitObject/doFit (line 94)
% model = fitter(model);
%
% Error in NonLinearModel.fit (line 1446)
% model = doFit(model);
%
% Error in fitnlm (line 99)
% model = NonLinearModel.fit(X,varargin{:});
I chucked the same values in Excel and it can create an exponential relationship no problem, but I have 336 relationships to derive so I'd rather do it in Matlab! :) I would also like to try this equation, which runs through with these values with a warning the model is overparameterised, but on one of the full sets (not given here for space) I get the same error as above.
%y=a((x+c)^b)
modelfun = @(b,x)b(1)*((x + b(2)).^b(3));
beta0 = [1 1 1];
X=[1694 3000 3464];
Y=[0.556167635253459 1 1.13624749173126];
mdl = fitnlm(X,Y,modelfun,beta0);
Any idea where I am going wrong?
Thanks for your help!
4 Comments
Torsten
on 22 Feb 2022
Edited: Torsten
on 22 Feb 2022
For the third model equation, you have 3 data points to determine 3 parameters.
Usually, you should get parameters that reproduce the data exactly. But this is not fitting, but interpolating. The parameters have no physical significance.
Why for the third model function you get the same error as above for a larger dataset, I can't tell. Maybe because x+b(2) becomes negative. Then (x+b(2)).^b(3) becomes complex-valued.
Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!