why I'm getting an error of index exceeds the number of element? how to fix it?
1 view (last 30 days)
Show older comments
ti = 0;
tf = 2.50E-9;
tspan=[ti tf];
y0=[0; 0; 0];
yita_mn = 0.01;
N = 5;
[T,Y]= ode45(@(t,y) rate_eq(t,y,yita_mn,N),tspan,y0);
subplot 211
plot(T,Y(:,2));
subplot 212
plot(T,Y(:,3));
function dy = rate_eq(t,y,yita_mn,N)
dy = zeros(3*N,1);
dNdt = zeros(N,1);
dSdt = zeros(N,1);
dWdt = zeros(N,1);
P =0.1;
a =0.1;
tc = 230E-6;
tp =5.4E-9;
o = 2E5;
k_c = 1/tp;
Nt = y(1:3:3*N-2);
St = y(2:3:3*N-1);
Wt = y(3:3:3*N-0);
for i = 1:N
dNdt(i) = (P - Nt(i).*((abs(St(i)))^2 +1))./tc ;
dSdt(i) = (Nt(i)-a).*((St(i))./tp);
dWdt(i) = o;
for j = 1:N
dSdt(i) = dSdt(i)+yita_mn(i,j)*k_c*(St(j))*cos(Wt(j)-Wt(i));
dWdt(i) = dWdt(i)+yita_mn(i,j)*k_c*((St(j)/St(i)))*sin(Wt(j)-Wt(i));
end
end
dy(1:3:3*N-2) = dNdt;
dy(2:3:3*N-1) = dSdt;
dy(3:3:3*N-0) = dWdt;
Loop_in = Loop_in+1;
Loop = Loop_in;
CalLoop=sprintf('Calculation Loop',Loop);
disp(CalLoop);
end
===============================================
Index exceeds the number of array elements. Index must not exceed 3.
Nt = y(1:3:3*N-2);
[T,Y]= ode45(@(t,y) rate_eq(t,y,yita_mn,N),tspan,y0);
f0 = ode(t0,y0,args{:}); % ODE15I sets args{1} to yp0.
odearguments(odeIsFuncHandle,odeTreatAsMFile, solver_name, ode, tspan, y0, options, varargin);
=======================================================
5 Comments
Karim
on 9 Aug 2022
because you are trying to acces
yita_mn(i,j)
as if it is a matrix, however you initalize
yita_mn = 0.01;
as a scalar, hance an error is thrown the moment either i or j are greater then 1.
Looking at the code you will need to make sure yita_mn has N rows and N columns. Or remove the indexing (i,j) in the routine to just use the scalar.
Answers (0)
See Also
Categories
Find more on Creating and Concatenating Matrices 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!