how to resolve this error in for loop

조회 수: 1 (최근 30일)
neelu pareek
neelu pareek 2022년 6월 7일
답변: Pooja Kumari 2022년 6월 10일
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에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by