How do I fit a nonlinear function correctly in matlab
Mostrar comentarios más antiguos
I have the following table, named "test":
0.0037071 0.5
0.015203 1
0.035039 1.5
0.062272 2
0.093988 2.5
0.12776 3
0.16291 3.5
0.19991 4
0.24002 4.5
0.28574 5
0.34696 5.5
0.47879 6
1.8882 6.1125
Now I want do fit a nonlinear function (error function) using matlab:
modelfun = @(b,x)erf(b(1)*x)./b(2) + b(3);
beta0 = [0, 0, 0];
mdl = fitnlm(test,modelfun,beta0)
But I get the following error:
Error using nlinfit (line 247)
No usable observations after removing NaNs in Y and in the result of evaluating MODELFUN at the initial value BETA0.
How can I solve this ?
(and how can I get the final fitted nonlinear function for plotting ? )
Respuestas (1)
Rik
el 18 de Nov. de 2018
I don't have the toolbox that contains the fitnlm so I'm using fminsearch instead:
data=[...
0.0037071 0.5
0.015203 1
0.035039 1.5
0.062272 2
0.093988 2.5
0.12776 3
0.16291 3.5
0.19991 4
0.24002 4.5
0.28574 5
0.34696 5.5
0.47879 6
1.8882 6.1125 ];
x=data(:,2);y=data(:,1);
initial_guess=[0 0 0];
modelfun = @(b,x)erf(b(1)*x)./b(2) + b(3);
f=modelfun;
%objective least squares cost function
OLS=@(b,x,y,f) sum((f(b,x) - y).^2);
opts = optimset('MaxFunEvals',50000, 'MaxIter',10000);
% Use 'fminsearch' to minimise the 'OLS' function
fit_val = fminsearch(OLS, initial_guess(:), opts,x,y,f);
Now the fit_val variable contains the estimated values of b. Now you can generate a new x vector and use your modelfun to calculate the corresponding y values.
2 comentarios
dh
el 18 de Nov. de 2018
Rik
el 18 de Nov. de 2018
Did this suggestion solve your problem? If so, please consider marking it as accepted answer. It will make it easier for other people with the same question to find an answer. If this didn't solve your question, please comment with what problems you are still having.
Categorías
Más información sobre Get Started with Curve Fitting Toolbox en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!