## Issues executing while loop

### Bryan (view profile)

on 17 Jul 2019
Latest activity Commented on by Star Strider

on 18 Jul 2019

### Star Strider (view profile)

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

### Tags ### Star Strider (view profile)

on 17 Jul 2019

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.

Star Strider

### Star Strider (view profile)

on 17 Jul 2019
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 (view profile)

on 18 Jul 2019
Hi Stryder,
I see what you did there. It is good. Thanks for your help!
Star Strider

### Star Strider (view profile)

on 18 Jul 2019
As always, my pleasure!