How to use CVX in a loop

Hi, and many thanks for running this great forum. I have a problem with CVX in a loop, and have searched your website and there a lot of useful information about loops but I could not implement mine.

for i=1:2
variables x(3,1)
for i=1:2



It is not my exact problem but if I understand this, I can handle mine. When I run this program, I face the “Conversion to double from CVX is not possible.” error. Indeed, I think for the second loop, the program cannot recognize the x variable. How can I run my program with no error? many thanks

I think you model is kind of strange but most likely you can do something a long the line

0<= a
(x(1)-1)^2 <= a
(x(2)-1)^4 <= a
min a

I mean do not use the max with something simpler and equivalent.

Many thanks for your reply. Indeed, it is not my model and I used this example to by random. My main question is that how to run this model in a loop, because when I eliminate the “for” loop, my model works very well

Your original program has multiple issues.

You are using the index i for the inner and outer for loops. That can not be a good thing. It is not clear that you even want an outer for loop (i…e, outside the cvx_begin … cvx_end). If not, then eliminate the outer for loop. You should only have an outer for loop if you want to solve more than one CVX (optimization) problem instance.

The error message you receive is because you are using an expression array (holder) without declaring it as such. See

However, it may be simpler to do something along the lines of WHAT @Erling suggested. In that case you need to declare a as a variable, and use minimizea).

Use an outer for loop if you are trying to solve more than one CVX problem. Use an inner for loop if you need it to specify multiple constraints, or to build up an expression such as the objective function.

Dear Mark, Many thanks for your thorough reply. I used an expression variable and surprisingly it worked. Your kind help was amazing and really rescued me. The Bests