Index in position 2 exceeds array bounds. Index must not exceed 1.
5 views (last 30 days)
Show older comments
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=J'*[B_w;B_w];
C_0=[C+D_u*K -D_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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)');
%axis([0 100 0 100 -0.15 0.06])
hold on;
figure(2)
surf(v,w,Xeh)
xlabel('i');
ylabel('j');
zlabel('x_e^h(i,j)');
%axis([0 100 0 100 -0.5 0.1])
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)');
%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;
"
0 Comments
Accepted Answer
More Answers (1)
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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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 Comments
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.
See Also
Categories
Find more on Interpolation of 2-D Selections in 3-D Grids in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!