why I'm getting an error of index exceeds the number of element? how to fix it?

2 次查看(过去 30 天)
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);
Index exceeds the number of array elements. Index must not exceed 3.

Error in solution>rate_eq (line 32)
Nt = y(1:3:3*N-2);

Error in solution (line 7)
[T,Y]= ode45(@(t,y) rate_eq(t,y,yita_mn,N),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);
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);
Error in untitled2 (line 7)
[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.
Error in ode45 (line 107)
odearguments(odeIsFuncHandle,odeTreatAsMFile, solver_name, ode, tspan, y0, options, varargin);
=======================================================
  5 个评论
Karim
Karim 2022-8-9
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.
SAHIL SAHOO
SAHIL SAHOO 2022-8-9
ti = 0;
tf = 1E-8;
tspan=[ti tf];
y0=[1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1]*10E-8;
yita_mn = [0 1 0 0 1; 1 0 1 0 0; 0 1 0 1 0; 0 0 1 0 1; 1 0 0 1 0]*(0.001);
N = 5;
tp = 5.4E-9;
[T,Y]= ode45(@(t,y) rate_eq(t,y,yita_mn,N),tspan./tp,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 = 14.5;
a = 0.1;
tc = 230E-6;
tp =5.4E-9;
o = 2E3;
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;
end
I dont know, why still this its not working?

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by