MATLAB: Fsolve and complex solution

fsolve and complex solution?

Hello,
my test function is f=x^2+1 the slution is easy : x1=i orx2=-i (both are imaginary)
fsolve is not finding these values…why? fsolve returns 0 for a solution??
Thank you.

Best Answer

function F = myfun(ripair)
x = ripair(1) + ripair(2)*1i;
F = x^2+1;
if imag(F) ~= 0; F = 10^5; end %you need to emit finite real value from myfun
end
and make sure your x0 has a pair of values.