
Error using nlinfit for logarithmic model
8 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Hello,
I'm trying to fit a logarithmic model to my data but I keep geting the following error:
Error using nlinfit>checkFunVals (line 641)
The function you provided as the MODELFUN input has returned Inf or NaN values.
clear all, clc;
y=[0;0.1244; 0.1569; 0.1016; 0.2784; 0.4066; 0.2746; 0.7044; 0.7061;...
0.7054; 0.6752; 0.7102; 0.707];
x=[0; 1; 11; 13; 19; 25; 31; 38; 55; 61; 74; 92; 109];
fun = @(a,x)a(1)*log(x);
a0=0.15;
f = nlinfit(x,y,fun,a0);
0 Commenti
Risposte (2)
Star Strider
il 27 Giu 2019
Of course it will return Inf or NaN values!
Note that:
x=[0; 1; 11; 13; 19; 25; 31; 38; 55; 61; 74; 92; 109];
and
log(0) = -Inf
Try this simple transformation:
fun = @(a,x) x.^a;
a0=0.15;
f = nlinfit(x,exp(y),fun,a0)
figure
plot(x, y, '+')
hold on
plot(x, log(fun(f,x)), '-r')
hold off
to get this reasonably decent fit:

4 Commenti
Image Analyst
il 29 Giu 2019
Modificato: Image Analyst
il 29 Giu 2019
OK, I've done it 4 ways, two models with slightly different rate equations, Star's way, and a log fit with fitnlm(). None of them look like a great fit. Star's model couln't get the fit at (0,0) so I took the residuals at just the second and later points to see which one fit the best. See the plot, created by the attached demo.

Note that the two rate equations are pretty much overlapped so you only see the dark green one.
The residuals are
residuals1 = 1.02835678164379
residuals2 = 1.02842292127149
residualsStarStrider = 1.51350802831932
residualsLog = 1.07621627525508
So it looks like the rate equation is the best. Note that the rate equation will level off at some assymptote (which your data seem to do), while the log fits will head up to y=infinity with increasing x, so that may be another reason to favor the rate equation over the log fit. But, like I said, none of those fits look great and you might want to try a different model, like the s-curve shaped "Boltzman equation".
3 Commenti
Steven Manz
il 24 Feb 2022
Hi @Image Analyst, I tried your example for nonlinear fitting. I used Rate Model 2. I tried using a while loop and allowing beta0 to change to the new coefficient values on every iteration, hoping the fitter would work like the curve fitting toolbox and simply keep iterating until the residuals were under a value of 2.
However, the coefficients/residuals do not seem to change. No matter what I put for beta0, I get the same exact coefficients/residuals every time. Could I be doing something incorrectly? Would you like me to create a separate question for this with my data?
Image Analyst
il 24 Feb 2022
@Steven Manz start your own question and attach your data and script of mine that you modified.
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

