# Error trying to use CVX

Hi all,

I am new to CVX. I tried optimizing the shannon rate equation using CVX but i got the folowing error:
" Error in maximize (line 14)
x = evalin( ‘caller’, sprintf( '%s ', varargin{:} ) );

Error in try (line 44)
maximize sum((sum((v.*BB./Rmax{S}).^Umuus{S}(1:U,1:S))).^rho{S}) ". Below are my codes please could someone help/assist?

B = zeros(M,S,U);

for m =1:M
for s=1:S
for u=1:U
x = -q + 2qrand();
y = -q + 2qrand();
du = sqrt(x^2 + y^2);
Ld= 40*(1 -4*(10^-3)h).log10(du./1000) - 18log10(h) + 21log10(f) + 80;
PLdb = Ld + log10(X);
GBSi =10.^-(PLdb/10);
NoB= -174 + 10*log10(RB);

``````        NB= (10.^(NoB/10))/1000;
BB(m,s,u) = RB*log2(1 + (PBSc*GBSi)/(NB))

end
end
``````

end

cvx_begin
cvx_precision(‘best’)
variable v
maximize sum((sum((v.*BB./Rmax{S}).^Umuus{S}(1:U,1:S))).^rho{S})
subject to
sum(sum(v.*RB))<=B;
sum(sum(v))==1;
sum(v)>=0;
sum(v)<=1;
cvx_end
primal_obj=cvx_optval;

You haven’t provided reproducible code. The expression you are trying to maximize involves MATLAB variables which you haven’t shown the values or even dimensions of. Please use the Preformatted text so that symbols such as * don’t get stripped off what is displayed. Your only declared CVX variable is v, which is a scalar, since you have not specified otherwise.

If instead of declaring v as a CVX variable, you assign it a numerical value as a (regular) MATLAB variable, does the expression you are trying to maximize evaluate to a scalar, and do so without MATLAB error? You must get to that point before you can even worry about whether it is a valid CVX expression to maximize.

Anyhow, I don’t think (I’m not sure) you can do something like v.^[.2 .4] in CVX. So yo may have to use a for loop to build up the sum, or find some other way. Also, because you are maximizing, you will need the exponent to be in the range 0 to 1, so that it will be concave.

Hello Mark,

Thanks for the response. Like you pointed out,the expression I am trying to maximize points out to a scalar. Below are my code:

``````clear all
clc

rho= [0.1 0.2 0.2 0.5];
f = 2;
q = 200; % radius of the coverage
U = 2;
B=5e6;
S = 4;
M =3;
h = 45;
X =8 ;
RB=180e3;
PBSc= 20;
N = 1e6;
muus = rand(1,U);
Umuus = muus/sum(muus);
Rmax = [1500 1500 700 300];
Rmin = [1000 1000 400 100];

B = zeros(M,S,U);

for m =1:M
for s=1:S
for u=1:U
x = -q + 2*q*rand();
y = -q + 2*q*rand();
du = sqrt(x^2 + y^2);
Ld= 40*(1 -4*(10^-3)*h).*log10(du./1000) - 18*log10(h) + 21*log10(f) + 80;
PLdb = Ld + log10(X);
GBSi =10.^-(PLdb/10);
NoB= -174 + 10*log10(RB);

NB= (10.^(NoB/10))/1000;
BB(m,s,u) = RB*log2(1 + (PBSc*GBSi)/(NB))

end
end
end

cvx_begin
cvx_precision('best')
variable v
maximize sum((sum((v.*BB./Rmax{S}).^Umuus{S}(1:U,1:S))).^rho{S})
subject to
sum(sum(v.*RB))<=B;
sum(sum(v))==1;
sum(sum(v))=rho_min.*RBsp;
sum(v.*log2(1 + (PBSc*GBSi)/(NB)))>= 36e3;
sum(v)>=0;
sum(v)<=1;
cvx_end
primal_obj=cvx_optval;``````

I don’t know what you’re trying to do in the objective function.

As I suggested before, as part of the debugging process, instead of starting CVX, just assign `v` a numerical value as a MATLAB variable, such as `v = 1`. Then evaluate your objective function. In MATLAB R2014A, I get the MATLAB error message `Cell contents reference from a non-cell array object.` That is a MATLAB error message - CVX was not even invoked. Are you missing some parentheses? If the expression is not a valid MATLAB expression, there is no possibility it will be a valid CVX expression when you declare some of the variables as CVX variables.

I will not make an assessment at this time as to whether your objective function is concave (which is necessary, but not sufficient, for CVX to accept it, given that you are maximizing), because I have no idea what your objective function is supposed to be, Have you proven that you objective function, whatever it is, is concave?