Index exceeds number of array elements. Index must not exceed 8. HELP
Show older comments
Here's my model code:
clear all;
close all;
S = 99;
I = 1;
R = 0;
N = 100; %Total population
beta= 0.1; % birth rate
alpha= 0.1; % infection person to person rate
lambda= 0.3; % infection by water rate
vac= 0.05; % recovery by vaccination rate
d= 0.03; % death rate
gamma= 0.8; % recovery rate
c= 0.9; % rate of contamination
m= 0.4; % rate of decay of V. cholera
t_f = 500; %Ending time of simulation
Q = [beta alpha lambda vac d gamma c m];
[t,y]=ode45('cholera_de',[0:t_f/100:t_f],[S I R]',[],Q);
figure(1)
plot(t,y(:,1),'k-',t,y(:,2),'r--',t,y(:,3),'b:');
xlabel('\bf Time (days)');
ylabel('\bf Number of People by Category');
legend('S','I','R');
z=y(end,:)'
SN=y(:,1)/N;
IN=y(:,2)/N;
figure(2)
plot(IN,SN);
xlabel('\bf I/N');
ylabel('\bf S/N');
r0=beta/beta
[maxIN,y_maxtime]=max(y(:,2)/N);
maxIN
maxtime=y(y_maxtime)
eqIN=y(100,2)/N;
eqIN
And my Function code:
function dy=cholera_de(t,Y,flag,Q)
beta= Q(1);
alpha= Q(2);
lambda= Q(3);
vac= Q(4);
d=Q(5);
gamma=Q(6);
c=Q(7);
m=Q(8);
B= Q(9);
S= Y(1);
I= Y(2);
R= Y(3);
N= S+I+R;
dy(1,1)= beta - alpha*I - lambda*B - vac*S - d;
dy(2,1)= alpha*I + lambda*B - d - gamma*I;
dy(3,1)= gamma*I + vac*S - d;
dy(4,1)= c*I - m*B;
Why am I getting this error?
Index exceeds the number of array elements. Index must not exceed 8.
Error in cholera_de (line 11)
B= Q(9);
Error in odearguments (line 92)
f0 = ode(t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode45 (line 104)
odearguments(odeIsFuncHandle,odeTreatAsMFile, solver_name, ode, tspan, y0, options, varargin);
Error in cholera_model (line 22)
[t,y]=ode45('cholera_de',[0:t_f/100:t_f],[S I R]',[],Q);
Answers (1)
Q = [beta alpha lambda vac d gamma c m];
The size of the vector is 1x8
2 Comments
VBBV
on 25 Apr 2024
But you try to access the non existent element of vector in this line
B=Q(9)
Cris LaPierre
on 25 Apr 2024
Moved: Cris LaPierre
on 25 Apr 2024
The error means you are trying to access an element in Q that does not exist. From the message, Q contains 8 elements, so you get an error when trying to indext a 9th element.
This makes sense. You define Q as having 8 elements.
Q = [beta alpha lambda vac d gamma c m];
but in your function, you try to access a 9th element
B= Q(9);
Categories
Find more on Matrix Indexing 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!