Solving a cost function

Trying to minimize the following cost function:

for n=1:20
minimize ((sum((max(cumsum((reshape (((reshape(squeeze(u(n,:,:)), [],1)).*r), n2, T))))))*C1)+(sum((max(cumsum((reshape (((reshape(squeeze(u(n,:,:)), [],1)).*t), n2, T))))))*C2);
end

In my code I’ve kept the cost function within a for loop. The reason I did this way is to avoid the error “Invalid constraint: {real affine} == {convex}”. In the above code r and t are binary variables declared under CVX as r (n2,T) & t (n2,T) . C1 & C2 are constants.

The error is as below:
An objective has already been supplied for this problem.

Can anyone help me to resolve it?

A CVX program (from cvx_begin to cvx_end) may have
no minimize or maximize statements
or
1 minimize statement
or
1 maximize statement

Anything else will result in an error.

You are not allowed to specify 20 minimize statements, which is what you have done.

Do you want a paradigm such as,

Objective = 0
for n=1:20
  Objective = Objective + some_convex_expression
end
minimize(Objective)

This uses assignments, rather than equality constraints, and is allowed, provided some_convex_expression is entered consistent with CVX’s rules.

Thanks @Mark_L_Stone