MATLAB: How to minimize a nonlinear function

MATLABmatlab functionnonlinearoptimization

Hi everyone, I heve tried to solve a nonlinear equation in matlab where x0 = [0;1;2] is the initial value for search
So I wrote a function :
function f = myfun(x)
f = (1./(1+(x1-x2).^2)) + sin.*(0.5*pi.*x2.*x3) + exp(((-x1+x)./x2)-2).^2)
end
Then:
x1 = 0; x2 = 1; x3 = 2; x0 = [x1;x2;x3];
options = optimoptions('fsolve','Display','iter');
[x,fval] = fsolve(@myfun,x0,options)
… But the following error appears:
"Caused by:
Failure in initial objective function evaluation. FSOLVE cannot continue."
How can i fix that.
I thank you.

Best Answer

There are issues with the definition of 'myfun'. Compare it with the following to see the errors.
function f = myfun(x)
x1 = x(1);
x2 = x(2);
x3 = x(3);
f = (1./(1+(x1-x2).^2)) + sin(0.5*pi.*x2.*x3) + exp(((-x1+x)./x2)-2).^2;
end