error:Conversion to double from cvx is not possible

hello，this is my code

``````function[Pr,Rr]=SolveP1(Pr)
global beta0 H  N1 N2  V L d0  tolerance  P_ave p1 p2 M SNR  R S;
L=2000;
N=100;
H=100;
N1=1.0000e-17;
N2=1.0000e-17;
P_ave=5;
beta0= 2*10^(-5);
R=6.7167e+08;
B=2*10^7;
tolerance=0.0001;
tol=tolerance;
M=100;
m = 1 : 1: M;
Q(1,m) = ((L)*(m-1)/(M-1));
Q(2,m)=0;
Qr=Q;
P_begin=10*ones(2,M);
Pl={P_begin};
for l=1:100
itera=l
h_au=beta0./((Qr(1,:)-L*ones(1,M)).^2 +Q(2,:).^2 +(H*ones(1,M)).^2);%constant 1*M
h_ub=beta0./(Q(1,:).^2 +Q(2,:).^2 +H*ones(1,M).^2);%constant  1*M
gama=(Pl{l}(1,:).*Pl{l}(2,:).*h_au.*h_ub )  ./  (Pl{l}(2,:).*h_au*N2 +Pl{l}(1,:).*h_ub*N1+ N1*N2*ones(1,M));%constant 1*M  %constant
gama_inv=(Pl{l}(1,:).*h_au*N2+Pl{l}(2,:).*h_ub*N1+N1*N2*ones(1,M))./(Pl{l}(1,:).*Pl{l}(2,:).*h_au.*h_ub);   %constant
R_itera=B*log2(1+gama);

cvx_begin
variable P(2,M)
expression S
for i=1:1:M
gama_inv1(i)=P(1,i).*h_au(i)*N2+P(2,i).*h_ub(i)*N1+N1*N2 .*(prod_inv([P(1,i) P(2,i)]).*h_ub(i).*h_au(i));
end
S=(gama.^2 *log2(exp(1))  ./ (2*(gama+1)))  .* (gama_inv1-gama_inv);

maximize sum(R_itera-S)

subject to
sum(P(1,:))<=N*20
sum(P(2,:))<=N*20
0<=P(1,:)
0<=P(2,:)
cvx_end
Pr=P
Pl = cat(1, Pl, {Pr});%stored the value in the i iteration and use it in the i+1 iteration
Rl=sum(R_itera-S);
R=[R,Rl];

if (l >= 2) &&(R(l) - R(l-1) < tol)
break;
end
end
save P
f = msgbox('over！', 'Done', 'help');
end
``````

I try to solve a convex problem through iteration until the result converges，the error occured in
for i=1:1:M
gama_inv1(i)=P(1,i).*h_au(i)*N2+P(2,i).h_ub(i)N1+N1N2 .(prod_inv([P(1,i) P(2,i)]).*h_ub(i).*h_au(i));
end
I have referd seveal the same mistake in forum ,such as gama_invl =cvx(1,N) or expression gama_invl and so on ,but it does not work ,the mistake occured in the second iteration,when iteration=1,it work properly
what is werong witn me? thank you!

You haven’t provided a reproducible problem. You haven’t shown us the solver and CVX output from either iteration. Did you carefully examine the output from the first iteration, hence the input for the 2nd iteration?

Conversion to double from cvx is not possible

Did you examine all the items in the statement which triggered the error message?

Here are some possible issues:

The input data is horribly scaled, which can make solving problems numerically unreliable. You are using a crude, unsafeguarded iterative algorithm which might not converge to anything, let alone a local minimum of your original problem.

this is the detail
text1

itera =

`````` 1
``````

Warning: A non-empty cvx problem already exists in this scope.
It is being overwritten.

In cvxprob (line 28)
In cvx_begin (line 41)
In text1 (line 31)

Calling SDPT3 4.0: 1202 variables, 600 equality constraints For improved efficiency, SDPT3 is solving the dual problem.

num. of constraints = 600
dim. of sdp var = 600, num. of sdp blk = 300
dim. of linear var = 302

SDPT3: Infeasible path-following algorithms

number of iterations = 11 primal objective value = 4.56965061e-09 dual objective value = -1.05227239e-18 gap := trace(XZ) = 4.94e-09 relative gap = 4.94e-09 actual relative gap = 4.57e-09 rel. primal infeas (scaled problem) = 3.25e-13 rel. dual " " " = 4.80e-12 rel. primal infeas (unscaled problem) = 0.00e+00 rel. dual " " " = 0.00e+00 norm(X), norm(y), norm(Z) = 4.1e-10, 3.5e+03, 3.5e+03 norm(A), norm(b), norm© = 4.2e+01, 1.0e+00, 1.4e+02 Total CPU time (secs) = 0.85 CPU time per iteration = 0.08 termination code = 0 DIMACS: 3.3e-13 0.0e+00 6.8e-12 0.0e+00 4.6e-09 4.9e-09

Status: Solved
Optimal value (cvx_optval): +3.90777e+10

itera =

`````` 2
``````

The following error occurred converting from cvx to double:
Conversion to double from cvx is not possible.

Error in text1 (line 35)
gama_inv1(i)=P(1,i).*h_au(i)*N2+P(2,i).h_ub(i)N1+N1N2 .(prod_inv([P(1,i)
P(2,i)]).*h_ub(i).*h_au(i));

The order of magnitude of h_au and h_ub are 10^(-7)~10^(-12),

You need to declare `gama_inv1` an expression, i.e., use expression holder. http://cvxr.com/cvx/doc/basics.html#assignment-and-expression-holders

Thank you for your patience！it works now