How do I fit a nonlinear function correctly in matlab

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 ? )

답변 (1개)

Rik
Rik 2018년 11월 18일
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

dh
dh 2018년 11월 18일
Thanks a lot !!
Rik
Rik 2018년 11월 18일
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.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Chemistry에 대해 자세히 알아보기

제품

릴리스

R2018a

질문:

dh
2018년 11월 18일

댓글:

Rik
2018년 11월 18일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by