Use of ``expression" in the objective function leads to confusing results

I have the following example where I would like to minimize x+y where x in [0,1] and y in [0, 1]. If I use minimize(z) where an expression z is assigned as z =x+y, I get an output which says ``Status: Solved, Optimal value (cvx_optval): +0" but the returned values are x=0.5, y=0.5, z=1.0. Please see the below code for the example. Since z is an expression and not (necessarily) an optimization variable, the documentation suggests one should use “=” instead of “==”, so I chose to use z=x+y instead of z==x+y. (

If I use “minimize(x+y)” as the objective, I obtain the expected results where x and y are zero (up to reasonable numerical accuracy). If I define “z==x+y” and “minimize(z)”, I again obtain the correct results.

In this simple example, it is of course possible to fix this issue easily. But for more complicated problem instances with multiple intermediate calculations, this is not convenient and the above issue creates a quite confusing situation where one seems to obtain wrong results. Hence, I am wondering whether I violated a rule about what not to do with ``expressions" in the below formulation or this is a bug.



variable x
variable y  
expression z

subject to
x <= x_bound;
y <= y_bound;

You need to have z=x+y before z is used in the minimize statement, not after.

If you add a line z just prior to minimize(z) , you will see

 z =
    cvx zero expression (scalar)

That is because CVX expressions are initialized to 0. So indeed all your program does is to minimize 0, subject to the constraints. Hence cvx_optval =0, and feasible values of x and y are returned.

Constraints can be in any order in the program, before and/or after the objective. providing that everything in them has been declared (and expressions used therein have been suitably assigned prior to use in the constraint).

Thank you very much! In retrospect, the perfect zero for the optimum value should have been my cue to suspect I was not solving the problem I intended to.