# SDP Objective function must be real

Hello I am trying to solve SDP problem maximize tr(AZ) s.t diag(Z)= I and Z is semi definite matrix.
I wrote this code

``````cvx_begin sdp
variable Z(n,n) hermitian toeplitz
maximize ( trace( phi{m-1}*Z ) );
subject to
diag(Z)==1
Z==hermitian_semidefinite(n);
``````

cvx_end

It gives error
??? Error using ==> cvxprob.newobj at 43
Expressions in objective functions must be real.

Error in ==> maximize at 21
newobj( prob, ‘maximize’, x );
NOTE: Phi{m-1} is written as i will be doing it iteratively but in each iteration I will have phi{m-1} as 64 X 64 matrix.

As the subject says, the objective function must be real. Now, if `Phi` is itself hermitian, then mathematically the trace will be real. But in practice, due to roundoff error, it may not be. Just take the real part, by wrapping the `trace` with a `real` function, and you should be fine.

hey thanks It worked

``````max Tr(𝐖𝐆)

𝑠. 𝑡. 𝐶1: Tr(𝐰'*𝐡𝟏*𝐡𝟏*𝐰) − Tr(𝐰'*𝐡2*𝐡2*𝐰) ≥ 0 𝐶2: 𝐖 ≥ 0
``````

I don’t know how solve this?I’m getting an unreasonable answer. h,g are channels and W beamforming vector.
` 𝐆 = 𝑔'*𝑔`

``````𝐇𝟏 = 𝐡𝟏'𝐡𝟏

𝐇𝟐 = 𝐡𝟐'𝐡𝟐
W=w'*w

g=sqrt(P1/2)*(rand(1,1)+1j*rand(1,1));
h=[sqrt(P1/2)*(rand(1,1)+1j*rand(1,1));
sqrt(P2/2)*(rand(1,1)+1j*rand(1,1))];

cvx_begin sdp
variable W(1,1)
H1=h(1,1)*h(1,1)';
H2=h(2,1)*h(2,1)';
G=g*g';
maximize (real(trace(W*G)));

subject to
real(trace(W*(H1-H2)))>=0 ;

cvx_end``````

The only variable you have declared is `W`, which is a scalar. I presume you want `W` to be a matrix constrained to be semidefinite. The statement `W=w'*w` which is executed prior to CVX being invoked, will be overriden (superseded) by `variable W(1,1)`, which declares W as a 1 by 1, i.e., scalar, CVX variable. That is, the statement `W=w'*w` w accomplishes nothing in your program, because `variable W(1,1) ` is used instead when it matters in the CVX program.

Do you even want to declare or use `W` at all in your program? Perhaps you want to declare `w` as a vector (complex?) variable, and use `w'*w` in place of `W` in your program? If so, then you would not need to constrain `W` to be semidefinite, because it would automatically hold by construction.

Yes exactly, w is complex. for start I’ve assumed I have just one beamforming vector which is a complex one and w’*w is a scalar now. if I have w with several arrays (more than one) then W will be matrix. So, I think for this step W as a scalar is good. Am I right?

I have no idea, because I don’t know what optimization problem you intend to solve.

Perhaps you should write out very carefully the mathematical specification of the optimization problem you wish to solve. Then, construct MATLAB and CVX code to implement that model. if you do thqt even if you don’t show it to anyone else, you will hopefully benefit from the clarity it brings to you, and the discipline it imposes on you.

Your answer did solve my problem but I don’t understand why with maximizing just the real part the problem is solved. For instance, why we don’t consider ‘abs’ since it includes both real and imaginary part…

That is a subject matter domain question, which is not suited for this forum. it’s up to you to figure out what optimization problem to formulate, and the relation between its solution and any real-world application. You will need to seek assistance form subject matter experts - perhaps a professor if you are a student? Or study books and papers.