# Index in position 2 exceeds array bounds. Index must not exceed 1.

20 views (last 30 days)
Le on 22 Jun 2024
Edited: Ganesh on 22 Jun 2024
How to fix errors? "Index in position 2 exceeds array bounds. Index must not exceed 1".
Thanks for everyone's help
Error in VU_MSSP_2024_QUYDAO (line 72)
Xv_2(s,i+1)=-inv(A_0(4,4))*(A_0(4,1)*Xh(s,i+1)+A_0(4,2)*Xeh(s,i+1)+A_0(4,3)*Xv_1(s,i+1)+A_0(4,5)*Xev_1(s,i+1)+A_0(4,6)*Xev_2(s,i+1)+B_0(4,1)*5*exp(-0.1*(s+i+1))*sin(i+s+1));
clc
clear all;
close all;
I_1=[1];
I_2=[1 0;0 1];
Eh=[1];
Ev=[1 0;0 0];
E=blkdiag(Eh,Ev);
E_c=blkdiag(Eh,Eh,Ev,Ev);
Ih=[1];
Iv=[1 0;0 1];
J=[Ih zeros(1,5);zeros(2,2) Iv zeros(2,2);0 Ih zeros(1,4); zeros(2,2) zeros(2,2) Iv];
A=[0.04 -0.01 0.3;0.01 0.9 0.3;0.15 0.01 -0.05];
H=[-0.02 0.01 0.02;0.05 -0.01 0.05;0.47 0.01 2.02];
C=[0.05 -0.02 0.05;-0.05 0.01 0.08];
B_u=[0.01;0.02;-0.01];
B_w=[-0.02;0.05;0.04];
D_u=[0.05;0.01];
D_w=[0.02;-0.03];
A_c=blkdiag(A,A);
B_wc=[B_w;B_w];
B_c=[B_u -B_u;zeros(3,1) zeros(3,1)];
H_c=[zeros(3,3) zeros(3,3); zeros(3,3) H];
N_E=[zeros(1,6);zeros(1,6);zeros(1,6);1 1 1 1 1 1;zeros(1,6);1 1 1 -1 1 1];
D_1=[D_u -D_u];
%--------------------
a=0.8;
gamma=4;
K=[ -0.4957 -9.2860 -4.8612];
L=[ -1726 -1094 43;950 603 -24;-18404 -11652 457];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
A_0=J'*[A+B_u*K -B_u*K;zeros(3,3) A-L*H]*J;
B_0=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label('i');
ylabel('j');
zlabel('x_1^v(i,j)');
hold on;
figure(4)
surf(v,w,Xv_2)
xlabel('i');
ylabel('j');
zlabel('x_2^v(i,j)');
hold on;
figure(5)
surf(v,w,Xev_1)
xlabel('i');
ylabel('j');
zlabel('x_{e1}^v(i,j)');
hold on;
figure(6)
surf(v,w,Xev_2)
xlabel('i');
ylabel('j');
zlabel('x_{e2}^v(i,j)');
%axis([0 100 0 100 -0.8 0.2])
hold on;
figure(7)
surf(v,w,Z)
xlabel('i');
ylabel('j');
zlabel('||z(i,j)||');
%axis([0 100 0 100 -0.8 0.2])
hold on;
figure(8)
surf(v,w,U)
xlabel('i');
ylabel('j');
zlabel('u(i,j)|');
%axis([0 100 0 100 -0.8 0.2])
hold on;
"

Ganesh on 22 Jun 2024
Moved: Voss on 22 Jun 2024
You've not defined Xev_1 in line 44
Le on 22 Jun 2024
Moved: Voss on 22 Jun 2024
Exactly! Thank you very much!

recent works on 22 Jun 2024
Try the follwoing one it is error free
clc
clear all;
close all;
I_1=[1];
I_2=[1 0;0 1];
Eh=[1];
Ev=[1 0;0 0];
E=blkdiag(Eh,Ev);
E_c=blkdiag(Eh,Eh,Ev,Ev);
Ih=[1];
Iv=[1 0;0 1];
J=[Ih zeros(1,5);zeros(2,2) Iv zeros(2,2);0 Ih zeros(1,4); zeros(2,2) zeros(2,2) Iv];
A=[0.04 -0.01 0.3;0.01 0.9 0.3;0.15 0.01 -0.05];
H=[-0.02 0.01 0.02;0.05 -0.01 0.05;0.47 0.01 2.02];
C=[0.05 -0.02 0.05;-0.05 0.01 0.08];
B_u=[0.01;0.02;-0.01];
B_w=[-0.02;0.05;0.04];
D_u=[0.05;0.01];
D_w=[0.02;-0.03];
A_c=blkdiag(A,A);
B_wc=[B_w;B_w];
B_c=[B_u -B_u;zeros(3,1) zeros(3,1)];
H_c=[zeros(3,3) zeros(3,3); zeros(3,3) H];
N_E=[zeros(1,6);zeros(1,6);zeros(1,6);1 1 1 1 1 1;zeros(1,6);1 1 1 -1 1 1];
D_1=[D_u -D_u];
%--------------------
a=0.8;
gamma=4;
K=[ -0.4957 -9.2860 -4.8612];
L=[ -1726 -1094 43;950 603 -24;-18404 -11652 457];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
A_0=J'*[A+B_u*K -B_u*K;zeros(3,3) A-L*H]*J;
B_0=J'*[B_w;B_w];
C_0=[C+D_u*K -D_u*K]*J;
n=80;
v=zeros(1,n);
w=zeros(1,n);
Xh=zeros(n,n); Xeh=zeros(n,n); Xv_1=zeros(n,n); Xv_2=zeros(n,n); Xev_1=zeros(n,n); Xev_2=zeros(n,n); Z=zeros(n,n); U=zeros(n,n);
for i=1:1:n
Xh(1,i)=0;
Xeh(1,i)=0;
end
for i=1:1:n
Xv_1(i,1)=0;
Xv_2(i,1)=0;
Xev_1(i,1)=0;
Xev_2(i,1)=0;
end
x1=zeros(6,1); x0=zeros(6,1); xc=zeros(6,1);
for i=1:1:n
v(1,i)=i;
w(1,i)=i;
end
for s=1:1:n-1
for i=1:1:n-1
x0(1,1)=Xh(s,i);
x0(2,1)=Xeh(s,i);
x0(3,1)=Xv_1(s,i);
x0(4,1)=Xv_2(s,i);
x0(5,1)=Xev_1(s,i);
x0(6,1)=Xev_2(s,i);
x1=A_0*x0+B_0*5*exp(-0.1*(s+i))*sin(i+s);
Xh(s+1,i)=A_0(1,1)*Xh(s,i) + A_0(1,2)*Xeh(s,i) + A_0(1,3)*Xv_1(s,i) + A_0(1,4)*Xv_2(s,i) + A_0(1,5)*Xev_1(s,i) + A_0(1,6)*Xev_2(s,i) + B_0(1,1)*5*exp(-0.1*(s+i))*sin(i+s);
Xeh(s+1,i)=A_0(2,1)*Xh(s,i) + A_0(2,2)*Xeh(s,i) + A_0(2,3)*Xv_1(s,i) + A_0(2,4)*Xv_2(s,i) + A_0(2,5)*Xev_1(s,i) + A_0(2,6)*Xev_2(s,i) + B_0(2,1)*5*exp(-0.1*(s+i))*sin(i+s);
Xv_1(s,i+1)=A_0(3,1)*Xh(s,i) + A_0(3,2)*Xeh(s,i) + A_0(3,3)*Xv_1(s,i) + A_0(3,4)*Xv_2(s,i) + A_0(3,5)*Xev_1(s,i) + A_0(3,6)*Xev_2(s,i) + B_0(3,1)*5*exp(-0.1*(s+i))*sin(i+s);
Xv_2(s,i+1)=-inv(A_0(4,4))*(A_0(4,1)*Xh(s,i+1) + A_0(4,2)*Xeh(s,i+1) + A_0(4,3)*Xv_1(s,i+1) + A_0(4,5)*Xev_1(s,i+1) + A_0(4,6)*Xev_2(s,i+1) + B_0(4,1)*5*exp(-0.1*(s+i+1))*sin(i+s+1));
Xev_1(s,i+1)=A_0(5,1)*Xh(s,i) + A_0(5,2)*Xeh(s,i) + A_0(5,3)*Xv_1(s,i) + A_0(5,4)*Xv_2(s,i) + A_0(5,5)*Xev_1(s,i) + A_0(5,6)*Xev_2(s,i) + B_0(5,1)*5*exp(-0.1*(s+i))*sin(i+s);
Xev_2(s,i+1)=-inv(A_0(6,6))*(A_0(6,1)*Xh(s,i+1) + A_0(6,2)*Xeh(s,i+1) + A_0(6,3)*Xv_1(s,i+1) + A_0(6,4)*Xv_2(s,i+1) + A_0(6,5)*Xev_1(s,i+1) + B_0(6,1)*5*exp(-0.1*(s+i+1))*sin(i+s+1));
Z(s,i)=norm(C_0*x0 + D_w*5*exp(-0.1*(s+i))*sin(i+s));
U(s,i)=K*([Xh(s,i); Xv_1(s,i); Xv_2(s,i)] - [Xeh(s,i); Xev_1(s,i); Xev_2(s,i)]);
end
end
figure(1)
surf(v,w,Xh)
xlabel('i');
ylabel('j');
zlabel('x^h(i,j)');
hold on;
figure(2)
surf(v,w,Xeh)
xlabel('i');
ylabel('j');
zlabel('x_e^h(i,j)');
hold on;
figure(3)
surf(v,w,Xv_1)
xlabel('i');
ylabel('j');
zlabel('x_1^v(i,j)');
hold on;
figure(4)
surf(v,w,Xv_2)
xlabel('i');
ylabel('j');
zlabel('x_2^v(i,j)');
hold on;
figure(5)
surf(v,w,Xev_1)
xlabel('i');
ylabel('j');
zlabel('x_{e1}^v(i,j)');
hold on;
figure(6)
surf(v,w,Xev_2)
xlabel('i');
ylabel('j');
zlabel('x_{e2}^v(i,j)');
hold on;
figure(7)
surf(v,w,Z)
xlabel('i');
ylabel('j');
zlabel('||z(i,j)||');
hold on;
figure(8)
surf(v,w,U)
xlabel('i');
ylabel('j');
zlabel('u(i,j)|');
hold on;
##### 3 CommentsShow 1 older commentHide 1 older comment
Le on 22 Jun 2024
Can you help me fix the same error in the following code?
Below is Matlab's error report and my code!
thank you so much!
Index exceeds the number of array elements. Index must not exceed 5.
Error in VU_CHAO_2024_THEOREM2_V4_QUYDAO (line 60)
x2(k+1)=-inv(A_c(2,2))*(A_c(2,1)*x1(k+1)+A_d(2,1)*x1(floor(k-1.5-1.5*round(sin(0.5*pi*(k+1)))))+A_d(2,2)*x2(floor(k-1.5-1.5*round(sin(0.5*pi*(k+1)))))+A_h(2,1)*(x1(k)+x1(k-1)+x1(k-2))+A_h(2,2)*(x2(k)+x2(k-1)+x2(k-2))+B_w(2,1)*sin(k+1)*exp(-0.1*(k+1))+B_f(2,2)*a*sqrt((x2(k+1))^2+(x1(floor(k-1.5-1.5*round(sin(0.5*pi*(k+1))))))^2+(x2(k)+x2(k-1)+x2(k-2))^2));
A_c=[ 0.2179 -0.4894;
0.3321 -4.7006];
A_d=[0.02 0;0.01 0.1];
A_h=[0.17 0.3;0.06 -0.02];
B_w=[0.02;0.01];
B_f=[0.05 0;0 0.03];
B_u=[-0.01; 0.01];
C=[ -2.8305 -4.9441];
C_d=[0.1 -0.2];
C_h=[0.2 0.6];
D_w=0.2;
D_f=[0.1 0.1];
D_u=-0.1;
E=[1 0;0 0];
I_1=1;
I_2=[1 0;0 1];
N_E=[0 0;1 1];
N=[1 0;0 1];
M=[1 0;0 1];
%%%%%%%%%%%%%
chi=0.9;
gamma=2;
d_1=1;
d_2=4;
h_1=1;
h_2=3;
d=d_2-d_1;
h=h_2-h_1;
a_x=0.01;
a_d=0.01;
a_h=0.01;
a_w=0;
a=0.01;
chi_m1=(chi^(d_1)*(1-chi))/(1-chi^(d_1));
chi_m2=(chi^(d_2)*(1-chi))/(1-chi^(d_2));
chi_n1=(chi^(d_1)*(1-chi)^2)/(1-(d_1+1)*chi^(d_1)+d_1*chi^(d_1+1));
chi_n2=(chi^(d_2)*(1-chi)^2)/(1-(d_2+1)*chi^(d_2)+d_2*chi^(d_2+1));
gamma_1=(d_1*(d_1+1))/2;
gamma_2=(d_2*(d_2+1))/2;
gamma_3=1/h_2;
gamma_4=(h_2*(h_2-h_1+1)*(h_2+h_1))/2;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for k=1:5;
x1(k)=0;
x2(k)=0;
end
for k=5:65;
x1(k+1)=A_c(1,1)*x1(k)+A_c(1,2)*x2(k)+A_d(1,1)*x1(floor(k-2.5-1.5*round(sin(0.5*pi*k))))+A_d(1,2)*x2(floor(k-2.5-1.5*round(sin(0.5*pi*k))))+A_h(1,1)*(x1(k-1)+x1(k-2)+x1(k-3))+A_h(1,2)*(x2(k-1)+x2(k-2)+x2(k-3))+B_w(1,1)*sin(k)*exp(-0.1*k)+B_f(1,1)*a*sqrt((x1(k))^2+(x2(floor(k-2.5-1.5*round(sin(0.5*pi*k)))))^2+(x1(k-1)+x1(k-2)+x1(k-3))^2);
x2(k+1)=-inv(A_c(2,2))*(A_c(2,1)*x1(k+1)+A_d(2,1)*x1(floor(k-1.5-1.5*round(sin(0.5*pi*(k+1)))))+A_d(2,2)*x2(floor(k-1.5-1.5*round(sin(0.5*pi*(k+1)))))+A_h(2,1)*(x1(k)+x1(k-1)+x1(k-2))+A_h(2,2)*(x2(k)+x2(k-1)+x2(k-2))+B_w(2,1)*sin(k+1)*exp(-0.1*(k+1))+B_f(2,2)*a*sqrt((x2(k+1))^2+(x1(floor(k-1.5-1.5*round(sin(0.5*pi*(k+1))))))^2+(x2(k)+x2(k-1)+x2(k-2))^2));
w(k+1)=sin(k+1)*exp(-0.1*(k+1));
z(k+1)=C(1,1)*x1(k+1)+C(1,2)*x2(k+1)+C_d(1,1)*x1(floor(k-2.5-1.5*round(sin(0.5*pi*k))))+C_d(1,2)*x2(floor(k-2.5-1.5*round(sin(0.5*pi*k))))+C_h(1,1)*(x1(k-1)+x1(k-2)+x1(k-3))+C_h(1,2)*(x2(k-1)+x2(k-2)+x2(k-3))+D_w*sin(k)*exp(-0.1*(k))+D_f(1,1)*a*sqrt((x1(k))^2+(x2(floor(k-2.5-1.5*round(sin(0.5*pi*k)))))^2+(x1(k-1)+x1(k-2)+x1(k-3))^2)+D_f(1,2)*a*sqrt((x2(k))^2+(x1(floor(k-2.5-1.5*round(sin(0.5*pi*(k))))))^2+(x2(k-1)+x2(k-2)+x2(k-3))^2);
end
figure(1)
k=-4:61;
plot(k,x1,'b-',k,x2,'r--', 'LineWidth',1.2)
axis([0 60 -0.015 0.01])
legend('x_{1}(k)','x_{2}(k)');
xlabel('Time (k)')
grid on; hold off;
figure(2)
k=-4:61;
plot(k,w,'r--','LineWidth',1.2)
axis([0 60 -0.5 0.5])
legend('w(k)');
xlabel('Time (k)')
grid on; hold off;
figure(3)
k=-4:61;
plot(k,z,'o-','LineWidth',1.2)
axis([0 60 -0.1 0.08])
legend('z(k)');
xlabel('Time (k)')
grid on; hold off;
Ganesh on 22 Jun 2024
Edited: Ganesh on 22 Jun 2024
In that line, when you are using the formula, you've accessed x2(k+1).
x2(k+1)=-inv(A_c(2,2))*(A_c(2,1)*x1(k+1)+A_d(2,1)*x1(floor(k-1.5-1.5*round(sin(0.5*pi*(k+1)))))+A_d(2,2)*x2(floor(k-1.5-1.5*round(sin(0.5*pi*(k+1)))))+A_h(2,1)*(x1(k)+x1(k-1)+x1(k-2))+A_h(2,2)*(x2(k)+x2(k-1)+x2(k-2))+B_w(2,1)*sin(k+1)*exp(-0.1*(k+1))+B_f(2,2)*a*sqrt((x2(k+1))^2+(x1(floor(k-1.5-1.5*round(sin(0.5*pi*(k+1))))))^2+(x2(k)+x2(k-1)+x2(k-2))^2));
This wouldn't be correct, since you're assigning x2(k+1) through the loop. Either it's a mistake, or you might want to redefine the equation.
For any future learning, when you encounter an index issue, it's quite easy to debug it. It simply means that you are trying to access an index of an array that simply doesn't exist at that point. I would suggest you to put a "breakpoint" at that line, and check if each of the variables are accessible at the breakpoint. Will help you find with variable is causing the issue.