# Next value of a vector as function of its previous one

Hello,

Suppose I have the following optimization problem:
Maximize f(x)
x
subject to
x[i+1]=x[i]+a

The optimization variable is x, which is a vector of size N. Please, how do I express the constraint x[i+1]=x[i]+a in CVX? I know that to declare a vector variable in CVX, I can write x(N). I tried to make a loop inside CVX to express this constraint but didn’t work. I also searched the user guide for similar problems but couldn’t find any.

Many thanks,

Ali

``````for i=1:n-1
x(i+1) == x(i) + a
end
``````

Note that `diff` would appear to offer a shortcut, but it can;t be used on CVX variables or expressions.

If `a` is a scalar constant (thew same for all `'i`'), instead of the above code, you could just declare `x` as a scalar, and then use `x`, `x + a`, `x + 2*a`, etc. as appropriate where needed.

It is also possible to use expression assignment instead, but I’ll leave that as an exercise for you. http://cvxr.com/cvx/doc/basics.html#assignment-and-expression-holders

Hi Mark,

Thanks again for striving to answer the forum questions!

I could now do it with both your way, where x is declared as a variable, and using expression assignment, i.e. x is declared as an expression. I wanted to know the exact difference between the two. For instance, I saw that, with x declared as a variable, the first value of the vector x is optimized such that x satisfies all the constraints. However, with expression, the first value of the vector x is always initialized as zero unless it is explicitly initialized otherwise. So what do you think please?

Best regards,

Ali

Upon CVX cimpletion, presuming it declares optimum, the optimal value is numerically populated in all CVX variables. However, the numerical values of expressions may not necessarily correspond to the optimum,. The true optimum is used internally so that the problem is correctly solved, but to be sure you have the correct optimal values, you have to compute the CVX expressions using the CVX (optimal) variable values.

1 Like

Thanks a lot. so I’ll stick with variables for now.

Ali