2

I am using trace in my CVX (MATLAB) code. Sometimes it is working fine, but sometimes it is showing some errors. Error message is as follows-

"trace" previously appeared to be used as a function or command, conflicting with its use here as the name of a variable. A possible cause of this error is that you forgot to initialize the variable, or you have initialized it implicitly using load or eval.

I searched in CVX website and I found this-

trace(Z) is valid only if the elements along the diagonal have the same curvature.

I could not make sense out of it. Looking forward for any generous suggestion.

I excerpt a part of my MATLAB code below to clearly state my problem.

The program where it is working properly-

%% Calling CVX Package
    G = zeros((M+1)*d,(M+1)*d);
    for i = 0:M
        G(i*d+(1:d),i*d+(1:d)) = eye(d);
    end
    cvx_precision best;
    cvx_begin
    variable G((M+1)*d,(M+1)*d) semidefinite           % Defining variables
    minimize(trace(C*G))                               % Objective function
    subject to
    % Constraints
    for i = 0:M
        G(i*d+(1:d),i*d+(1:d)) == eye(d);
    end
    cvx_end

The program where it is giving errors-

%% Calling CVX Package
Q = zeros(N,N);
    Mij = zeros(N,N);
    cvx_precision best;
    cvx_begin
    variable Q(N,N) semidefinite                       % Defining variables
    minimize(trace(Q))                                 % Objective function
    subject to
    % Constraints
    for i = 1:N-1
        for j = i:N
            if E(i,j) ~= 0
                Mij = Mij-Mij;
                Mij(i,j) = -1;
                Mij(j,i) = -1;
                Mij(i,i) = 1;
                Mij(j,j) = 1;
                trace(Mij*Q) = E(i,j);
            end
        end
    end
Falko
  • 17,076
  • 13
  • 60
  • 105
Rajat
  • 249
  • 1
  • 11
  • The error message is pretty clear in explaining the issue. `trace` in the CVX package is defined as a function. In your provided logic statement you're defining it as an array (`trace(Mij*Q) = E(i,j);`) which is causing a scope conflict. – sco1 May 18 '15 at 11:55
  • `E(i,j)` is a number not an array. So, my next doubt is, if that is the issue for the error, then what is the way to define such kind of constrains in CVX? Thanks for your help, @excaza . – Rajat May 18 '15 at 14:37
  • Thanks for your help again. I rectified my mistake. Really, it was a very silly mistake and question to ask. To others, if you are facing the same problem like mine, then change your code as follows- `trace(Mij*Q) == E(i,j);` – Rajat May 18 '15 at 14:49
  • Yes, I know `E(i,j)` is not an array, the issue is with setting `trace(Mij*Q)` equal to `E(i,j)`, because `trace` is supposed to be a function but that line defines it as an array. You need to address that line and either remove it or use a different variable name. Regarding your second comment I would caution against comparing two (I'm assuming) floats for equality due to rounding errors without incorporating a tolerance. See: [What Every Computer Scientist Should Know About Floating-Point Arithmetic](http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html) – sco1 May 18 '15 at 15:52
  • @Rajada, if you solved the problem, please paste an answer for others. – Royi Jul 25 '15 at 14:09
  • Hey hi @Drazick, check my last comment. You will find the answer. – Rajat Aug 04 '15 at 05:30

0 Answers0