Error of inner matrix dimensions must agree


(Steven) #1

Could someone tell me how to obtain information of matrices defined in the cvx. The error shows that ‘Inner matrix dimensions must agree.’ in the cvx program, and I cannot have access to check the matrix dimensions manually from the workspace. Thanks in advance.


(Mark L. Stone) #2

Does whos do what you need? Ot just enter a CVX variable or expression, and information on the type of expression and dimensions will be provided. Or does “I cannot have access to check the matrix dimensions manually from the workspace” mean that you some how can not do this.


(Steven) #3

Can you please help me look at my program?
function [C,K] = RCIset(A,Bu,Bw,x_u,u_u,v)
[n m] = size(Bu);
nw = size(Bw,2);
nx=n;%[-x_bar,+x_bar]
nc=m;%[-u_bar,+u_bar]
Nx=nx;
Nu=nc;
Nn=n;
% state and input constraints
x_ba=x_u;
u_ba=u_u;
T=[ones(nx,n)];
N=[ones(nc,m)];

%disturbance boundaries
d = ones(n,1);

%Compute initial approximations of C,K and W_down
cvx_begin sdp
variable W_down_sqrt(n,n) symmetric;
variable lamda(n,n) diagonal;
variable lamda_hat(n,n) diagonal;
variable Xinv(n,n,Nn) symmetric;
variable P(n,n,Nn) symmetric;
variable Q(n,n,Nn) symmetric;
variable H(n,n,Nn) diagonal;
variable F(n,n,Nn) diagonal;
variable Z(n,n,Nn) diagonal;
variable C_inv(n,n);
variable D(n,n,Nx) diagonal semidefinite;
variable Du(n,n,Nu) diagonal semidefinite;
variable Dx(n,n,Nn) diagonal semidefinite;
variable Dw(nw,nw,Nn) diagonal semidefinite;
variable K_hat(m,n);

ide=eye(Nn);
ide1 = eye(Nu);
ide2 = eye(Nx);

%maximize the volume of invariant set
% use log_det defined in cvx only
maximize(log_det(W_down_sqrt))
subject to

lamda >= 0;
lamda_hat >= 0;
W_down_sqrt >= 0;
[lamda_hateye(n) lamda_hateye(n) zeros(n,n);(lamda_hateye(n))’ C_inv’+C_inv W_down_sqrt;zeros(n,n) (W_down_sqrt)’ lamda_hateye(n)]>=0;
newlamda = diag(lamda);
for i = 1:Nn
%(17)
[P(:,:,i) Z(:,:,i);Z(:,:,i)’ Q(:,:,i)]>=0;
li = 4*(newlamda(i,:)*ide(:,i)’*d-d’*Dx(:,:,i)d-v’Dw(:,:,i)v);
[Q(:,:,i)-Xinv(:,:,i) F(:,:,i)-C_inv H(:,:,i)-C_inv Z(:,:,i)‘ide(:,i);
(F(:,:,i)-C_inv)’ 2
lamda-P(:,:,i) lamda+F(:,:,i)’-Z(:,:,i) zeros(n,1);
(H(:,:,i)-C_inv)’ (lamda+F(:,:,i)’-Z(:,:,i))’ H(:,:,i)’+H(:,:,i)-Q(:,:,i) zeros(n,1);
(Z(:,:,i)'ide(:,i))’ zeros(1,n) zeros(1,n) li]>=0;
%5.18 (18) after congruence transformation
[Dx(:,:,i) zeros(n,nw) (A
C_inv+Bu
K_hat)’;
zeros(nw,n) Dw(:,:,i) Bw’;
(A
C_inv+Bu
K_hat) Bw Xinv(:,:,i)]>=0;
end

for j = 1:Nu
[Du(:,:,j) -0.5*K_hat’*N’ide1(:,j);
(-0.5
K_hat’*N’*ide1(:,j))’ ide1(j,:)*u_ba-d’*Du(:,:,j)*d]>=0;
end

for m=1:Nx
[D(:,:,m) -0.5*(C_inv)’*T’ide2(:,m);
(-0.5
(C_inv)’*T’*ide2(:,m))’ ide2(m,:)*x_ba-d’*D(:,:,m)*d]>=0;
end

cvx_end;

%Return Initial solution
C0_inv = C_inv;
W0_down_sqrt = W_down_sqrt;
W0_down = (W0_down_sqrt)*(W0_down_sqrt);
W0_downinv = inv(W0_down);
K0_hat = K_hat;

%Update the initial solution
for i = 1:Nn
X0(:,:,i) = inv(Xinv(:,:,i));
end

ite = 5;
k = 1;

%Iterations
while (k<=ite)
C0_inv = C_inv;
K0_hat = K_hat;
for i = 1:1:Nn
X0(:,:,i) = inv(Xinv(:,:,i));
end

cvx_begin sdp
variable W_down_sqrt(n,n) symmetric;
variable lamda(n,n) diagonal;
variable lamda_hat(n,n) diagonal;
variable Xinv(n,n,Nn) symmetric;
variable P(n,n,Nn) symmetric;
variable Q(n,n,Nn) symmetric;
variable H(n,n,Nn) diagonal;
variable F(n,n,Nn) diagonal;
variable Z(n,n,Nn) diagonal;
variable C_inv(n,n);
variable D(n,n,Nx) diagonal semidefinite;
variable Du(n,n,Nu) diagonal semidefinite;
variable Dx(n,n,Nn) diagonal semidefinite;
variable Dw(nw,nw,Nn) diagonal semidefinite;
variable K_hat(m,n);

 ide=eye(Nn);
 ide1 = eye(Nu);
 ide2 = eye(Nx);

%maximize the volume of invariant set
maximize(log_det(W_down_sqrt))
subject to

lamda >= 0;
lamda_hat >= 0;
W_down_sqrt >= 0;
%(2,2),(2,3) blocks of (26) is replaced
[lamda_hateye(n),lamda_hateye(n),zeros(n,n);(lamda_hateye(n))’ C_inv’W0_downinvC0_inv+C0_inv’W0_downinvC_inv C0_inv’W0_downinvW_down_sqrt; zeros(n,n) (C0_inv’W0_downinvW_down_sqrt)’ lamda_hateye(n)]>=0;
newlamda = diag(lamda);
for i = 1:Nn
%(17) is replaced by (31)
[P(:,:,i) Z(:,:,i);Z(:,:,i)’ Q(:,:,i)]>=0;
L_11=C_inv’*X0(:,:,i)*C0_inv+C0_inv’*X0(:,:,i)*C_inv- C0_inv’*X0(:,:,i)*Xinv(:,:,i)*X0(:,:,i)*C0_inv;
[L_11 ide(:,i)*newlamda(i,:);
(ide(:,i)newlamda(i,:))’ 4(newlamda(i,:)*ide(i,:)*d-v’Dw(:,:,i)v-d’Dx(:,:,i)d)]>=0;
%The other matrix stays the same
%5.18 (18) after congruence transformation
[Dx(:,:,i) zeros(n,nw) (A
C_inv+Bu
K_hat)’;
zeros(nw,n) Dw(:,:,i) Bw’;
(A
C_inv+Bu
K_hat) Bw Xinv(:,:,i)];
end

for j = 1:Nu
[Du(:,:,j) -0.5*K_hat’*N’ide1(:,j);
(-0.5
K_hat’*N’*ide1(:,j))’ ide1(j,:)*u_ba-d’*Du(:,:,j)*d]>=0;
end

for m=1:Nx
[D(:,:,m) -0.5*(C_inv)’*T’ide2(:,m);
(-0.5
(C_inv)’*T’*ide2(:,m))’ ide2(m,:)*x_ba-d’*D(:,:,m)d]>=0;
end
cvx_end;
k = k+1;
end
C = inv(C_inv);
K = K_hat
C;

The main program is shown below:
%combined Robust model predictive control
clear all
close all
clc

n = 2;% state dim
m = 1;%input dim
n_d = 2;%disturbance dim
N = 5;%horizon length
ite = 100;%iteration times
n_f = m + n; %f = [x;u]
n_z = m + n; %z = [x;u]
N_f = Nn_f;
N_u = N
m;
N_d = N*n_d;
e = ones(n,1);
d = ones(n,1);
[A,Bu,Bw,C,D,Q,R,P] = genMatrix(n,m,n_d);

%Define the constraint for input and state
x_l = [-4;-4];
x_u = -x_l;
u_l = -2.5;
u_u = -u_l;
X_l = kron(ones(N,1),x_l);
X_u = kron(ones(N,1),x_u);
U_l = kron(ones(N,1),u_l);
U_u = kron(ones(N,1),u_u);
d_u = ones(n_d,1);

%find invariant set (RCI set) and inner controller–offline method
[C,K] = RCIset(A,Bu,Bw,x_u,u_u,d_u);

I want to compute C and K, while the error shows in the RCIset function is ‘Inner matrix dimensions must agree’. It seems so strange since in the first cvx program, the same matrix does not show error.


(Mark L. Stone) #4

You have a big non-reproducible mess here. I don’t know what;s what, what you ran, where you got the error, etc.

Try using whos just prior to the error occurring. Look at the dimensions of each item entering into the expression in which the error occurs; if they don’t all show up in whos, then enter each item by itself on a line, which will cause its dimensions to be displayed, Unless there’s an actual bug in CVX, you should be able to diagnose this the same as if this were pure MATLAB, with no involvement of CVX (which, as far as I know, may even be the case).