1

This is my code in CVX:

load('C')

r=C(:,4);
t=C(:,5);

n = size(C,1);
N = 100;

for i=1:n
    eta(i,1) = randn()/2;
end

cvx_begin
    variable x(n,1)

    maximize r'*x - t'*x

    subject to
        ones(n,1)'*x == N
        x >= zeros(n,1)
        exp(-x/N) >= eta
cvx_end

It gives the following error in the line where the objective function is declared:

“Inner matrix dimensions must agree.”

What am I doing wrong?

The error persists even if I write the last constraint as follows:

for i=1:n
    exp(-x(i,1)/N) >= eta(i,1)
end
Kristada673
  • 3,512
  • 6
  • 39
  • 93
  • Yes, since `r`, `x` and `t` all are `n x 1` vectors, `r'*x - t'*x` should be a scalar. – Kristada673 Aug 08 '17 at 18:13
  • Wait you modified the error message. in which line is the error happening? – Ander Biguri Aug 08 '17 at 18:19
  • @AnderBiguri Yes, I just figured out why the `objective function is not scalar` error was showing. It was because when declaring the variable `x`, I should have declared it as `x(n,1)`, otherwise, as you said, `CVX` would think it is a scalar if I simply declare it as `x`. So now that's fixed, but I have a different error, which is what I have edited the question to reflect. – Kristada673 Aug 08 '17 at 18:22
  • @AnderBiguri In the line `maximize r'*x - t'*x`. – Kristada673 Aug 08 '17 at 18:29
  • My guess is the same: cvx does not know the size of t and r – Ander Biguri Aug 08 '17 at 18:32
  • But I have defined `t` and `r` at the beginning itself as vectors whose dimensions are `n x 1` – Kristada673 Aug 08 '17 at 18:40
  • 1
    @AnderBiguri Got the error. Need to put parentheses around the objective function, which is required because it has 2 terms: `maximize (r'*x-t'*x)` – Kristada673 Aug 08 '17 at 18:45
  • 1
    Answer your own question ;) – Ander Biguri Aug 08 '17 at 18:46

1 Answers1

1

The error is that I did not put parentheses around the objective function, which is required in this particular case as it has 2 terms. So, maximize (r'*x-t'*x) solves the error.

Kristada673
  • 3,512
  • 6
  • 39
  • 93