# Cannot maximize a(n) complex affine expression

Recently , I find this problem when I do a simulation. And I don’t know how to do for the error. The CVX function code is following:
cvx_begin sdp
variable B((MM),(MM))
variable t(NN*(NN-1)/2,1)

  AAA=trace(Ba)*Trace(B*Ca)-trace(Ba*xk(:,:,k));
BBB=numb*t;
CCC=AAA-lamda*BBB;
dual variables P{NN*(NN-1)/2}
dual variable Q
maximize(CCC)
subject to
......
cvx_end


Result on matlab give:
??? Error using ==> maximize at 36
Disciplined convex programming error:
Cannot maximize a(n) complex affine expression.

Error in ==> mutual at 145
maximize(CCC)

where Ba and Ca are known matrices, xk is a matrix which change with iterations.
The error message is saying that CCC is complex—that is, it has both a real and an imaginary part. It is ill-posed to try and maximize or minimize a complex expression: after all, what does it mean to say x\leq y, if x and y are complex?
Now, sometimes you intend for your expression to be real, but due to roundoff errors, there remains a small imaginary residual. If that’s the case here, you can just do maximize(real(CCC)).
On the other hand, if you’re trying to maximize the magnitude of CCC—well, CVX can’t do that, because you can’t maximize a convex expression in CVX, and complex magnitude is convex.
It is also possible (because I’ve seen this kind of mistake before) that you simply need to think more about what you are trying to do with your model; that CCC isn’t really the correct objective.