The following cvx variable(s) have been cleared or overwritten:

Hi,

I am trying to run this code:

for istep=1:length(param_range)

param = param_range(istep);
p = param; % Aliasing.

% Some arbitrary possibly non-convex param sweep.
F_param = [p 0 0
0 0 p
0 p 0];

cvx_clear
cvx_begin
variable X(3,3);
variable v(3);
expression X(3,3)
X = X0 + F_param + [0 v(2) 0
0 0 v(3)
v(1) 0 0];
minimize norm_nuc(X);
subject to
v(1) >= lower_bounds(1);
v(2) >= lower_bounds(2);
v(3) >= lower_bounds(3);
v(1) <= upper_bounds(1);
v(2) <= upper_bounds(2);
v(3) <= upper_bounds(3);

cvx_end
end

It gives me the captioned error. I am wondering what could be the mistake. Thanks a lot

You have separately declared X as a variable and as an expression. Remove the declaration of X as a variable. You can also remove the declaration of X as an expression if you want.

Sorry for the late follow-up. Even when I change it to something like this, the error still persists.

cvx_clear
cvx_begin
variable X(3,3);
variable v(3);

minimize norm_nuc(X);
subject to
v(1) >= lower_bounds(1);
v(2) >= lower_bounds(2);
v(3) >= lower_bounds(3);
v(1) <= upper_bounds(1);
v(2) <= upper_bounds(2);
v(3) <= upper_bounds(3);
X = X0 + F_param + [0 v(2) 0
0 0 v(3)
v(1) 0 0];

cvx_end

I wonder where am I missing out. Thanks.

You got the message because you still have variable X(3,3); . Remove that line from your program. And then you need to have X = X0 + F_param + [0 v(2) 0;0 0 v(3)l;v(1) 0 0]; before you use X in the objective.

Also note that although the following code is correct

v(1) >= lower_bounds(1);
v(2) >= lower_bounds(2);
v(3) >= lower_bounds(3);
v(1) <= upper_bounds(1);
v(2) <= upper_bounds(2);
v(3) <= upper_bounds(3);

you could instead use
lower_bounds <= v <= upper_bounds

So your complete program could be

cvx_begin
variable v(3);
X = X0 + F_param + [0 v(2) 0;0 0 v(3);v(1) 0 0];
minimize norm_nuc(X);
subject to
lower_bounds <= v <= upper_bounds
cvx_end

Alternatively, you could keep the declaration of X as a variable, and change what is now an expression assignment on X into a constraint.

cvx_begin
variable v(3); 
variable X(3,3);
minimize norm_nuc(X);
subject to
lower_bounds <= v <= upper_bounds
X == X0 + F_param + [0 v(2) 0;0 0 v(3);v(1) 0 0];
cvx_end

I’'m not sure, but I think CVX may present the exact same model (equally efficient) to the solver in either case.

Or you can just use the expression for X directly in the objective.

    cvx_begin
    variable v(3); 
    minimize(norm_nuc(X0 + F_param + [0 v(2) 0;0 0 v(3);v(1) 0 0]))
    subject to
    lower_bounds <= v <= upper_bounds
    cvx_end

Yeah, before I saw your reply, I’d already tried the penultimate code snippet you mentioned and it worked. Thanks a lot for your inputs!