how to resolve this error in for loop

1 次查看(过去 30 天)
T(0)=.1;I(0)=0;V(0)=.1;P=.1;Alpha=.02;beta=.3;l=2.4;K=.0027;Tmax=1500;N=10;r=3;mu=.9;eta=.99;nu=1;
T(1)=(.397953)/gamma(mu+1); I1=(.000027)/gamma(eta+1); V1=-.24/gamma(nu+1);
for k=1:10
T(k+1)=(gamma(k*mu+1)/gamma(k*mu+mu+1))*( ((r-Alpha)*T(k))-((r/Tmax)*
(symsum(T(n)T(k-n),n,[0 k])- symsum(T(n)I(k-n),n,[0 k]))-K(symsum(V(n)T(k-n),n,[0 k])));
I(k+1)= (gamma(k*eta+1)/gamma(k*eta+eta+1))*((K*(symsum(V(n)T(k-n),n,[0 k]))-beta*I(k));
V(k+1)= (gamma(k*nu+1)/gamma(k*nu+nu+1))*((N*beta*I(k))-(l*V(k)));
end
Attempted to access (0); index must be a positive integer or logical.
  2 个评论
Torsten
Torsten 2022-6-7
MATLAB array indices start at 1, not at 0.
Thus T(0), I(0),V(0), symsum(T(n)T(k-n),n,[0 k]),... all throw an error.
Jan
Jan 2022-6-7
编辑:Jan 2022-6-7
I get a different message, when I run your code:
T(0)=.1;I(0)=0;V(0)=.1;P=.1;Alpha=.02;beta=.3;l=2.4;K=.0027;Tmax=1500;N=10;r=3;mu=.9;eta=.99;nu=1;
T(1)=(.397953)/gamma(mu+1); I1=(.000027)/gamma(eta+1); V1=-.24/gamma(nu+1);
for k=1:10
T(k+1)=(gamma(k*mu+1)/gamma(k*mu+mu+1))*( ((r-Alpha)*T(k))-((r/Tmax)* ...
(symsum(T(n)T(k-n),n,[0 k])- symsum(T(n)I(k-n),n,[0 k]))-K(symsum(V(n)T(k-n),n,[0 k])));
Invalid expression. Check for missing multiplication operator, missing or unbalanced delimiters, or other syntax error. To construct matrices, use brackets instead of parentheses.
I(k+1)= (gamma(k*eta+1)/gamma(k*eta+eta+1))*((K*(symsum(V(n)T(k-n),n,[0 k]))-beta*I(k));
V(k+1)= (gamma(k*nu+1)/gamma(k*nu+nu+1))*((N*beta*I(k))-(l*V(k)));
end
Please post the code you really use or a minimal working example.
Torsten's suggestion is fundamental already.
By the way, in which Matlab version do you get the message "Attempted to access (0); index must be a positive integer or logical." ? And why is this clear message not an explanation already?

请先登录,再进行评论。

回答(1 个)

Pooja Kumari
Pooja Kumari 2022-6-10
Dear Neelu,
I am Pooja Kumari and it is my understanding that you are facing “index must be positive integer or logical” error in your code.
I tried to run the above code and I got the following error:
The reason for this error is incorrect delimiters and missing multiplication operator.
%above code
T(0)=.1;I(0)=0;V(0)=.1;P=.1;Alpha=.02;beta=.3;l=2.4;K=.0027;Tmax=1500;N=10;r=3;mu=.9;eta=.99;nu=1;
T(1)=(.397953)/gamma(mu+1); I1=(.000027)/gamma(eta+1); V1=-.24/gamma(nu+1);
for k=1:10
T(k+1)=(gamma(k*mu+1)/gamma(k*mu+mu+1))*( ((r-Alpha)*T(k))-((r/Tmax)*(symsum(T(n)T(k-n),n,[0 k])- symsum(T(n)I(k-n),n,[0 k]))-K(symsum(V(n)T(k-n),n,[0 k])));
1 2 2 2
I(k+1)= (gamma(k*eta+1)/gamma(k*eta+eta+1))*((K*(symsum(V(n)T(k-n),n,[0 k]))-beta*I(k));
1 2
V(k+1)= (gamma(k*nu+1)/gamma(k*nu+nu+1))*((N*beta*I(k))-(l*V(k)));
end
% I have marked 1 for incorrect delimiter
% And 2 for missing multiplication operator.
You can use Code Analyzer to resolve this error. Refer to the link below for this purpose:
The second error “index must be positive integer or logical “occurs because you are attempting to index variable with 0.
MATLAB supports 1-indexing.
You can understand that Array Indexing in MATLAB starts from 1 from the link provided below:
Sincerely,
Pooja Kumari

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by