Summation inside a loop in CVX

Hello,

Suppose I have the following optimization problem

maximize f(x,y)
x(N,2),y
subject to 
sum(norm(x(i+1,:)-x(i,:))+norm(x(i+1,:)-x(i,:))^3)<=y

where x is a matrix variable and y is a scaler variable. I have a doubt about how to implement the above summation in CVX. Based on your previous answers, the guide, and MATLAB, I tried to do the following in CVX

variables x(N,2),y,s

maximize f(x,y)

subject to 

for i=1:N-1
s>=s+norm(x(i+1,:)-x(i,:))+pow_pos(norm(x(i+1,:)-x(i,:)),3)
end
s<=y

As you can see, I introduced a new variable, s, to represent the summation. I have a doubt about the introduction of the variable s. Please, will s properly hold the summation? Or, is there a better way of doing this? How should it be if y was a scaler constant instead of a variable?

Your help is much appreciated,

Ali

Don’t declare s as a variable,. Let is be an expression, which doesn’t have to be declared.

I don’t think it’s necessary, but it won’t hurt to start with
s = 0
prior to a for loop.
Then have inside the for loop
s = s + <ith term to be added>
which is an expression assignment, not a constraint.

Do not use >= as you have.

You may also find norms to be convenient.for some of your problems.

Mark,

Thanks a lot for your guidance. I have used the following based on your advice:

variables x(N,2),y
expression s
maximize f(x,y)
subject to
s=0;
for i=1:N-1
s=s+norm(x(i+1,:)-x(i,:))+pow_pos(norm(x(i+1,:)-x(i,:)),3)
end
s<=y

Just verifying, I noticed in the guide that expressions are usually addressed with index (e.g. i) inside the loop, so is it still ok not to index the expression s here? Also, if y is a constant, does the constraint s<=y still hold here?

Ali

Yes to both questions.

You can, but don’t need to declare expressions if they are scalars, but may need to when they are vectors. See http://cvxr.com/cvx/doc/basics.html#assignment-and-expression-holders