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

1 次查看(过去 30 天)
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