Error with nlinfit and it said Index exceeds the number of array elements (1)

조회 수: 1 (최근 30일)
Aaron Lee
Aaron Lee 2020년 11월 8일
답변: Rajanya 2024년 11월 26일
I just start learing how to use Matlab, and I was trying to run the nlinfit like this:
ogfun = @(var,x) 1./(1+exp(-(var(1)+var(2)*x)));
var=[1;1]; x=(-10:1:10);
y=logfun(var,x);
plot(x,y);
K>> beta=nlinfit(x,y,logfun,1)
Then it gave me an error said "Error using nlinfit (line 213)
Error evaluating model function '@(var,x)1./(1+exp(-(var(1)+var(2)*x)))'.
Caused by:
Index exceeds the number of array elements (1)."
I know it's a silly question but can anyone tell me why it said that index exceeds the number of array elements and what (1) stands for?

답변 (1개)

Rajanya
Rajanya 2024년 11월 26일
I have been able to reproduce the issue at my end and the error is because the function ‘nlinfit’ demands its ‘beta0’ argument (initial parameter values) to be a vector. Here, 1 is being passed which is interpreted as a scalar. Consequently, when the ‘logfun’ function is evaluated, attempting to access ‘var(2)’ results in this error.
Modifying the ‘nlinfit’ call as shown below resolves the error.
beta = nlinfit(x,y,logfun,[1 1])
To know more about ‘nlinfit’ and its usages, you can refer to the documentation page by executing the following command from the MATLAB Command Window:
doc nlinfit
Hope this helps!

카테고리

Help CenterFile Exchange에서 Nonlinear Regression에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by