Sums of sdpvars and sparse assignment limited to 2D

81 views
Skip to first unread message

Collin Lutz

unread,
Sep 1, 2013, 9:27:31 AM9/1/13
to yal...@googlegroups.com
Hello,

I am getting the error "Sparse assignment is limited to 2 dimensions" stemming from line 27 below. Could the tildeX assignment on line 20 be causing issues from double/sdpvar confusion as discussed here? If so, how do you build a sum of sdpvars?

Also, what is the correct way to initialize variables that will have sdpvars in their structure (e.g., XBlock)? If I initialize XBlock = zeros(nx+nw,nx+nw,N), I run into troubles with the double/sdpvar confusion. Should I instead initialize XBlock = sdpvar(nx+nw,nx+nw,N)? In the code below, I do not initialize XBlock, tildeX, or brl at all.

  1. global A
  2. global B
  3. global C
  4. global D
  5. global N

  6. loadSystemMats();

  7. P = [ 0.3 0.7 ; 0.3 0.7 ];

  8. [nx,dum]=size(A(:,:,1));
  9. [nz,dum]=size(C(:,:,1));
  10. [dum,nw]=size(D(:,:,1));

  11. X = sdpvar(nx,nx,N,'symmetric','real');
  12. F = [];

  13. for i = 1:N
  14.    M(:,:,i) = [ A(:,:,i), B(:,:,i) ; C(:,:,i), D(:,:,i) ];
  15.    tildeX(:,:,i) = zeros(nx,nx);

  16.    for j = 1:N
  17.       tildeX(:,:,i) = tildeX(:,:,i) + P(i,j).*X(:,:,j);
  18.    end

  19.    tildeBlock(:,:,i) = [ tildeX(:,:,i), zeros(nx,nz) ; zeros(nz,nx), eye(nz) ];
  20.    XBlock(:,:,i) = [ X(:,:,i), zeros(nx,nw) ; zeros(nw,nx), eye(nw) ];
  21.    brl(:,:,i) = M(:,:,i)' * tildeBlock(:,:,i) * M(:,:,i) - XBlock(:,:,i);

  22.    F = [ F, X(:,:,i) >= 0, brl(:,:,i) <= 0 ];
  23. end
  24. options = sdpsettings('solver','sedumi');
  25. diagnostics = solvesdp(F,[],options)
  26. checkset(F)
Thank you.
Collin
Message has been deleted
Message has been deleted

Johan Löfberg

unread,
Sep 1, 2013, 9:41:24 AM9/1/13
to yal...@googlegroups.com
The easiest for you is to simply avoid using a 3D matrix. Just put the slices in cells instead, Xtilde{i} = ...

The way you define Xtilde (etc) does not work anyway do to a design feature in MATLAB
http://users.isy.liu.se/johanl/yalmip/pmwiki.php?n=Extra.NANInModel

Collin Lutz

unread,
Sep 1, 2013, 10:21:42 AM9/1/13
to yal...@googlegroups.com
Thanks for the quick response! Should I initialize blocks that will contain other sdpvars as sdpvars or doubles? For example, which of the following would be correct?


for i = 1:N
   XBlock{i} = zeros(nx+nw,nx+nw);
end

OR


for i = 1:N
   XBlock{i} = sdpvar(nx+nw,nx+nw);
end

In the code below, I initialize tildeX{i} to be zeros and it seems to work. Here's the code in case it will help anyone else in the future:

    1. global A
    2. global B
    3. global C
    4. global D
    5. global N
    6. loadSystemMats();
    7. P = [ 0.3 0.7 ; 0.3 0.7 ];
    1. [nx,dum]=size(A{1});
    2. [nz,dum]=size(C{1});
    3. [dum,nw]=size(D{1});
    1. for i = 1:N
    1.     X{i} = sdpvar(nx,nx,'symmetric','real');
    2. end
    1. F = [];
    2. for i = 1:N
    1.    M{i} = [ A{i}, B{i} ; C{i}, D{i} ];
    2.    tildeX{i} = zeros(nx,nx);
    1.    for j = 1:N
    1.       tildeX{i} = tildeX{i} + P(i,j).*X{j};
    2.    end
    3.    tildeBlock{i} = [ tildeX{i}, zeros(nx,nz) ; zeros(nz,nx), eye(nz) ];
    4.    XBlock{i} = [ X{i}, zeros(nx,nw) ; zeros(nw,nx), eye(nw) ];
    5.    brl{i} = M{i}' * tildeBlock{i} * M{i} - XBlock{i};
    6.    F = [ F, X{i} >= 0, brl{i} <= 0 ];

    Johan Löfberg

    unread,
    Sep 1, 2013, 11:04:37 AM9/1/13
    to yal...@googlegroups.com
    you don't have to predefine anything when you work with cells

    This is perfectly ok code in MATLAB
    for i = 1:10
     Z
    {i} = something
    end

    where something is what ever you want, and Z is undefined when you start



    Johan Löfberg

    unread,
    Sep 1, 2013, 11:05:11 AM9/1/13
    to yal...@googlegroups.com
    line 17 is completely redundant

    Collin Lutz

    unread,
    Sep 1, 2013, 11:18:55 AM9/1/13
    to yal...@googlegroups.com
    If I comment out line 17, I get an "Undefined variable "tildeX" or class "tildeX" error.

    Johan Löfberg

    unread,
    Sep 1, 2013, 11:21:27 AM9/1/13
    to yal...@googlegroups.com
    Of course, since you (redundantly) reference the zero object Xtilde on line 19

    Collin Lutz

    unread,
    Sep 1, 2013, 11:26:38 AM9/1/13
    to yal...@googlegroups.com
    Is there a better way to build \sum_{j=1}^{N} P(i,j) .* X_j ? Thanks for your comments and patience.

    Johan Löfberg

    unread,
    Sep 1, 2013, 11:28:39 AM9/1/13
    to yal...@googlegroups.com
    Looks like the easiest approach. You can probably write it using kron and some nasty reshapes, but I don't see why one would like to do that

    Collin Lutz

    unread,
    Sep 1, 2013, 11:35:43 AM9/1/13
    to yal...@googlegroups.com
    Okay. Thanks. Under the hood, is tildeX{i} being cast as an sdpvar each time X{j} is added?

    Johan Löfberg

    unread,
    Sep 1, 2013, 11:44:06 AM9/1/13
    to yal...@googlegroups.com
    tildeX{i} is what ever you have placed in tildeX. No casting or anything happening. A cell object is a container for anything

    X{1} = sdpvar(2);
    X
    {2} = pi;
    X
    {3} = 'bananas';


    Collin Lutz

    unread,
    Sep 1, 2013, 12:08:15 PM9/1/13
    to yal...@googlegroups.com
    Ok. Thanks again!
    Reply all
    Reply to author
    Forward
    0 new messages