non linear regression problem. fitnlm gives error
22 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
sepideh
el 18 de Jul. de 2023
Comentada: sepideh
el 18 de Jul. de 2023
Hello
I need to do curve fitting with 2 set of data and I know the model function and initial guess. I used "fitnlm " but it gives error.
would you please check my code and tell me whether I made a mistake or it is the nature of these data.
load FITDATAK
load FITDATAB
tb1= table (FITDATA1,FITDATA)
modelfun= @(b,x) b(1)*x(:,1)^b(2)
beta0= [0.22 -0.5]
md1= fitnlm(tb1,modelfun,beta)
it gives me this errors :
Error using nlinfit>checkFunVals
The function you provided as the MODELFUN input has returned Inf or NaN values.
Error in nlinfit>LMfit (line 596)
if funValCheck && ~isfinite(sse), checkFunVals(r); end
Error in nlinfit (line 284)
[beta,J,~,cause,fullr] = LMfit(X,yw, modelw,beta,options,verbose,maxiter);
Error in NonLinearModel/fitter (line 1153)
nlinfit(X,y,F,b0,opts,wtargs{:},errormodelargs{:});
0 comentarios
Respuesta aceptada
Torsten
el 18 de Jul. de 2023
Editada: Torsten
el 18 de Jul. de 2023
Look at the values of K and B. Do you really want to approximate B by a*K^b ?
format long
K = load("FITDATAK.mat");
K = K.FITDATA1
nnz(isnan(K))
nnz(isinf(K))
B = load("FITDATAB.mat");
B = B.FITDATA
nnz(isnan(B))
nnz(isinf(B))
modelfun = @(b) b(1)*K.^b(2)-B;
beta0= [1e17 1];
sol = lsqnonlin(modelfun,beta0,[],[],optimset('MaxFunEvals',100000,'MaxIter',100000))
norm(modelfun(sol))
5 comentarios
Torsten
el 18 de Jul. de 2023
Editada: Torsten
el 18 de Jul. de 2023
My code from above gives the result of the linearized and the nonlinear problem in one plot.
The result for the nonlinear problem formulation looks far off, but remember that the plot is in loglog scale where the result of the linearized problem is the optimal one.
Más respuestas (1)
Matt J
el 18 de Jul. de 2023
Editada: Matt J
el 18 de Jul. de 2023
Your model function can easily generate NaN's and Infs because with fitnlm there is nothing to bound the b parameters (see below).. It would be better to use fit() with the 'power1' model, and with appropriate bounds on b(2).
load FITDATAK
modelfun= @(b,x) b(1)*x^b(2);
modelfun([1,-100],FITDATA1(50))
modelfun([0,-100],FITDATA1(50))
0 comentarios
Ver también
Categorías
Más información sobre Nonlinear Regression en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!