How can I formulate contraints which are defined in an FOR loop?



how can I construct an optimization problem where the constraints are defined in a FOR loop?

Easy Example:
variables x(5) y
minimize y + sum(x)
for i = 1:5
y + x(i) = 1

I now that in this problem I could write each constraint seperatly but in my case the number of equations depend on an input integer and hence I cannot do this.

Thanks for your help and your time.


Not sure what [Solved] means, so perhaps you’ve figured it out.

Nevertheless, just put whatever constraints you want in a for loop in a straightforward way.
Of course, you need to use == for an equality constraint, not = .

In your particular example, you can use a single vectorized constraint rather than needing to use a for loop.

y + x == 1

will work.

I’m sure your real problem will not be unbounded as this one is.

Hey Mark,

thank you very much for your help. This was exactly my problem.