How to solve ”Unable to perform assignment because value of type ‘cvx’ is not convertible to ‘double’

I have encountered this problem while writing an optimization problem in which the MSE function is called to calculate the mean square error, I would like to ask what is the reason for this error? Thanks!!!

optimization problem:

function [P,f] = update_P(para, c, P, Z, w, g_MMSE,rho)
%Update P using cvx toolbox
% [P,f] = update_P_Rq(para, rho, c, P, Z, w, g_MMSE)
%Inputs:
% para: structure of the initial parameters
% c: equivalent communication channel
% P: beamformer at BS
% a: steering vector
% Z: a positive semidefinite matrix
% w: weight of MSE
% g_MMSE: MMSE receiver
%Outputs:
% P: updated P
% f: optimal value of objective function

cvx_begin

variable P(para.M,para.K) complex

%objective function
f = objective_func(para, w, c, P, g_MMSE, Z,rho);
minimize(f);

subject to
%constrant

norm(P,‘fro’) <= para.Pt;

cvx_end
end

%objective function
function [f] = objective_func(para, w, c, P, g_MMSE, Z,rho)

term_1 =  w'*MSE(para, c, P, g_MMSE);
term_2 = 0;
for k = 1:para.K
    pk = P(:,k);
    term_2 = term_2 + pk'* Z *pk;
end
f = rho*term_1 + term_2;

end

MSE function:

function [e] = MSE(para, c, P, g)
%The Mean Square Error at the MMSE receiber
% [e] = MSE(para, c, r, P, Rq)
%Inputs:
% para: structure of the initial parameters
% c: equivalent communication channel
% P: beamformer at BS
% g: MMSE receiver
%Outputs:
% e: mean square error

% MSE at the MMSE receiver
e = zeros(para.K,1);
for k = 1:para.K
pk = P(:,k);
ck = c(:,k);
gk = g(k);

e(k) = pow_abs(gk, 2) * ( sum(pow_abs(ck'*P,2)) + para.n )...
    - 2*real( gk*ck'*pk ) + 1;

end
end

I think e needs to be declared as an expressiion
expression e(para.k,1)
or
expression e(para.K,1)
rather than e = zeros(para.k,1)

See The Basics — CVX Users' Guide

Thank you for your reply, but I am still confused. e is the column vector returned by the MSE function which I called when I created the optimization function, do you mean I need to declare an expression for e in the optimization problem? Or do I modify e in the MSE function?

It needs to be declared an expression before first use. It will automastically be initialized by CVX to all zeros

Thanks for your help, I tried to add the expression declaration for e but I still get the same error, maybe I didn’t represent the code clearly, the MSE is a separate .m file that I created and the update_P is another .m file. I used the MSE function in the update_P function, specifically when writing the optimization function f, which is returned by objective_func. Now it looks like this:

cvx_begin

variable P(para.M,para.K) complex
expression e(para.K,1)

%objective function
f = objective_func(para, w, c, P, g_MMSE, Z,rho);

minimize(f);
subject to

%constrant
norm(P,‘fro’) <= para.Pt;

cvx_end

end

function [f] = objective_func(para, w, c, P, g_MMSE, Z,rho)

term_1 =  w'*MSE(para, c, P, g_MMSE);
term_2 = 0;
for k = 1:para.K
    pk = P(:,k);
    term_2 = term_2 + pk'* Z *pk;
end
f = rho*term_1 + term_2;

Am I doing it right?

You don’t show where `e is used. It must be declared before it is used, wherever that is.

Actually, I called the MSE function in this equation and the MSE function return value is e, meaning term_1 = w’*e = w’*MSE(para, c, P, g_MMSE). But now that I have declared e in cvx, I still get the same error, and the statement that reports the error is this line: term_1 = w’*MSE(para, c, P, g_MMSE), and the error says: Incorrect use of MSE
Unable to perform assignment because value of type ‘cvx’ is not convertible to ‘double’. So, I still don’t understand why this error occurs.
thank u for your reply!!!

Please show your complete program so readers know exactly what you ran.

Ok, thanks for the suggestion, I’ll try to organize the code related to the Update_P function to make it easier for the reader to understand.
1.First, there is the Update_P function, which is a separate .m file and uses cvx to address optimization issues:

function [P,f] = update_P(para, c, P, Z, w, g_MMSE,rho)

%Update P using cvx toolbox
%Inputs:
% para: structure of the initial parameters
% c: equivalent communication channel
% P: beamformer at BS
% a: steering vector
% Z: a positive semidefinite matrix
% w: weight of MSE
% g_MMSE: MMSE receiver
%Outputs:
% P: updated P
% f: optimal value of objective function

cvx_begin

variable P(para.M,para.K) complex
expression e(para.K,1)

%objective function
f = objective_func(para, w, c, P, g_MMSE, Z,rho);

minimize(f);
subject to
 %constrant
 norm(P,'fro') <= para.Pt;

cvx_end
end

function [f] = objective_func(para, w, c, P, g_MMSE, Z,rho)

term_1 =  w'*MSE(para, c, P, g_MMSE);
term_2 = 0;
for k = 1:para.K
    pk = P(:,k);
    term_2 = term_2 + pk'* Z *pk;
end
f = rho*term_1 + term_2;

end

2.Then, it’s the MSE function that the error statement points to, which is another .m file that I called while writing the objective function f.
Specifically, term_1 = w’*MSE(para, c, P, g_MMSE) error:

function [e] = MSE(para, c, P, g)

%Inputs:
% para: structure of the initial parameters
% c: equivalent communication channel
% P: beamformer at BS
% g: MMSE receiver
%Outputs:
% e: mean square error
% MSE at the MMSE receiver

e = zeros(para.K,1);
for k = 1:para.K
pk = P(:,k);
ck = c(:,k);
gk = g(k);

e(k) = pow_abs(gk, 2) * ( sum(pow_abs(ck'*P,2)) + para.n )...
    - 2*real( gk*ck'*pk ) + 1;

end
end

3.Finally, there is the function that calculates g_MMSE:

function [g] = MMSE_receiver(para, c, P)

%The MMSE receiver
%Inputs:
% para: structure of the initial parameters
% c: equivalent communication channel
% P: beamformer at BS
%Outputs:
% g: MMSE receiver

g = zeros(para.K, 1);
for k = 1:para.K
pk = P(:,k);
ck = c(:,k);
g(k) = pk’*ck / ( sum(pow_abs(ck’*P,2)) + para.n );
end
end

4.It is worth mentioning that para, c, Z, rho are some of the parameters that have been defined and wk is computed as follows:
e_MMSE = MSE(para, c, P, g_MMSE);
w = para.weight ./ e_MMSE;

These are the functions that I think are associated with the error, thanks for your reply!

Your statement
expression e(para.K,1)
is an orphan, because that e is never used, and therefore can be removed from the code.

The function MSE has exactly the code I said not to use. It needs to have
expression e(para.K,1)
rather than
e = zeros(para.K,1);

i think The MSE function is not in the CVX model, so i cannot use expression e(para.K,1)

But e = zeros(para.K,1) means that I create a K-dimensional vector to store the e(k) value obtained by the loop, and in the MSE function I have to output an e to be able to use it in term_1 = w’*MSE(para, c, P, g_MMSE) so I don’t think that such a modification makes sense, or maybe I’m misinterpreting what you’re saying?

Your multiple levels of function calling just serve to make the program confusing to everyone, apparently including yourself. I believe the CVX variable P is passed from the main function eventually through to MSE. You then violated the rule I keep telling you not to do, by assigning some expression involving P into a portion of the double precision array created from your statement e = zeros(para.K,1); That is exactly doing the thing which triggers the error message. Don’t do it.

If you insist on using such a convoluted program structure, maybe you should declare e as an expression in the top level function, then pass that e all the way through to MSE as a function call argument. There would be no other declarations for e anywhere. If you want to see what’s going on, try inserting statements such as whos e or just the statement e to see what MATLAB or CVX says e is… Or do it for any other item.