MATLAB: I keep getting this error (Failure in initial user-supplied objective function evaluation. FSOLVE cannot continue)

errorfsolve

My currenct script looks like this:
function F = myfun(x)
x0 = [0.1 0.2 0.3 0.4]
x = x0
f1 = inline('1*(1 - x(1))+100*(-1*x(1)*x(2))');
f2 = inline('1*(2 - x(2))+100*(-1*x(1)*x(2)-1*x(2)*x(3))');
f3 = inline('1*(0 - x(3))+100*(1*x(1)*x(2)-1*x(2)*x(3))');
f4 = inline('1*(0 - x(4))+100*(1*x(2)*x(3))');
F = [f1(x);f2(x);f3(x);f4(x)]
options=optimset('Display','iter');
x = fsolve(@myfun,x0);
end
Still nothing seems to get this working, can anyone help me out? If i do not define my first [x] he will not run because there are not enough input arguments.
Thanks in advance!
Frenk

Best Answer

The call to fsolve and the definition of myfun should take place in two separate workspaces. The function definition should look like this,
function F = myfun(x)
f1 = 1*(1 - x(1))+100*(-1*x(1)*x(2));
f2 = 1*(2 - x(2))+100*(-1*x(1)*x(2)-1*x(2)*x(3));
f3 = 1*(0 - x(3))+100*(1*x(1)*x(2)-1*x(2)*x(3));
f4 = 1*(0 - x(4))+100*(1*x(2)*x(3));
F = [f1;f2;f3;f4];
end
The call to fsolve should be in a different function and look like this,
x0 = [0.1 0.2 0.3 0.4];
options=optimset('Display','iter');
x = fsolve(@myfun,x0,options);
Notice also that there is no need to be using inline() in this situation. Moreover, unless you have a really old version of MATLAB, you should be discontinuing the use of inline() altogether and using Anonymous Functions instead.