Search code examples
matlabcomplex-numbersnonlinear-optimization

Problem with separating Real and Imaginary parts for fmincon (constrained MATLAB optimization). How to program it correctly?


I am trying to solve this optimization problem with fmincon function in MATLAB:

enter image description here

Where all H are complex matrices, g and Pdes are complex column vectors, D0 and E0 are numbers.

I expect to get complex column vector of g (and in general its should be complex), So I divided problem in two parts: real and imag, but it does not work, MATLAB returning me a message:

Not enough input arguments.

Error in temp>nonlincon (line 17)
c(1) = norm ( H_d*([g(1)+1i*g(4); g(2)+1i*g(5); g(3)+1i*g(6)]) )^2 - D_0;

Error in temp (line 12)
        = fmincon(objective,x0,[],[],[],[],[],[],nonlincon);

Where I am wrong?

And in general am I right in writing given problem in the following way:?

% For example:
D_0 = 2*10^(-5)*10^(60/10);
E_0 = 50;
H_b = rand(15,3) + 1i*rand(15,3);
P_des = rand(15,1) + 1i*rand(15,1);
H_d = rand(10,3) + 1i*rand(10,3);

    objective = @(g) (norm ( H_b*([g(1)+1i*g(4); g(2)+1i*g(5); g(3)+1i*g(6)]) - P_des ))^2;                        
x0 = ones(1,3*2)';
options = optimoptions('fmincon','MaxFunctionEvaluations',10e3);
[X,FVAL,EXITFLAG,OUTPUT,LAMBDA,GRAD,HESSIAN]...
        = fmincon(objective,x0,[],[],[],[],[],[],nonlincon);

% So I expect to get a column vector g: 1st 3 elements - Real part, next 3 - Imag

function [c,ceq] = nonlincon(g, H_d, E_0, D_0)
c(1) = (norm ( H_d*([g(1)+1i*g(4); g(2)+1i*g(5); g(3)+1i*g(6)]) ))^2 - D_0;                        
c(2) = (norm ([g(1)+1i*g(4); g(2)+1i*g(5); g(3)+1i*g(6)]))^2 - E_0;
ceq = [];
end

Solution

  • You just need to specify that nonlincon is a function of variable g only when calling fmincon

    The code is as follow

    [X,FVAL,EXITFLAG,OUTPUT,LAMBDA,GRAD,HESSIAN]...
            = fmincon(objective,x0,[],[],[],[],[],[],@(g)nonlincon(g, H_d, E_0, D_0));
    

    Solution X

    X = [0.2982; 0.1427; 0.3597; -0.0729; -0.1187; 0.2090]