How can I solve this myfun/numel problem?
Show older comments
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.
Accepted Answer
More Answers (1)
Image Analyst
on 11 Nov 2013
If p has 2 elements, you'll have to use ./ instead of /, like this:
s1 =(exp(5-p(1))) ./ (1+exp(5-p(1))+exp(3.5-p(2))+exp(5-p(3)));
Same for the other equations. For F, use .* (dot star) instead of * (star).
Categories
Find more on Matrices and Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!