MATLAB: How to fix Error using trustnleqn (line 28) Objective function is returning undefined values at initial point. FSOLVE cannot continue.

fsolveinitial pointsolve

Hi, could anyone help me? I am having this error.
Error using trustnleqn (line 28) Objective function is returning undefined values at initial point. FSOLVE cannot continue.
Error in fsolve (line 388) trustnleqn(funfcn,x,verbosity,gradflag,options,defaultopt,f,JAC,…
global W_RMPL lambda0_RMPL n m1_RMPL
n = 5;
lambda0_RMPL = -3.9465;
W_RMPL = [-0.806323524683253,-1.40611713252880,-0.867814301186637,1.91256242324179,0.0628391323302569];
m1_RMPL = sum(W_RMPL)/n;;
fun_RMPL = @root2d_RMPL;
x0_RMPL = [-0.6140,0.2490];
options = optimset('Display','off');
S_RMPL = fsolve(fun_RMPL ,x0_RMPL ,options)
%function
function E = root2d_RMPL(x)
global W_RMPL n m1_RMPL lambda0_RMPL
E(1) = ((m1_RMPL - x(1))./x(2)) - (lambda0_RMPL./n).*sum(normpdf((lambda0_RMPL.*(W_RMPL -x(1))./x(2)))./normcdf((lambda0_RMPL.*(W_RMPL -x(1))./x(2))));
E(2) = (-1) + (1/n).*sum(((W_RMPL -x(1)).^2)/(x(2).^2)) - (lambda0_RMPL./(n)).*sum(((W_RMPL -x(1))./x(2)).*normpdf((lambda0_RMPL.*(W_RMPL -x(1))./x(2)))./normcdf((lambda0_RMPL.*(W_RMPL -x(1))./x(2))));
Thank you.

Best Answer

Only you know what your function is supposed to return, but I assume it is not this:
>> fun_RMPL(x0_RMPL)
ans =
NaN NaN
You must make it so that fun_RMPL returns a finite value.