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 views (last 30 days)
Show older comments
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
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
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);
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.
odearguments(odeIsFuncHandle,odeTreatAsMFile, solver_name, ode, tspan, y0, options, varargin);
=========================================================================================
1 Comment
Sathvik
on 3 May 2023
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.
Answers (1)
Shivani
on 29 May 2023
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!
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!