# Errors because of a PSD matrix

Hello,

my code faces the following errors only because matrix H is semidefinite. Whenever I remove the keyword semidefinite, the code has no error. But, in fact matrix H must be a PSD matrix. Could you please help me fix this proble? Thank you for your help and time.

cvx_begin SDP

``````cvx_solver SEDUMI

variable y

variable M1(m + q + 1, m + q + 1)
variable M2(m + q + 1, m + q + 1)
variable M(m + q + 1, m + q + 1)
variable W(m + q + 1, m + q + 1)
variable H(m + q + 1, m + q + 1) semidefinite
variable G(m + q + 1, m + q + 1)

variable alphaa(q , 1)
variable betaa(q , 1)
variable gamaa(m , 1)

maximize ( y )

subject to:

%===================== 1st Constraint set ==========================

for i = 1 : q, M1 == M1 + alphaa(i , 1) * [2*b(i)  -1*Ap(i,:) ; -1*Ap(i,:)' zeros(m + q , m + q)]; end

for i = 1 : q, M2 == M2 + betaa(i , 1) * [b(i)^2  zeros(1, m + q) ; zeros(m + q, 1) -1*Ap(i,:)'*Ap(i,:)]; end

M == M1 + M2;
%===================== 2nd Constraint set ==========================

for i = 1 : m, W == W + gamaa(i , 1) * [0 -1*E(:,i)' ; -1*E(:,i) 2*E(:,i)*E(:,i)']; end

%===================== 3rd Constraint set ==========================
for i = 1 : m + q + 1
for j = 1 : m + q + 1

G(i , j) >= 0;

end
end

%===================== 4th Constraint set ==========================

[-1*y  cp' ; cp  Qp ] == M + W + H + G;
``````

cvx_end

%********************************************* Errors ***************************************************

## Calling SeDuMi 1.34: 694 variables, 0 equality constraints

Status: Error
Optimal value (cvx_optval): NaN

Error using sparse
Index exceeds matrix dimensions.

Error in pretransfo (line 444)
dblks = cumsum(full(sparse(1,istrt,1,1,sdpL)));

Error in sedumi (line 261)
[A,b,c,K,prep,origcoeff] = pretransfo(A,b,c,K,pars);

Error in cvx_run_solver (line 50)
[ varargout{1:nargout} ] = sfunc( inputs{:} );

Error in cvx_sedumi>solve (line 245)
[ xx, yy, info ] = cvx_run_solver( @sedumi, At, b, c, K, pars, ‘xx’, ‘yy’, ‘info’, settings, 5 );

Error in cvxprob/solve (line 429)
[ x, status, tprec, iters ] = shim.solve( At, b, c, cones, quiet, prec, solv.settings, eargs{:} );

Error in cvx_end (line 88)
solve( prob );

Error in Knapsack_Copositive_problem25 (line 139)
cvx_end

Have you tried other solvers (sdpt3 or mosek, or if using CVX 3.0beta, scs)?

If you are using CVX 3.0beta, try CVX 2.1.

Thank you for your help. Only MOSEK can solve this problem without Errors.

What error message did you get with sdpt3?

Error using *
Inner matrix dimensions must agree.

Error in cvx_sdpt3>solve (line 405)
x(1,tvec{k}) = x(1,tvec{k}) + xx{k}(:)’ * xvec{k};

Error in cvxprob/solve (line 429)
[ x, status, tprec, iters ] = shim.solve( At, b, c, cones, quiet, prec, solv.settings, eargs{:} );

Error in cvx_end (line 88)
solve( prob );

Error in Knapsack_Copositive_problem25 (line 118)
cvx_end

I have one more question about my code for you.Two of my constraints include summation (You can see them in the uploaded image. I coded them as the first and second constraints in CVX. Have I coded these constraints correctly (in terms of summations). Thank you for your help and time.

I believe your code is in error. You are actually issuing a separate constraint on M1 for each of the q times through the loop. Similarly for M2 and W. The only way these (erroneous) constraints can be satisfied is with `alphaa = betaa = zeros(q,1)` and `gamaa = zeros(m,1)`. I think if you check your run without the psd constraint, you will see that is the result.

Instead, don’t declare M1, M2, M, or W at all (you could declare them as expression , but that is not necessary). Insert `M1 = zeros( m + q + 1;` and `M2 = zeros( m + q + 1)` and `W = zeros( m + q + 1)` before the for loops. Then use `=` rather than `==` in the for loops. This builds up CVX expressions for M1, M2, and W. And then `M = M1 + M2`

Constraints on G are not wrong, but can be expressed more compactly without for loops as `G(:) >= 0` ., which imposes element-wise constraints on G.

Despite yout CVX code having not implemented the model you wanted, there apparent;y is a bug somewhere between CVX, sdpt3 and sedumi which you encountered.

I have not put any effort into determining whether the fixed up model makes any sense from an optimization modeling perspective. I leave that to you.

Thank you so much. The Errors were fixed completely. Now, all solvers can solve the problem correctly. Therefore, the problem was related to the coding of the constraints including summations not solvers.