Error "??? Attempted to access p(15); index must be a positive integer or logical."

2 views (last 30 days)
I am getting error . Please help me its urgent "Warning: FOR loop index is too large. Truncating to 2147483647. > In NR at 7 ??? Attempted to access p(15); index must be a positive integer or logical.
Error in ==> NR at 11 p(q+1)=yi(k+1);"
When i debug this program
%Newton Rapson Method Iteration
clc
s=0:1:100;
yi=1:0.1:300;
for xi=0:0.01:5
for k=1:1:inf
yi(k+1)=yi(k)-(fvalue(xi,yi(k))/dvalue(xi,yi(k)));
e=((yi(k+1)-yi(k))/yi(k+1))*100;
q=xi*100;
p(q+1)=yi(k+1);
if (e<5)&(e>-5)
p
break
end
end
end

Answers (2)

Matt Fig
Matt Fig on 28 Mar 2011
In general, q will not be an integer. See the many discussions on floating point arithmetic here and on CSSM.
Also, perhaps a WHILE loop would be better? This will rid you of that warning message.
k = 1;
while 1
% Your code here, no FOR loop needed. Use loop counter k.
k = k + 1;
end

Walter Roberson
Walter Roberson on 28 Mar 2011
k = 0;
while true
k = k + 1;
....
end
"for" loops should not be used for infinite loops.

Categories

Find more on Loops and Conditional Statements 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!