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

5 visualizaciones (últimos 30 días)
Aaron Lee
Aaron Lee el 8 de Nov. de 2020
Respondida: Rajanya el 26 de Nov. de 2024 a las 12:18
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?

Respuestas (1)

Rajanya
Rajanya el 26 de Nov. de 2024 a las 12:18
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!

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by