Error using horzcat Dimensions of arrays being concatenated are not consistent.

There is an error in the line with bold letters:
clear
Ad=[0.941 0.192; -0.576 0.903];
Bd=[.020; 0.192]; Gd=[.020; 0.192]; Sw=0.5;
Dx=[1 0;0 1;0 0]; Du=[0;0;1];
zmin=[-0.4; -1e6; -3]; zmax=[0.4; 1e6; 3];
nx=2;nu=1;nw=1;nz=3;
Q=[1 0;0 1]; R=0.5; S=[0; 0];
P=dare(Ad,Bd,Q,R);
L=inv(R+Bd'*P*Bd)*Bd'*P*Ad; Qf=P;
mpc=1; % set to zero for unconstrained LQOC
delay=0; % set to zero no delay
if(mpc == 1)
N=10; %horizon size
Mu=zeros(nu*N);Mx=zeros(nx*N);Mux=zeros(nu*N,nx*N);
for i=1:N-1
Mu(1+(i-1)*nu:nu+(i-1)*nu,1+(i-1)*nu:nu+(i-1)*nu)=R;
Mx(1+(i-1)*nx:nx+(i-1)*nx,1+(i-1)*nx:nx+(i-1)*nx)=Q;
Mux(1+i*nu:nu+i*nu,1+(i-1)*nx:nx+(i-1)*nx)=S';
end
Mu(1+(N-1)*nu:nu+(N-1)*nu,1+(N-1)*nu:nu+(N-1)*nu)=R;
Mx(1+(N-1)*nx:nx+(N-1)*nx,1+(N-1)*nx:nx+(N-1)*nx)=Qf;
M=[Mu Mux;Mux' Mx]; m=zeros((nu+nx)*N,1);
% Add Slack variables
M=[M zeros(N*(nx+nu),nz);zeros(nz,N*(nx+nu)) 0.0*eye(nz)]
m=[m; 1e6*[1; 1; 10]];
A0u=zeros(nx*N,nu*N);A0x=zeros(nx*N,nx*N);
A0u(1:nx,1:nu)=Bd; A0x(1:nx,1:nx)=-eye(nx);
for i=2:N
A0u(1+(i-1)*nx:nx+(i-1)*nx,1+(i-1)*nu:nu+(i-1)*nu)=Bd;
A0x(1+(i-1)*nx:nx+(i-1)*nx,1+(i-1)*nx:nx+(i-1)*nx)=-eye(nx);
A0x(1+(i-1)*nx:nx+(i-1)*nx,1+(i-2)*nx:nx+(i-2)*nx)=Ad;
end
A0=[A0u A0x]; b0=zeros(nx*N,1);
A0=[A0 zeros(nx*N,nz)]; % Add Slack variables
A1u=zeros(nz*N,nu*N); A1x=zeros(nz*N,nx*N); A1u(1:nz,1:nu)=Du;
b1max=zeros(nz*N,1); b1min=zeros(nz*N,1);
A1slack=zeros(nz*N,nz); % Add Slack variables
A1slack(1:nz,1:nz)=-eye(nz); % Add Slack variables
for i=2:N
A1u(1+(i-1)*nz:nz+(i-1)*nz,1+(i-1)*nu:nu+(i-1)*nu)=Du;
A1x(1+(i-1)*nz:nz+(i-1)*nz,1+(i-2)*nx:nx+(i-2)*nx)=Dx;
A1slack(1+(i-1)*nz:nz+(i-1)*nz,1:nz)=-eye(nz);
b1max(1+(i-1)*nz:nz+(i-1)*nz,1)=zmax;
b1min(1+(i-1)*nz:nz+(i-1)*nz,1)=-zmin;
end
A1=[A1u A1x;-A1u -A1x];
% Add Slack variables
A1=[A1 [A1slack; A1slack];zeros(nz,N*(nu+nx)) -eye(nz)];
end
Error using horzcat
Dimensions of arrays being concatenated are
not consistent.
Error in Untitled10 (line 26)
M=[M zeros(N*(nx+nu),nz);zeros(nz,N*(nx+nu)) 0.0*eye(nz)];

댓글 수: 8

It is working fine in my case.....
Did you just copy word to word?
Also, could you just try solving only '[M zeros(N*(nx+nu),nz);zeros(nz,N*(nx+nu)) 0.0*eye(nz)]' in the command window and see if it's working? I guess as a whole it's just ignoring instead of solving.
Actually this is an example of 2*2 matrix and it's working fine, but it's not working in 6*6 matrix.
TIA
M=[M zeros(N*(nx+nu),nz);zeros(nz,N*(nx+nu)) 0.0*eye(nz)]
It is working fine while code is run. When you run it again onthe command window, obviously it will throw error, as the size of M is now modified.
I am so sorry, but just 1 last thing. Could you see if it's working with these matrices? It's not working for me for 6*6 matrix. TIA
Ad=[0.478969081440410,-22.9347831060310,0,0.137256358561114,0.206216068991871,-1.92578298257640;-3.05094506276960e-05,0.0355133168622329,0,-0.000177637354131665,-6.76055868392685e-06,0.0265441846751822;3.05094506276902e-05,0.743287466209174,0.778800783071405,0.000177637354131653,6.76055868391955e-06,0.154257043717761;0.143460556023548,-150.655973086421,0,0.756656283737142,0.0343140896402799,-30.9725701802351;0.256127472105004,-78.4816875337030,0,0.424108014754793,0.254006097085642,-9.84553327948011;0,0,0,0,0,0.670320046035639];
Bd=[1.44391760017206e-06;-4.38901579232035e-09;4.38901579232036e-09;2.13261679566051e-05;6.97487101920980e-06;0];
Gd=[-0.205728181434440;0.00968483103669374;0.0308938523771156;-6.18883197642767;-1.40822087675432;0.329350383903707];
Dx=[0,0,1000,0,0,0;0,0,0,1,0,0;0,0,0,0,0,0];
Du=[-5;0;1];
Dw=0;
zmax=[-1e6 450 2.9e6];
zmin=[-1e8 350 2e6];
nx=6; nu=1; nw=1; nz=3; Sigw=0.02
Q=diag([1000 1000 1000 1000 1000 1000 ]);R=0.0005;
P=dare(Ad,Bd,Q,R); L=inv(R+Bd'*P*Bd)*Bd'*P*Ad; Qf=P;
S=[0;0;0;0;0;0];
Replace:
L=inv(R+Bd'*P*Bd)*Bd'*P*Ad;
with:
L=(R+Bd'*P*Bd)\(Bd'*P*Ad);

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

도움말 센터File Exchange에서 Aerospace Blockset에 대해 자세히 알아보기

태그

질문:

2019년 5월 10일

댓글:

2019년 5월 10일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by