MATLAB: How to solve this thefun/numel problem

numel myfun

function F = myfun(p)
s1 =(exp(5-p(1)))/(1+exp(5-p(1))+exp(3.5-p(2))+exp(5-p(3)));
s2 =(exp(3.5-p(2)))/(1+exp(5-p(1))+exp(3.5-p(2))+exp(5-p(3)));
s3 =(exp(5-p(3)))/(1+exp(5-p(1))+exp(3.5-p(2))+exp(5-p(3)));
F = [s1+(p(1)-1)*(-s1)*(1-s1)+(p(2)-1)*(s1*s2);
s2+(p(1)-1)*(s1*s2)+(p(2)-1)*(-s2)*(1-s2);
s3+(p(3)-1)*(-s3)*(1-s3)];
p0 = [-5; -5];
options = optimoptions('fsolve','Display','iter');
[p,fval] = fsolve(@myfun,p0,options)
Attempted to access p(3); index out of bounds because numel(p)=2.
Error in myfun (line 2)
s1 =(exp(5-p(1)))/(1+exp(5-p(1))+exp(3.5-p(2))+exp(5-p(3)));
Error in fsolve (line 217)
fuser = feval(funfcn{3},x,varargin{:});
Caused by:
Failure in initial user-supplied objective function evaluation. FSOLVE cannot
continue.

Best Answer

You need to provide fsolve an initial guess for p(3)