Index exceeds the number of array elements .

function v = model(t,y,Z)
r=10^(-1);
d=5*10^(-1);
delta = 10^(-1);
c = 10^(2);
K = 10^(8);
C = 10^(8);
x = 1;
sigma = 10^(-2);
delta_m = 2.5*10^(-2);
ylag1 = Z(:,1);
dy = zeros(4,1);
for i = 1:6
dy(4*(i-1)+1) = r*y(4*(i-1)+1)*(1-((y(4*(i-1)+1)+y(4*(i-1)+2))/K))-d*y(4*(i-1)+3)*y(4*(i-1)+1);
dy(4*(i-1)+2) = r*y(4*(i-1)+1)*((y(4*(i-1)+1)+y(4*(i-1)+2)/K))-delta*y(4*(i-1)+3)*y(4*(i-1)+2)-delta_m*y(4*(i-1)+2);
dy(4*(i-1)+3) = c*(((ylag1(4*(i-1)+1)+ylag1(4*(i-1)+2))/C)^x)*(1-y(4*(i-1)+3));
dy(4*(i-1)+4) = 0;
end
got error in dy(4*(i-1)+1) = r*y(4*(i-1)+1)*(1-((y(4*(i-1)+1)+y(4*(i-1)+2))/K))-d*y(4*(i-1)+3)*y(4*(i-1)+1); line that Index exceeds the number of array elements .
Can someone please help me?

댓글 수: 1

Hi Gupta. Curious what process led to this type of model formulation in delay diffenetial equations. Can you share the process?
Thanks!

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

 채택된 답변

Guillaume
Guillaume 2019년 3월 11일
편집: Guillaume 2019년 3월 11일

2 개 추천

In the code you wrote,
for i = 1:6
So, maximum value of i is 6. It is used in your expression:
dy(4*(i-1)+1) = xxx*y(4*(i-1)+3)*xxx
So, maximum index for y is 4*(6-1)+3 == 23. So if y does not have at least 23 elements, your code will error with index exceeds the number of array elements error. We can safely conclude that it is indeed the case. The fix is to either pass an y with enough elements, or change your algorithm so that it needs less elements.
In the same vein, you declare dy as an array of 4 elements, but will assign 24 elements to it (if your i loop goes up to 6). This will not cause an error (matlab will automatically grow the array), but clearly indicates that something has not been thought out properly.
edit: As you wrote as a comment to Jan's answer, your y has 4 elements. As said, your code requires a y with 23 elements. So change your code or your y.

댓글 수: 4

parag gupta
parag gupta 2019년 3월 11일
편집: parag gupta 2019년 3월 11일
for i =1
v = zeros(4,1);
v(1) = r*y(1)*(1-((y(1)+y(2))/K))-d*y(3)*y(1);
v(2) = r*y(1)*((y(1)+y(2)/K))-delta*y(3)*y(2)-delta_m*y(2);
v(3) = c*(((ylag1(1)+ylag1(2))/C)^x)*(1-y(3));
Actually these are 3 differential equations for one variant i.e v1,m1,a1 where dv1/dt = v(1) ,dm1/dt = v(2) ,da1/dt = v(2) and I solved them by using dde23 solver.
But now I want to find a solution of delay differential equations for i = 1: 6
vi(1) = r*yi(1)*(1-((y(1)+yi(2))/K))-d*yi(3)*y(1);.........................1
vi(2) = r*yi(1)*((y(1)+yi(2)/K))-delta*yi(3)*y(2)-delta_m*yi(2);...............2
vi(3) = c*(((yilag1(1)+ylag1(2))/C)^x)*(1-yi(3));.......................................3
where i is changing from 1 to 6 with different intial condition for different i.
so i wrote above code in which i got error.Could you please tell me how to edit my code so that i can get solution to system of differential equation which i have highlighted above..
thanks
Guillaume
Guillaume 2019년 3월 11일
I don't understand what you're asking. Your code is not commented, so I've no idea what it does.
You say, I want to find a solution of delay differential equations for i = 1: 6. What does that mean? What is i, how does it relate to y?
parag gupta
parag gupta 2019년 3월 11일
vi(1) = r*yi(1)*(1-((y(1)+yi(2))/K))-d*yi(3)*y(1);.........................1
vi(2) = r*yi(1)*((y(1)+yi(2)/K))-delta*yi(3)*y(2)-delta_m*yi(2);...............2
vi(3) = c*(((yilag1(1)+ylag1(2))/C)^x)*(1-yi(3));.......................................3
I want to solve this system of delay differential equations by dde23 solver....
where v i (1) = dvi/dt. (here i is in subcript)
v i (2) = dmi/dt
v i (3) = dai/dt
Use
for i=0:6
Your starting value of i must start from 0 since your have (i-1) in your equation.
Just start your i value from 0 instaed of 1. This should fix the error.

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

추가 답변 (4개)

Jan
Jan 2019년 3월 11일

0 개 추천

Simplify your code:
ii = 4 * (i-1) + 1;
dy(ii) = r * y(ii) * (1 - (y(ii) + y(ii+1)) / K) - d * y(ii+2) * y(ii);
Now use the debugger:
dbstop if error
When Matlab stops at the error, check the values of the indices:
size(y)
ii
By the way, 5*10^(-1) is an expensive power operation and a multiplication, while 5e-1 or 0.5 is a cehap constant.

댓글 수: 3

parag gupta
parag gupta 2019년 3월 11일
>> size(y)
ii
ans =
4 1
ii =
4
got these values ...I have created a zero matrix of order 4x1 ..( dy = zeros(4,1);) but still getting same error..
Jan
Jan 2019년 3월 11일
If ii is 4 and y is a [4x1] vector, y(ii+1) and y(ii+2) must fail.
parag gupta
parag gupta 2019년 3월 11일
or i =1
v = zeros(4,1);
v(1) = r*y(1)*(1-((y(1)+y(2))/K))-d*y(3)*y(1);
v(2) = r*y(1)*((y(1)+y(2)/K))-delta*y(3)*y(2)-delta_m*y(2);
v(3) = c*(((ylag1(1)+ylag1(2))/C)^x)*(1-y(3));
Actually these are 3 differential equations for one variant i.e v1,m1,a1 where dv1/dt = v(1) ,dm1/dt = v(2) ,da1/dt = v(2) and I solved them by using dde23 solver.
But now I want to find a solution of delay differential equations for i = 1: 6
vi(1) = r*yi(1)*(1-((y(1)+yi(2))/K))-d*yi(3)*y(1);.........................1
vi(2) = r*yi(1)*((y(1)+yi(2)/K))-delta*yi(3)*y(2)-delta_m*yi(2);...............2
vi(3) = c*(((yilag1(1)+ylag1(2))/C)^x)*(1-yi(3));.......................................3
where i is changing from 1 to 6 with different intial condition for different i.
so i wrote above code in which i got error.Could you please tell me how to edit my code so that i can get solution to system of differential equation which i have highlighted above..
thanks

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

Giomara Llumiquinga
Giomara Llumiquinga 2022년 5월 31일

0 개 추천

costo=abs((picos(I+1))-pico_ref)/pico_ref; %devuelve el pico siguiente
El índice supera el número de elementos de la matriz. El índice no debe exceder de 1.
Enguerrand Galmiche
Enguerrand Galmiche 2023년 2월 13일

0 개 추천

Hello,
This code return the error message "Index exceeds the number of array elements. Index must not exceed 6.". The idea is read the values of table Excel and concatenate the values for every iterate of loop. What can I for correct this error and this problem.
Thanks
for ifile=1:nfiles
disp(['Traitement du fichier n° ',sprintf('%d',ifile)])
filename = fullfile('C:\Users\Stagiaire\Desktop\Enguerrand\Analyse_Pose_Docking',filelist(ifile).name); % Récupération du nom de fichier complet
disp(filename)
% Importation des données de RMSD et De Score de chaque fichier excel
% du répertoire de données
RMSD = xlsread(filename,'B:B');
Glide_Score = xlsread(filename,'C:C');
RMSD_Random = [RMSD(ifile); RMSD];
Glide_Score_Random = [Glide_Score(ifile); Glide_Score];
Analyse_Vec = horzcat(RMSD_Random, Glide_Score_Random); %Concatène les 2 vecteurs dans une matrice
disp(RMSD)
disp(Glide_Score)
disp(Glide_Score_Random)
disp(RMSD_Random)
end

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

질문:

2019년 3월 11일

답변:

2023년 12월 14일

Community Treasure Hunt

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

Start Hunting!

Translated by