为什么显示要串联的数组的维度不一致?

8 次查看(过去 30 天)
SuSan jin
SuSan jin 2023-9-7
评论: Torsten 2023-9-7
close all
clear all
clc
% some global
global kappa1;
global kappa2;
global kappa3;
global R11;
global R22;
global R12;
global R21;
global R13;
global R23;
global R33;
global R32;
global R31;
global Q11;
global Q22;
global Q33;
global d1;
global d2;
global d3;
global i;
i=1;
d1=2;
d2=2;
d3=2;
c1=1;
c2=0;
c3=0;
%definitions of R
R11=0.1;
R12=0.01;
R13=0.01;
R31=0.01;
R22=0.1;
R23=0.01;
R33=0.1;
R32=0.01;
R21=0.01;
%definitions of Q
Q11=200*eye(2);
Q22=200*eye(2);
Q33=200*eye(2);
%initial conditions
x0=[1 1 1 1 1 1 rand(1,3) rand(1,3) rand(1,3) [1 1] [0.5 -0.5] [1.5 -1.5] [-1 -1] [2 -2] [0.5 -0.5] [1.5 -1.5] zeros(1,3)];
%call ode
options = odeset('OutputFcn',@odeplot);
[t,x]= ode23(@dynamicsfault,[0 30],x0,options);
ans = 1×2
2 1
ans = 1×2
2 1
ans = 1×2
2 1
ans = 1×2
3 1
ans = 1×2
3 1
ans = 1×2
3 1
ans = 1×2
2 1
ans = 1×2
2 1
ans = 1×2
2 1
ans = 1×2
2 1
ans = 1×2
2 2
ans = 1×2
2 2
ans = 1×2
2 2
ans = 1×2
1 1
ans = 1×2
1 1
ans = 1×2
1 1
Error using vertcat
Dimensions of arrays being concatenated are not consistent.

Error in solution>dynamicsfault (line 297)
xout=[(d1+c1)*(f1-f0+g1*(unew1-u1f))+(d2+c2)*(f2-f0+g2*(unew2-u2f))+(d3+c3)*(f3-f0+g3*(unew3-u3f));...

Error in odearguments (line 92)
f0 = ode(t0,y0,args{:}); % ODE15I sets args{1} to yp0.

Error in ode23 (line 106)
odearguments(odeIsFuncHandle, odeTreatAsMFile, solver_name, ode, tspan, y0, options, varargin);
%plots
% states
figure (1);
subplot(3,1,1)
plot(t,x(:,16:17));
hold on
plot(t,x(:,22),'r');
plot(t,x(:,23),'m');
legend ('$x_{11}$','$x_{12}$','$x_{01}$','$x_{02}$','interpreter','latex');
xlabel('Time (s)');
subplot(3,1,2)
plot(t,x(:,18:19));
hold on
plot(t,x(:,22),'r');
plot(t,x(:,23),'m');
legend ('$x_{21}$','$x_{22}$','$x_{01}$','$x_{02}$','interpreter','latex');
xlabel('Time (s)');
subplot(3,1,3)
plot(t,x(:,20:21));
hold on
plot(t,x(:,22),'r');
plot(t,x(:,23),'m');
legend ('$x_{31}$','$x_{32}$','$x_{01}$','$x_{02}$','interpreter','latex');
xlabel('Time (s)');
figure (2);
subplot(3,1,1)
plot(t,x(:,7:9));
title ('Critic 1');
xlabel('Time(s)')
% figure (3);
subplot(3,1,2)
plot(t,x(:,10:12));
title ('Critic 2');
xlabel('Time(s)')
% figure (4);
subplot(3,1,3)
plot(t,x(:,13:15));
title ('Critic 3');
xlabel('Time(s)')
figure (5);
plot3(kappa1,kappa2,kappa3);
title ('Controls');
figure (6);
plot(kappa1,'r');
hold on;
plot(kappa2,'b');
hold on;
plot(kappa3,'y');
legend ('u1', 'u2','u3');
figure (7)
grid on
plot(t,x(:,1:2));
hold on;
plot(t,x(:,3:4));
hold on;
plot(t,x(:,5:6));
title ('the agent error dynamic','interpreter','latex');
xlabel('Time(s)')
legend ('$\delta_{11}$', '$\delta_{12}$','$\delta_{21}$','$\delta_{22}$','$\delta_{31}$','$\delta_{32}$','interpreter','latex');
function xout=dynamicsfault(t,x)
%global variables
global kappa1;
global kappa2;
global kappa3;
global R11;
global R22;
global R12;
global R21;
global R13;
global R23;
global R33;
global R32;
global R31;
global Q11;
global Q22;
global Q33;
global d1;
global d2;
global d3;
global L1;
global L2;
global c1;
global c2;
global c3;
global i;
%constants
a1=1;
a2=1;
a3=1;
a4=1;
a5=1;
a6=1;
d1=2;
d2=2;
d3=2;
c1=1;
c2=0;
c3=0;
L1=300;L2=100;
delta1=[x(1) x(2)];
delta2=[x(3) x(4)];
delta3=[x(5) x(6)];
W1=[x(7) x(8) x(9)]';
W2=[x(10) x(11) x(12)]';
W3=[x(13) x(14) x(15)]';
x1=[x(16) x(17)];
x2=[x(18) x(19)];
x3=[x(20) x(21)];
x0=[x(22) x(23)];
x1f=[x(24) x(25)];
x2f=[x(26) x(27)];
x3f=[x(28) x(29)];
u1f=x(30);
u2f=x(31);
u3f=x(32);
%定义非线性动力学
f1=[x(17);-0.5*x(16)-x(17)+x(16)^2*x(17)-0.25*x(17)*(cos(2*x(16))+2)^2+0.25*x(17)*sin(4*x(17)^2+2)^2];
g1=[0; cos(2*x(16))+2];
f2=[x(19);-0.5*x(18)-x(19)+x(18)^2*x(19)-0.25*x(19)*(cos(2*x(18))+2)^2+0.25*x(19)*sin(4*x(19)^2+2)^2];
g2=[0; cos(2*x(18))+2];
f3=[-x(21);-0.5*x(20)-x(21)+x(20)^2*x(21)-0.25*x(21)*(cos(2*x(20))+2)^2+0.25*x(21)*sin(4*x(21)^2+2)^2];
g3=[0; cos(2*x(20))+2];
f0=[0.5*x(22)-x(23)*cos(x(22));sin(x(22))-x(23)];
f1f=[x(25);-0.5*x(24)-x(25)+x(24)^2*x(25)-0.25*x(25)*(cos(2*x(24))+2)^2+0.25*x(25)*sin(4*x(25)^2+2)^2];
g1f=[0; cos(2*x(24))+2];
f2f=[x(27);-0.5*x(26)-x(27)+x(26)^2*x(27)-0.25*x(27)*(cos(2*x(26))+2)^2+0.25*x(27)*sin(4*x(27)^2+2)^2];
g2f=[0; cos(2*x(26))+2];
f3f=[x(29);-0.5*x(28)-x(29)+x(28)^2*x(29)-0.25*x(29)*(cos(2*x(28))+2)^2+0.25*x(29)*sin(4*x(29)^2+2)^2];
g3f=[0; cos(2*x(28))+2];
phix1=[x(1)^2 x(1)*x(2) x(2)^2]';
dphix1=[2*x(1) 0; x(2) x(1); 0 2*x(2)];
phix2=[x(3)^2 x(3)*x(4) x(4)^2]';
dphix2=[x(3) 0; x(4) x(3); 0 2*x(4)];
phix3=[x(5)^2 x(5)*x(6) x(6)^2]';
dphix3=[2*x(5) 0; x(6) x(5); 0 2*x(6)];
u1=-0.5*(d1+c1)*inv(R11)*g1'*dphix1'*W1;
u2=-0.5*(d2+c2)*inv(R22)*g2'*dphix2'*W2;
u3=-0.5*(d3+c3)*inv(R33)*g3'*dphix3'*W3;
s1=dphix1*((d1+c1)*(f1-f0+g1*(u1)+(d2+c2)*(f2-f0+g2*u2)+(d3+c3)*(f3-f0+g3*u3)));
s2=dphix2*((d1+c1)*(f1-f0+g1*(u1)+(d2+c2)*(f2-f0+g2*u2)+(d3+c3)*(f3-f0+g3*u3)));
s3=dphix3*((d1+c1)*(f1-f0+g1*(u1)+(d2+c2)*(f2-f0+g2*u2)+(d3+c3)*(f3-f0+g3*u3)));
Y1=(-delta1*Q11*delta1'-u1*R11*u1'-u3*R13*u3'-u2*R12*u2');
Y2=(-delta2*Q22*delta2'-u2*R22*u2'-u1*R21*u1'-u3*R13*u3);
Y3=(-delta3*Q33*delta3'-u3*R33*u3'-u1*R31*u1'-u2*R32*u2');
e1=W1'*s1-Y1;
e2=W2'*s2-Y2;
e3=W3'*s3-Y3;
Win1=-a1*(s1./(s1'*s1+1)^2)*e1';
Win2=-a2*(s2./(s2'*s2+1)^2)*e2';
Win3=-a3*(s3./(s3'*s3+1)^2)*e3';
% sbar1=(s1/(s1'*s1+1));
% sbar2=(s2/(s2'*s2+1));
% sbar3=(s3/(s3'*s3+1));
%
% D1=dphix1*B1*inv(R11)*B1'*dphix1';
% D2=dphix2*B2*inv(R22)*B2'*dphix2';
% D3=dphix3*B3*inv(R33)*B3'*dphix3';
%
% F1=5*eye(length(W4));
% F2=5*eye(length(W5));
% F3=5*eye(length(W6));
%
% Win4=a4*(F1*W1-F1*W4)-0.25*D1*a4*W4*(s1./(s1'*s1+1)^2)'*W1-0.25*a4*dphix3*B3*inv(R33)*R13*inv(R33)*B3'*dphix3'*W4*(s1./(s1'*s1+1)^2)'*W3;
% Win5=a5*(F2*W2-F2*W5)-0.25*D2*a5*W5*(s2./(s2'*s2+1)^2)'*W2-0.25*a5*dphix1*inv(R11)*R21*inv(R11)*dphix1'*W5*(s2./(s2'*s2+1)^2)'*W1;
% Win6=a6*(F3*W3-F3*W6)-0.25*D3*a6*W6*(s3./(s3'*s3+1)^2)'*W3-0.25*a6*dphix1*inv(R11)*R31*inv(R11)*dphix1'*W6*(s3./(s3'*s3+1)^2)'*W1-0.25*a6*dphix2*inv(R22)*R32*inv(R22)*dphix2'*W6*(s3./(s3'*s3+1)^2)'*W2;
if 10<=t&&t<=20
u1f=cos(5*t/(2*pi))+sin(t);
else
u1f=0;
end
if 20<=t&&t<=25
u3f=10*sin(t/(2*pi))-cos(2*t);
else
u3f=0;
end
if t<=30
u2f=0;
unew1=((u1)+0*exp(-0.00*t)*(sin(t)^2*cos(t)+sin(2*t)^2*cos(0.1*t)+sin(-1.2*t)^2*cos(0.5*t)+sin(t)^5));
unew2=((u2)+0*exp(-0.00*t)*(sin(t)^2*cos(t)+sin(2*t)^2*cos(0.1*t)+sin(-1.2*t)^2*cos(0.5*t)+sin(t)^5));
unew3=((u3)+0*exp(-0.00*t)*(sin(t)^2*cos(t)+sin(2*t)^2*cos(0.1*t)+sin(-1.2*t)^2*cos(0.5*t)+sin(t)^5));
else
unew1=u1;
unew2=u2;
unew3=u3;
end
kappa1(i)=u1;
kappa2(i)=u2;
kappa3(i)=u3;
i=i+1;
size((d1+c1)*(f1-f0+g1*(unew1-u1f))+(d2+c2)*(f2-f0+g2*(unew2-u2f))+(d3+c3)*(f3-f0+g3*(unew3-u3f)))
size((d2+c1)*(f1-f0+g1*(unew1-u1f))+(d2+c2)*(f2-f0+g2*(unew2-u2f))+(d3+c3)*(f3-f0+g3*(unew3-u3f)))
size((d3+c1)*(f1-f0+g1*(unew1-u1f))+(d2+c2)*(f2-f0+g2*(unew2-u2f))+(d3+c3)*(f3-f0+g3*(unew3-u3f)))
size(Win1)
size(Win2)
size(Win3)
size(f1+g1*unew1-g1*u1f)
size(f2+g2*unew2-g2*u2f)
size(f3+g3*unew3-g3*u3f)
size(f0)
size(f1f+g1*(unew1-u1f)+L1*(x1-x1f))
size(f2f+g1*(unew2-u2f)+L1*(x2-x2f))
size(f3f+g1*(unew3-u3f)+L1*(x3-x3f))
size(-L2*g1f'*(x1-x1f)')
size(-L2*g2f'*(x3-x3f)')
size(-L2*g2f'*(x3-x3f)')
xout=[(d1+c1)*(f1-f0+g1*(unew1-u1f))+(d2+c2)*(f2-f0+g2*(unew2-u2f))+(d3+c3)*(f3-f0+g3*(unew3-u3f));...
(d2+c1)*(f1-f0+g1*(unew1-u1f))+(d2+c2)*(f2-f0+g2*(unew2-u2f))+(d3+c3)*(f3-f0+g3*(unew3-u3f));...
(d3+c1)*(f1-f0+g1*(unew1-u1f))+(d2+c2)*(f2-f0+g2*(unew2-u2f))+(d3+c3)*(f3-f0+g3*(unew3-u3f));...
Win1;Win2; Win3;...
f1+g1*unew1-g1*u1f;f2+g2*unew2-g2*u2f;f3+g3*unew3-g3*u3f;f0;...
f1f+g1*(unew1-u1f)+L1*(x1-x1f);f2f+g1*(unew2-u2f)+L1*(x2-x2f);f3f+g1*(unew3-u3f)+L1*(x3-x3f);...
-L2*g1f'*(x1-x1f)';-L2*g2f'*(x3-x3f)';-L2*g2f'*(x3-x3f)'];
end
错误使用 vertcat
要串联的数组的维度不一致。
出错 dynamicsfault (line 148)
xout=[(d1+c1)*(f1-f0+g1*(unew1-u1f))+(d2+c2)*(f2-f0+g2*(unew2-u2f))+(d3+c3)*(f3-f0+g3*(unew3-u3f));...
出错 odearguments (line 90)
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
出错 ode23 (line 114)
odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
出错 fault9_7 (line 65)
[t,x]= ode23('dynamicsfault',[0 30],x0,options);

采纳的回答

Torsten
Torsten 2023-9-7
As you can see in your code above,
f1f+g1*(unew1-u1f)+L1*(x1-x1f)
f2f+g1*(unew2-u2f)+L1*(x2-x2f)
f3f+g1*(unew3-u3f)+L1*(x3-x3f)
are 2x2 matrices. To be able to form xout, they have to be column vectors.
  2 个评论
SuSan jin
SuSan jin 2023-9-7
谢谢,看到了,根据指出来的进行了修改,想问下这个矩阵维数ans是怎么调试出来的,只会跑程序,像这种单独看哪一个不合适该怎么调用
ans = 1×2
2 1
ans = 1×2
2 1
ans = 1×2
2 1
ans = 1×2
3 1
ans = 1×2
3 1
ans = 1×2
3 1
ans = 1×2
2 1
ans = 1×2
2 1
ans = 1×2
2 1
ans = 1×2
2 1
ans = 1×2
2 2
ans = 1×2
2 2
ans = 1×2
2 2
ans = 1×2
1 1
ans = 1×2
1 1
ans = 1×2
1 1
Error using vertcat
Dimensions of arrays being concatenated are not consistent.
Error in solution>dynamicsfault (line 297)
xout=[(d1+c1)*(f1-f0+g1*(unew1-u1f))+(d2+c2)*(f2-f0+g2*(unew2-u2f))+(d3+c3)*(f3-f0+g3*(unew3-u3f));...
Error in odearguments (line 92)
f0 = ode(t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode23 (line 106)
odearguments(odeIsFuncHandle, odeTreatAsMFile, solver_name, ode, tspan, y0, options, varargin);
Torsten
Torsten 2023-9-7
You are interested in how to debug a code ? As you can see, I inserted the commands
size((d1+c1)*(f1-f0+g1*(unew1-u1f))+(d2+c2)*(f2-f0+g2*(unew2-u2f))+(d3+c3)*(f3-f0+g3*(unew3-u3f)))
size((d2+c1)*(f1-f0+g1*(unew1-u1f))+(d2+c2)*(f2-f0+g2*(unew2-u2f))+(d3+c3)*(f3-f0+g3*(unew3-u3f)))
size((d3+c1)*(f1-f0+g1*(unew1-u1f))+(d2+c2)*(f2-f0+g2*(unew2-u2f))+(d3+c3)*(f3-f0+g3*(unew3-u3f)))
size(Win1)
size(Win2)
size(Win3)
size(f1+g1*unew1-g1*u1f)
size(f2+g2*unew2-g2*u2f)
size(f3+g3*unew3-g3*u3f)
size(f0)
size(f1f+g1*(unew1-u1f)+L1*(x1-x1f))
size(f2f+g1*(unew2-u2f)+L1*(x2-x2f))
size(f3f+g1*(unew3-u3f)+L1*(x3-x3f))
size(-L2*g1f'*(x1-x1f)')
size(-L2*g2f'*(x3-x3f)')
size(-L2*g2f'*(x3-x3f)')
in your code because it was obvious that one or several of them couldn't be column vectors.
For the general procedure of debugging this site may help:

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Deep Learning Toolbox 的更多信息

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by