# Performing arithmetic operations within CVX optimization block (MATLAB)

I am trying to mimic the results obtained in the following paper, but am having some trouble with arithmetic operations that are used to create constant values within the CVX optimization block. If we consider a radial vector `r = [rx, ry]`, I am trying to perform the arithmetic operation `b = -1/norm( r )^3`, which is a non-convex expression. However, the expression is calculated after the vector `r` has been calculated by CVX at iteration `k`. For example, let’s assume that CVX has performed `k` iterations, and on the `k`th iteration it finds that `r` should be `r = [1, 2]`. What I would like to be able to do is then set the variable `b = -1/norm( r )^3`, which in this case is `b = -0.0894`, and use `b` as a constant value at iteration `k+1` in CVX. Is such a thing possible? I’ve tried to define `b` as an “expression” at the start of the CVX block, but I still seem to be getting DCP errors of the form:

Disciplined convex programming error:
Illegal operation: {convex} .^ {3}
(Consider POW_P, POW_POS, or POW_ABS instead.)

I’ve also tried using `b = -inv_pos(pow_pos(norm( r ),3))`, but get the error:

Disciplined convex programming error:
Illegal operation: pow_p( {convex}, {-1} )

If r is a CVX variable, then after CVX_end, r will have a numerical value determined by the now completed optimization. Until such time as r is declared a variable in a new CVX optimization, you can do whatever you want with it following the normal MATLAB rules.

``````cvx_begin
variable r
...
cvx_end
b = -1/norm( r )^3; % b now has a numerical value as a MATLAB variable
% feel free to use b as a constant (MATLAB variable) in a new CVX invocation (in a loop if you want)
``````

Hi Mark, I’m hoping to use `b = -1/norm(r)^3` inside the `cvx_begin` and `cvx_end` block, and after the variable `r` has been calculated for the current iteration (that is, CVX has not completed the optimization process, and will use the value calculated by `b` in its next iteration). So, for instance, I would have something of the form:

cvx_begin
variable r

Do things with r

b = -1/norm( r )^3
cvx_end

Since CVX uses iterative algorithms to perform optimization, I’m hoping to have CVX calculate a value for `r` at iteration `k`, and then use that numerical value of `r` to set `b = -1/norm(r)^3` which will then be used in the next iteration (`k+1`) of CVX’s optimization algorithm. I hope that makes sense.

r is not “calculated” until after cvx_end. So as you have used it, b is a CVX expression, subject to all the CVX DCP rules. If you are running CVX in a loop, you can start with (the first CVX invvocation) an initial value of b as a MATLAB variable. Then update it after CVX_end using the just computed optimal r. Then use this updated value of b in the next CVX invocation.

Note that
`b = -pow_p(norm(r),-3)`
is not a valid CVX expression because the argument of pow_p for power < 0 must be concave, which norm is not.

You wrote

that is, CVX has not completed the optimization process, and will use the value calculated by b in its next iteration

I am not sure what you mean by that. You can’t change anything in the middle of solver iterations to solve a given CVX problem (cvx_begin to cvx_end). However, if cvx_begin to cvx_end is inside a loop, then each time through that loop is a separate CVX problem. If you are calling each such CVX problem an iteration, then you can take the results from the previous CVX_problem to define input data for the next CVX problem, as I described in my 1st paragraph of this answer.

1 Like