# CVX optimization for 2 dimensions variables

Dear All,
I have written my code :
`CVX `

n=10;
m=20;
L_tot=[1:1:10];

``````       cvx_begin
variable f(n,m); % two dimensional matrix
variable L(n,m); % two dimensional matrix
dual variables A B C D E F
minimize (sum(f.^2)+sum(power(2,2.*L/200)));
subject to
A: sum(L)-L_tot==0;
B: -L<=0;
C: -R<=0;
D: f-1000<= 0;
E: L<=2*f ;
F: 2*L-R<=0;
cvx_end
``````

but, Unfortunately, I am unable to solve the problem and this problem exist:

“Your objective function is not a scalar.”

but, the results seems scalar!!!

Thanks all.

To the extent possible, CVX tries to adhere to the same conventions MATLAB does with its functions. For example, when `X` is a matrix, `SUM(X)` is a vector, not a scalar.

So that’s your problem here: quantities like `sum(f.^2)` are not scalars.

This is how I would write your objective: `sum_square(vec(f))+sum(pow(2,2.*vec(L)/200))`

Note that `pow(2,x)` is just `exp(log(2)*x)`, which is subject to the standard warning about the use of exponentials, logarithms, etc. in CVX models.

I’ll only address the first term in the objective function, and leave the second term as an exercise for the OP.

f.^2 is an n by m matrix, hence sum(f.^2) is a 1 by m vector with the ith element being the sum of the ith column of f.^2. Hence it is not a scalar.

Your objective function needs to be scalar.

I checked: sum_square(vec(f))+sum(power(2,2.*vec(L)/200)), that is fine but, what is your opinion this solution for main aforementioned object to be: sum_square(f(:))+sum(power(2,2.*L(:)/200))?

That is fine too.