how to resolve this error in for loop

1 view (last 30 days)
neelu pareek
neelu pareek on 7 Jun 2022
Answered: Pooja Kumari on 10 Jun 2022
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 Comments
Torsten
Torsten on 7 Jun 2022
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 on 7 Jun 2022
Edited: Jan on 7 Jun 2022
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?

Sign in to comment.

Answers (1)

Pooja Kumari
Pooja Kumari on 10 Jun 2022
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

Tags

Community Treasure Hunt

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

Start Hunting!