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

1 次查看(过去 30 天)
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

回答(2 个)

Matt Fig
Matt Fig 2011-3-28
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 2011-3-28
k = 0;
while true
k = k + 1;
....
end
"for" loops should not be used for infinite loops.

类别

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