Hi guys,
I am not able to run this code. I plan to run this code which will make the 4 values in my DPnew array to be close enough with a certain tolerance percentage. I am using a while loop which works like a do while statement. It is not working all the time though. Sometimes it works and sometimes MATLAB starts to pause and debug the code. What is the issue with it? It is part of an algorithm so changing the formulas for the calculation is not an option.
DPbest = 0.4;
B = rand;
DPold = [0.2;0.4;0.6;0.8];
DPnew = [0;0;0;0];
vold = [0;0;0;0];
vnew = [0;0;0;0];
dataType = 'double';
while true
for i = 1:1:4
f = 0.3 + (0.5-0.3)*B;
vnew(i) = vold(i)+(DPold(i) - DPbest)*f;
if DPold(i) > DPbest
DPnew(i) = DPold(i) - abs(vnew(i));
else
DPnew(i) = DPold(i) + abs(vnew(i));
end
end
vold = vnew;
if abs((max(DPnew)-min(DPnew))/max(DPnew)) < 0.1
break;
end
end

 채택된 답변

Star Strider
Star Strider 2019년 7월 17일

0 개 추천

When I ran the code you posted, the while loop becomes infinite if ‘min(DPnew)’ is negative. In that event, this expression:
abs((max(DPnew)-min(DPnew))/max(DPnew))
appears to increase until it reaches a value of about 3. (I don’t know if it increases beyond that, since I interrupted it before then.)
Trapping negative values of ‘DPnew’ and correcting them, or setting an additional limit on the while condition (for example, adding a counter and setting the additional while condition that the counter does not exceed some value), should solve that problem, or at least prevent the loop from becoming infinite.
I doubt MATLAB is pausing to debug the code.

댓글 수: 7

Bryan
Bryan 2019년 7월 17일
Thanks for the reply Star Strider,
It there a way to let it run without setting a counter? It is supposed to run until it satisfies the condition i set in the code. Need you to be more elaborate on the ways as I am not so familiar with that you have explained.
Thanks.
Bryan
Bryan 2019년 7월 17일
And also how do I trap the negative values for DPnew?
My pleasure.
If you want it to run without setting a counter, you need to detect and correct any negative values of ‘DPnew’. One way to do this is simply to add:
DPnew(i) = abs(DPnew(i));
just before the end of the for loop.
That is the only way (that I can see) to avoid the while loop becoming infinite.
Bryan
Bryan 2019년 7월 17일
I have tried what you suggested by adding it right before the end of the for loop. It is giving the same error though.
I confirmed that result as well. It also occurs with positive values of ‘DPnew’.
Checking further, the problem appears to be that in some situations, this value:
abs((max(DPnew)-min(DPnew))/max(DPnew))
can increase without bound as the while loop iterates.
One way to deal with that is to trap any increasing values, and stop the loop, defining ‘testprv’ earlier:
test = abs((max(DPnew)-min(DPnew))/max(DPnew));
if (abs((max(DPnew)-min(DPnew))/max(DPnew)) < 0.1) | (test >= testprv)
break;
end
testprv = test;
That will stop the while loop if the value of what I call ‘test’ (in my revision of your code) increases, and will keep it from becoming infinite.
You would likely need to begin the entire routine over again to get a valid result, since the result you appear to want depends on the value of ‘test’ monotonically decreasing as the while loop iterates.
The revised version of your code then becomes:
DPbest = 0.4;
B = rand;
DPold = [0.2;0.4;0.6;0.8];
DPnew = [0;0;0;0];
vold = [0;0;0;0];
vnew = [0;0;0;0];
dataType = 'double';
testprv = Inf;
while true
for i = 1:1:4
f = 0.3 + (0.5-0.3)*B;
vnew(i) = vold(i)+(DPold(i) - DPbest)*f;
if DPold(i) > DPbest
DPnew(i) = DPold(i) - abs(vnew(i));
else
DPnew(i) = DPold(i) + abs(vnew(i));
end
end
vold = vnew;
test = abs((max(DPnew)-min(DPnew))/max(DPnew));
if (abs((max(DPnew)-min(DPnew))/max(DPnew)) < 0.1) | (test >= testprv)
break;
end
testprv = test;
end
That is the best I can do.
Bryan
Bryan 2019년 7월 18일
Hi Stryder,
I see what you did there. It is good. Thanks for your help!
Star Strider
Star Strider 2019년 7월 18일
As always, my pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

질문:

2019년 7월 17일

댓글:

2019년 7월 18일

Community Treasure Hunt

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

Start Hunting!

Translated by