# How can I express 1/xy as a convex function using DCP rules

How can I express 1/xy as a convex function using DCP rules

Please be consistent between the title and question as to what you want. Ans use parentheses and multiplication sign (` *` )as appropriate if you were evaluating a double prevision expression in MATLAB.

I suppose you are looking for `prod_inv`

Hi, thank you for your answer, but I want to express 1/xy as a convex function in matlab.
prod_inv does not work for express 1/xy in matlab.

Hi, thank you for your answer, I want to express 1/xy as a convex function in matlab.

`prod_inv(x,y) = 1/(x*y)` presuming that `x > 0, y > 0`, which `prod_inv(x,y)` enforces.

If however you mean `(1/x)*y`, that is a linear fractional, which is not convex.

Hi, thank you for your answer, but in matlab I have a Error using cvx/prod_inv

Error using cvx/prod_inv
Second argument must be a positive integer.

You probably want to call it with one vector argument:
`prod_inv([x y])`

Do not call it as `prod_inv(x,y)` which I’m guessing is what you did.

The 2nd argument is optional, intended for use with matrix or higher dimensional array.

help prod_inv

prod_inv inverse of the product of a positive vector.
For a real vector, matrix, or X, prod_inv(X) returns 1.0 ./ PROD(X) if
the elements of X are all positive, and +Inf otherwise.

``````For matrices, prod_inv(X) is a row vector containing the inverse
product of each column of X. For N-D arrays, prod_inv(X) is an array of
inverse products taken along the first non-singleton dimension of X.

prod_inv(X,DIM) takes inverse products along the dimension DIM of X.

prod_inv(X,DIM,P), where P is a positive real constant, computes
prod_inv(X).^P. This is slightly more efficient than the equivalent
POW_POS(prod_inv(X),P).

Disciplined convex programming information:
prod_inv(X) is convex and nonincreasing in X; therefore, when used
in CVX specifications, its argument must be concave or affine.
``````

Hi, thank you for your answer, The code I wrote in MATLAB is as follows, but it still gives an error despite using the prod_inv [x y] command, please check my code.

clc
clear all
cvx_begin sdp
variables x y
minimize( prod_inv([x y]) )
subject to
x<=8;
2*y<=4 ;
cvx_end

When the program runs, the following error is displayed

Error in cvx/prod_inv (line 37)
geo_mean( cat( dim, x, y ), dim, [ ones(n,1) ; p ] ) >= 1; %#ok

Error in ex00 (line 5)
minimize( prod_inv([x y]) )

There is a bug in `prod_inv` which I had discovered in 2018, but forgotten about.

In `cvx/functions/@cvx/prod_inv.m` on the line in which the error message occurs, change `ones(n,1)` to `ones(length(x),1)` . Or equivalently, insert a new code line before that: `n = length(x);`

That fix is correct at least for a vector input, as you have. I’m not sure whether it is correct for the general case with N-D array and a second argument.

Hi, thank you for your answer. I have a new question. How to obtain the values of the vector v by knowing the values of the matrix Q in the relation Q = V * V in matlab ?

That is off-topic, as it is not a CVX question. Nevertheless, today is your lucky day, because I will tell you what to do.

I presume you mean `Q = V*V'` (you did not include `'`). So such a column vector `V` will exist if `Q` is psd and rank one, so that `Q` has only one non-zero eigenvalue. Use `eig(Q)` to find the eigenvector `a` corresponding to the non-zero eigenvalue, `b`.
Then `V = sqrt(b)*a`.
`V = -sqrt(b)*a` also works.

How to fix error {convex} + {complex constant} ?

Why is there a complex constant in your model? Should there be? It is your problem and model, so you should understand that, and figure out what to do.

You should look at the value of the complex constant. if it has only a roundoff-level imaginary part, you can use `real(complex_constant)` to eliminate it. if that is not the case, you need to figure out what your model should be; and if that optimization problem is not convex, CVX is not a proper tool to solve it.

my code is

clc
clear all

NR = 5; %number of eav receive antennas
NT = 5; %number of eav transmit antennas

M = 5 ;
K = 1:M;

% n =rand(M)
% %n = rand(M,M)+1i*randM,M)
%
% %n = randi(5,M,M)
% c1 = diag(n)
% theta_r_0 = diag(c1)

%n = rand(M,M)+1i*randM,M)

%n = randi(5,M,M)
% c2 = diag(n)
% theta_t_0 = diag(c2)

ro1 = 4
ro2 = 4

w = 10

Pe_vdb = 0:2:4;%dB
Pe_v = 10.^(Pe_vdb./10);%watt

ep = 10^(-7)

Ps = 10.^(15./10);%watt;

d1 = 1; %distance of R & E (m)
d2 = 1; %distance of S & R (m)
d3 = 1; %distance of S & D (m)
d4 = 1; %distance of R & D (m)
d5 = 1; %distance of E & R (m)

t = 3 ; %taw

Ne = 1 ; %noise variance of E
Nd = 1 ; %noise variance of D

itration = 2;

lambda1 = 1 ;

lambda2 = .1;

lambda3 = 1;

lambda4 = 1 ;

lambda5 = .1;

lambda6 = .1;

Ex1 = zeros(1,length(Pe_vdb));
Ex0 = zeros(1,length(Pe_vdb));

theta_r_0 = diag(K);
theta_t_0 = diag(K);
theta_r_h_0 = diag(theta_r_0)‘;
theta_t_h_0 = diag(theta_t_0)’;
%
theta_r_bar_0 = [theta_r_h_0 1];
theta_t_bar_0 = [theta_t_h_0 1];

Qr_0= (theta_r_bar_0)'(theta_r_bar_0);
Qt_0 = (theta_t_bar_0)'
(theta_t_bar_0);

norm_2_Qt_0 = norm(Qt_0);
norm_2_Qr_0 = norm(Qr_0);

normnuc_Qt = norm_nuc(Qt_0);
normnuc_Qr = norm_nuc(Qr_0);

% maxro=1
% ep1=.01
%
% dife=1
ep1=.01

% while maxro > ep1
% while dife> ep2

``````for n = 1:4
n
``````

for ii = 1:length(Pe_vdb)

``````ii
Q_E = Pe_v(ii);
c = 0;
j  = 0;
c1 = 0;
c0 = 0;
``````

for i = 1:itration

h_RD=sqrt(.5.lambda1)(randn(1,M)+sqrt(-1)*randn(1,M));

h_SR=sqrt(.5.lambda2)(randn(M,1)+sqrt(-1)*randn(M,1));

h_SD=sqrt(.5.lambda3)(randn(1,1)+sqrt(-1)*randn(1,1));

H_ER=sqrt(.5.lambda4)(randn(M,NT)+sqrt(-1)*randn(M,NT));

H_RE=sqrt(.5.lambda5)(randn(NR,M)+sqrt(-1)*randn(NR,M));

H_EE=sqrt(.5.lambda6)(randn(NR,NT)+sqrt(-1)*randn(NR,NT));

u = H_EE + H_REtheta_r_0H_ER;

y_max = 1 + (Q_E/Nd)( ( norm( h_RDtheta_t_0*H_ER) ).^2 );

for z = 1:5;
y = 1 + (y_max./5).*(z-1);

``````   %............. Optimization Problem ..............

cvx_begin  sdp quiet
variable Z(NT,NT) hermitian;
variable s;
variable f_y;

dual variables p1 p2 p3 p4 p5

minimize f_y;

subject to

p1: s == hermitian_semidefinite(1);
p2: trace(Z) == s;
p3: s + (Q_E/Ne)*trace(Z*( u'  )*(  u ) ) == 1
p4: (Q_E/Ne)*trace(Z*H_ER'*theta_t_0'*h_RD'*h_RD*theta_t_0*H_ER) == s .* (y-1)
p5: Z == hermitian_semidefinite(NT)
f_y - (Q_E/(Ne))*trace(Z*(u'*H_RE*theta_t_0*h_SR*h_SR'*theta_t_0'*H_RE'*u))== hermitian_semidefinite(1)
cvx_end

if( strcmp( cvx_status, 'Infeasible' ) == 1 );

Exy(z) = 0;
else
Exy(z) = f_y + (  (  (  (  Ne  )./( Nd ) ) .*( abs( h_SD + h_RD*theta_r_0*h_SR )^2 )  )./y )  ;
end
``````

end

E_xy = nonzeros(Exy);
[l,e] = min(E_xy) ;

``````       if mean(Exy) ~= 0

j = find(Exy==l);
end

y = 1 + (y_max-1)/5*(j-1);
``````

cvx_begin sdp quiet
variable Z(NT,NT) hermitian;
variable s;
variable v;

``````      dual variables p1 p2 p3 p4 p5

minimize v ;

subject to

p1: s == hermitian_semidefinite(1);
p2: trace(Z) == s;
p3: s + (Q_E/Ne)*trace(Z*( u'  )*(  u ) ) == 1
p4: (Q_E/Ne)*trace(Z*H_ER'*theta_t_0'*h_RD'*h_RD*theta_t_0*H_ER) == s .* (y-1)
p5: Z == hermitian_semidefinite(NT)
v -  (Q_E/(Ne))*trace(Z*(u'*H_RE*theta_t_0*h_SR*h_SR'*theta_t_0'*H_RE'*u))== hermitian_semidefinite(1)
cvx_end
%% .......................... W and w_t .............................
if length(cvx_status)==6 && sum(cvx_status == 'Solved')==6;
W     = Z/s ;
h_t   = real(cvx_optval);
[V D] = eigs(W);
d     = real(diag(D));
[d I] = sort(d,'descend');
w_opt = V(:,I(1));
W_opt = W;
else
w_opt = zeros(NT,1);
W_opt = zeros(NT);
h_t   = 0;
end

A = Q_E*( H_EE*w_opt + H_RE*theta_r_0*H_ER*w_opt )*( (H_EE*w_opt + H_RE*theta_r_0*H_ER*w_opt)')+Ne;

Wr = inv(A)*H_RE*theta_t_0*h_SR/norm(inv(A)*H_RE*theta_t_0*h_SR);
``````

end

end
%B = (H_EE + H_REtheta_rH_ER)w_opt(w_opt’)((H_EE + H_REtheta_r*H_ER)')

``````       %Wr = ((inv( (Q_E/Nd).*(B) + eye(NR) )) *H_RE*theta_t*h_SR )/norm( ((inv( (Q_E/Nd).*(B) + eye(NR) )) *H_RE*theta_t*h_SR ))
``````

%
% A = Q_E*( H_EEw_opt + H_REtheta_r_0H_ERw_opt )( (H_EEw_opt + H_REtheta_r_0H_ERw_opt)')+Ne;
%
% Wr = inv(A)H_REtheta_t_0
h_SR/norm(inv(A)H_REtheta_t_0*h_SR);

``````       ak1 = ( diag(h_RD) )*h_SR
``````

ak1_bar = [ak1’,h_SD’]’
Ak1 = ak1_bar*(ak1_bar’)

ak2 = ( diag(h_RD) )H_ERw_opt
ak2_bar = [ak2’,0]’
Ak2 = ak2_bar*(ak2_bar’)

ak3 = ( diag(Wr’*H_RE) )h_SR
ak3_bar = [ak3’,0]’
Ak3 = ak3_bar
(ak3_bar’)

ak4 = ( diag(Wr’H_RE) )H_ERw_opt
b = Wr’H_EEw_opt
ak4_bar = [ak4’,b]’
Ak4 = ak4_bar
(ak4_bar’)
cvx_begin sdp

variables x y T
variable Qt(M+1,M+1) semidefinite;
variable Qr(M+1,M+1) semidefinite;

n = n+1

[Ut Rt] = eig(Qt_0)
m = (max(Rt))
ut = Ut(:,6)
utut’(Qt-Qt_0)

[Ur Rr] = eig(Qr_0)
m = (max(Rr))
ur = Ur(:,6)
urur’(Qr-Qr_0)

Qt_bar_n = norm_2_Qt_0+trace(utut’(Qt-Qt_0));

Qr_bar_n = norm_2_Qr_0+trace(urur’(Qr-Qr_0));

% p = trace(Ak3Qt_0)+trace(Ak2Qt_0)
t1= square( trace(Ak3Qt)-trace(Ak2Qt) )
t2 = ((-2)( trace(Ak3Qt_0)+trace(Ak2Qt_0) )( trace(Ak3Qt)+trace(Ak2Qt) ))
t3 = ((trace(Ak3Qt_0)+trace(Ak2Qt_0)).^2)

t6 = ((-2)( trace(Ak4Qr_0)-trace(Ak1Qr_0) )( trace(Ak4Qr)-trace(Ak1Qr) ))
t7 = (( trace(Ak4Qr_0)-trace(Ak1Qr_0) )^2)

T == [x y]

minimize ( prod_inv(T)+ro1*( norm_nuc(Qt)- Qt_bar_n )+ro2*( norm_nuc(Qr)-Qr_bar_n) )

subject to

4inv_pos(x)+square( trace(Ak3Qt)-trace(Ak2*Qt) )+t2+t3 <= 0;

square( trace(Ak4Qr)+trace(Ak1Qr) )+t6+t7-4*y <= 0;

diag(Qr)+diag(Qt) == eye(M+1,1)

Qt>=0
Qr>=0

cvx_end

Qt_0 = Qt
Qr_0 = Qr

[V_t,l_t] = eig(Qt,‘vector’)
l_t == 0
V_t(:, l==0) = []
l_t(l_t==0) = [];
l_t
th_bart = (V_t)*sqrt(l_t)
th_tilt = th_bart(1:end-1 , theta_t_0 = diag(th_tilt)

[Vr,lr] = eig(Qr,‘vector’)
lr == 0
Vr(:, l==0) = []
lr(lr==0) = [];
lr
th_barr = (Vr)*sqrt(lr)
th_tilr = th_barr(1 :end-1 , theta_r_0 = diag(th_tilr)

% maxro_t = max( norm_nuc(Qt) - norm(Qt) )
% maxro_r = max( norm_nuc(Qr) - norm(Qr) )
%
% if maxro_t<.01 && maxro_r<.01
%
% break
%
% else
% Qt_n = Qt
% Qr_n = Qr
% ro1 = wro1
% ro2 = w
ro2
%
% end
end

% SINR_d = (Ps*(abs(h_SD + h_RDtheta_rh_SR).^2).(d4.^t).(d5.^t) )/( (d3.^t).( Q_E( abs( h_RDtheta_tH_ERw_opt).^2 ) + Nd ) );
% %SINR_e = (Ps
(abs(Wr’H_REtheta_th_SR).^2)) /( (d1.^t).(d2.^t).( Q_E( abs(Wr’H_EEw_opt + Wr’H_REtheta_rH_ERw_opt ).^2 ) + Ne ) );
% if SINR_e >= SINR_d;
%
% c0 = c0 + 1;
% end
%
% % if SINR_e_tzf >= SINR_d_tzf;
%
% % c1 = c1 + 1;
%
% end
%
% Ex0(ii) = c0./itration;
% % Ex1(ii) = c1./itration;
%
%
%
% figure(1)
% hold on
% plot(Pe_vdb,Ex0,‘b’)
% hold on
% % plot(Pe_vdb,Ex1,‘r’)
% %
% grid on
% box on
% %
%
% % f0 =
% %
% % f(n) =
% %
% if

% <=ep

% ro1 = wro1
%
% else
% %
% ro2 = w
ro2
%
% Qt=Qt(n)
% Qr=Qr(n)
%
% end
%
%
%
% %
% %
% % norm_2_Qt_n = ;
% % norm_2_Qr_n = norm(Qr(n));
% % %
% %
% % normnuc_Qt = ;
% % normnuc_Qr = norm_nuc(Qr(n));
% %
% %
% % [Ut Rt] = eig(Qt(n))
% % m = (max(Rt))
% % ut = Ut(:,6)
% % utut’(Qt-Qt(n))
% %
% % [Ur Rr] = eig(Qr(n))
% % m = (max(Rr))
% % ur = Ur(:,6)
% % urur’(Qr-Qr(n))
% %
% % Qt_bar_n = norm_2_Qt(n)+trace(utut’(Qt-Qt(n)));
% %
% % Qr_bar_n = norm_2_Qr_0+trace(urur’(Qr-Qr(n)));
%
%
t2 and t6 are cvx complex affine expressions (scalar)

t3 and t7 are complex constants

Perhaps you can help us by showing the line of code which the error message refers to, and you can show us the numerical values of all the input data which enters into that line of code, and can tell us why that line of code is consistent with the optimization problem being convex.

The lines of code which the error message refers to,

4inv_pos(x)+square( trace(Ak3Qt)-trace(Ak2Qt) )+ ((-2)( trace(Ak3* Qt_0)+trace(Ak2Qt_0) ) ( trace(Ak3Qt)+trace(Ak2 Qt) ))+ ((trace(Ak3Qt_0)+trace(Ak2 Qt_0)).^2) <= 0;

square( trace(Ak4Qr)+trace(Ak1Qr) )+ ((-2)( trace(Ak4 Qr_0)-trace(Ak1Qr_0) ) ( trace(Ak4Qr)-trace(Ak1 Qr) ))+ (( trace(Ak4Qr_0)-trace(Ak1 Qr_0) )^2)-4*y <= 0;

## The numerical values of all input data which enters into those lines of code are given below

Ak3 =

Columns 1 through 5

0.0002 - 0.0000i -0.0023 - 0.0019i -0.0005 - 0.0005i 0.0014 + 0.0002i -0.0001 - 0.0006i
-0.0023 + 0.0019i 0.0380 + 0.0000i 0.0091 + 0.0002i -0.0160 + 0.0093i 0.0058 + 0.0045i
-0.0005 + 0.0005i 0.0091 - 0.0002i 0.0022 - 0.0000i -0.0038 + 0.0023i 0.0014 + 0.0011i
0.0014 - 0.0002i -0.0160 - 0.0093i -0.0038 - 0.0023i 0.0090 + 0.0000i -0.0013 - 0.0033i
-0.0001 + 0.0006i 0.0058 - 0.0045i 0.0014 - 0.0011i -0.0013 + 0.0033i 0.0014 - 0.0000i
0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i

Column 6

## 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i

Ak2 =

Columns 1 through 5

1.4213 + 0.0000i -0.1423 + 0.4648i -0.1329 - 0.2877i -0.0816 - 0.4340i 0.9073 - 1.3589i
-0.1423 - 0.4648i 0.1662 + 0.0000i -0.0808 + 0.0723i -0.1337 + 0.0701i -0.5353 - 0.1606i
-0.1329 + 0.2877i -0.0808 - 0.0723i 0.0707 - 0.0000i 0.0955 + 0.0241i 0.1902 + 0.3107i
-0.0816 + 0.4340i -0.1337 - 0.0701i 0.0955 - 0.0241i 0.1372 + 0.0000i 0.3629 + 0.3550i
0.9073 + 1.3589i -0.5353 + 0.1606i 0.1902 - 0.3107i 0.3629 - 0.3550i 1.8785 + 0.0000i
0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i

Column 6

## 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i

Ak4 =

Columns 1 through 5

0.0342 - 0.0000i 0.0555 + 0.0113i -0.0181 - 0.0083i -0.0276 - 0.0108i 0.0073 - 0.0216i
0.0555 - 0.0113i 0.0938 + 0.0000i -0.0321 - 0.0075i -0.0483 - 0.0083i 0.0048 - 0.0375i
-0.0181 + 0.0083i -0.0321 + 0.0075i 0.0116 - 0.0000i 0.0172 - 0.0010i 0.0013 + 0.0132i
-0.0276 + 0.0108i -0.0483 + 0.0083i 0.0172 + 0.0010i 0.0256 + 0.0000i 0.0009 + 0.0197i
0.0073 + 0.0216i 0.0048 + 0.0375i 0.0013 - 0.0132i 0.0009 - 0.0197i 0.0152 - 0.0000i
-0.0290 - 0.0028i -0.0461 - 0.0141i 0.0147 + 0.0085i 0.0225 + 0.0114i -0.0080 + 0.0177i

Column 6

## -0.0290 + 0.0028i -0.0461 + 0.0141i 0.0147 - 0.0085i 0.0225 - 0.0114i -0.0080 - 0.0177i 0.0248 - 0.0000i

Ak1 =

Columns 1 through 5

0.0097 + 0.0000i 0.0181 - 0.0181i -0.0018 - 0.0113i 0.0083 + 0.0200i 0.0015 - 0.0412i
0.0181 + 0.0181i 0.0673 - 0.0000i 0.0176 - 0.0242i -0.0218 + 0.0526i 0.0796 - 0.0737i
-0.0018 + 0.0113i 0.0176 + 0.0242i 0.0133 - 0.0000i -0.0246 + 0.0060i 0.0474 + 0.0093i
0.0083 - 0.0200i -0.0218 - 0.0526i -0.0246 - 0.0060i 0.0481 + 0.0000i -0.0833 - 0.0384i
0.0015 + 0.0412i 0.0796 + 0.0737i 0.0474 - 0.0093i -0.0833 + 0.0384i 0.1748 + 0.0000i
-0.0506 - 0.0197i -0.1307 + 0.0578i -0.0135 + 0.0622i -0.0028 - 0.1208i -0.0914 + 0.2114i

Column 6

## -0.0506 + 0.0197i -0.1307 - 0.0578i -0.0135 - 0.0622i -0.0028 + 0.1208i -0.0914 - 0.2114i 0.3035 + 0.0000i

Qt_0 =

``````0.2009   -0.0001    0.0012   -0.0013   -0.0038    0.0000
``````

## -0.0001 0.0003 0.0000 0.0000 -0.0000 -0.0000 0.0012 0.0000 0.0003 -0.0000 -0.0000 -0.0000 -0.0013 0.0000 -0.0000 0.0003 0.0000 0.0000 -0.0038 -0.0000 -0.0000 0.0000 0.0003 0.0000 0.0000 -0.0000 -0.0000 0.0000 0.0000 0.0003

Qr_0 =

``````0.7995    0.0000   -0.0000   -0.0000    0.0000   -0.0000
0.0000    0.0003   -0.0000   -0.0000   -0.0000   -0.0000
``````

## -0.0000 -0.0000 0.0003 0.0000 -0.0000 0.0000 -0.0000 -0.0000 0.0000 0.0003 -0.0000 0.0000 0.0000 -0.0000 -0.0000 -0.0000 0.0002 -0.0000 -0.0000 -0.0000 0.0000 0.0000 -0.0000 0.0003

Qt and Qr are optimization variables.

Is there a reason that the left -hand side (LHS) of those inequalities should be real? If so, and if you are sure that the LHS appears complex only due to roundoff-level imaginary term, you can enter `real(LHS) <= 0`. If not what do you intend the inequality `{complex thing} <= 0` to mean? CVX does not allow complex inequalities. If you mean that `real(LHS) <= 0`, you are free to enter that, and CVX should accept it. If you mean `real(LHS) <= 0` and `imag(LHS) <= 0`, you can provide those two inequalities. Perhaps neither of these is what you want. It is your model, not mine, so I don’t know what the correct resolution is.