MATLAB: Is it possible to use the FSOLVE function in the Optimization Toolbox to solve complex equations

complex variable optimizationOptimization Toolbox

A function of a complex variable "z" is called a complex function "f(z)". I have the following complex function:
f(z) = z^2 - z + 1
How do I determine the zeros of this complex function?

Best Answer

The FSOLVE function cannot directly handle complex-valued functions or variables. However, a complex-valued function of a complex variable "z" can also be thought of as a system of 2 real-valued equations in 2 real variables.
Consider the following example:
f(z) = z^2 - z + 1
To solve this complex function using the FSOLVE function, we first create the following function file:
function F = myfun(X)
z = X(1, :) + j*X(2, :); % Create complex value from real and imaginary parts
f = z.^2 - z + 1; % Evaluate complex function
F = [real(f); imag(f)]; % Separate real and imaginary parts
Then, using the initial guess "z = 1 + 2j", the function can be solved by the following command:
[z fval] = fsolve(@myfun, [1; 2])
which produces the results:
z =
0.5000
0.8661
fval =
6.2523e-005
Therefore, the complex value that produces a local minimum of "abs(f(z))" is found to be:
z = 0.5 + i*0.8661
For information on how to optimize complex functions using the Genetic Algorithm and Direct Search Toolbox, see the Related Solution.