I'm having this error in code "Index in position 2 exceeds array bounds. Index must not exceed 20." can a nyone please explain how can I get rid of this?

조회 수: 3 (최근 30일)
clc
clear
ti = 0;
tf = 7E-5;
tp = 1E-9;
tspan= [0:1E-7:7E-5]./tp;
KC = 1;
h = 1E-2;
N = 20;
y0 = zeros(3*N*N,1);
for j = 1:N*N
y0(3*j,1) = (-3.14).*rand(1,1) + (3.14).*rand(1,1);
end
y0;
for j = 1:3*N*N
if y0(j,1) == 0
y0(j,1) = (h)*rand(1,1);
else
y0(j,1) = y0(j,1);
end
end
y0
y0 = 1200×1
0.0080 0.0009 1.0877 0.0014 0.0090 2.0797 0.0039 0.0064 -0.4429 0.0011
L = 1
L = 1
C = zeros(N,N);
for i = 1:N
for j = 1:N
if i ~= j
C(i,j) = 1;
end
end
end
yita_mn = C.*2E-3
yita_mn = 20×20
0 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020 0.0020
tp = 1E-9;
o = sort(10e2*rand(1,N*N),'ascend');
[T,Y]= ode45(@(t,y) rate_eq(t,y,yita_mn,o),tspan,y0);
Index in position 2 exceeds array bounds. Index must not exceed 20.

Error in solution>rate_eq (line 68)
dAdt(i) = dAdt(i) + yita_mn(i,j)*(At(j))*cos(Ot(j)-Ot(i)) + yita_mn(j,i)*(At(j))*cos(Ot(j)-Ot(i));

Error in solution>@(t,y)rate_eq(t,y,yita_mn,o) (line 41)
[T,Y]= ode45(@(t,y) rate_eq(t,y,yita_mn,o),tspan,y0);

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

Error in ode45 (line 107)
odearguments(odeIsFuncHandle,odeTreatAsMFile, solver_name, ode, tspan, y0, options, varargin);
figure(3)
plot(T,(Y(:,3)),'linewidth',2);
function dy = rate_eq(t,y,yita_mn,o)
N = 20;
dy = zeros(3*N*N,1);
dGdt = zeros(N*N,1);
dAdt = zeros(N*N,1);
dOdt = zeros(N*N,1);
P = 0.4;
a = 0;
T = 800;
tp = 1E-9;
Gt = y(1:3:3*N*N-2);
At = y(2:3:3*N*N-1);
Ot = y(3:3:3*N*N-0);
k = 1E-3;
for i = 1:N*N
dGdt(i) = (P - (Gt(i)) - (1 + 2.*Gt(i)).*((At(i)))^2)./T ;
dAdt(i) = Gt(i)*(At(i));
dOdt(i) = (-a).*(Gt(i)) + o(1,i).*tp;
for j = 1:N*N
dAdt(i) = dAdt(i) + yita_mn(i,j)*(At(j))*cos(Ot(j)-Ot(i)) + yita_mn(j,i)*(At(j))*cos(Ot(j)-Ot(i));
dOdt(i) = dOdt(i) + yita_mn(i,j)*((At(j)/At(i)))*sin(Ot(j)-Ot(i));
end
end
dy(1:3:3*N*N-2) = dGdt;
dy(2:3:3*N*N-1) = dAdt;
dy(3:3:3*N*N-0) = dOdt;
end
=========================================================================================
Index in position 2 exceeds array bounds. Index must not exceed 20.
dAdt(i) = dAdt(i) + yita_mn(i,j)*(At(j))*cos(Ot(j)-Ot(i)) + yita_mn(j,i)*(At(j))*cos(Ot(j)-Ot(i));
[T,Y]= ode45(@(t,y) rate_eq(t,y,yita_mn,o),tspan,y0);
f0 = ode(t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode45 (line 107)
odearguments(odeIsFuncHandle,odeTreatAsMFile, solver_name, ode, tspan, y0, options, varargin);
=========================================================================================
  댓글 수: 1
Sathvik
Sathvik 2023년 5월 3일
The 'yita_mn' variable is a 20*20 matrix, while the indices i and j are between 1 and 400 in the for loop within the function. Modify the loop accordingly and it should work.

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

답변 (1개)

Shivani
Shivani 2023년 5월 29일
Hi Sahil,
The reason why you are getting an index out of bounds error is because the limits of the for loop exceed the dimensions of the array that you are trying to index into within the loop body. I understand that you’re using two variables i and j to index into the matrix yita_mn within the loop body. However, the matrix is of dimension 20*20 i.e. you have 20 rows and 20 columns in your matrix. Therefore, your max limit for i and j should be 20 and not 20*20. 0
You will have to modify your loop conditions to something like this to successfully loop through the matrix
for i = 1:N
dGdt(i) = (P - (Gt(i)) - (1 + 2.*Gt(i)).*((At(i)))^2)./T ;
dAdt(i) = Gt(i)*(At(i));
dOdt(i) = (-a).*(Gt(i)) + o(1,i).*tp;
for j = 1:N
dAdt(i) = dAdt(i) + yita_mn(i,j)*(At(j))*cos(Ot(j)-Ot(i)) + yita_mn(j,i)*(At(j))*cos(Ot(j)-Ot(i));
dOdt(i) = dOdt(i) + yita_mn(i,j)*((At(j)/At(i)))*sin(Ot(j)-Ot(i));
end
end
You can look at the official documentation for more insights regarding indexing: Matrix Indexing in MATLAB - MATLAB & Simulink (mathworks.com)
Hope this helps!

카테고리

Help CenterFile Exchange에서 Mathematics에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by