Clear Filters
Clear Filters

How to end an indefinite loop?

1 view (last 30 days)
Hosoda Hung
Hosoda Hung on 25 Feb 2022
Edited: Voss on 25 Feb 2022
My goal;
  1. Guess values of T
  2. Use T to calculate L
  3. Find largest L element
  4. Update the T element correlating to the largest L element. Keep other T's unchanged.
  5. Use updated T array to find L again.
  6. Repeat until largest L element is less than 0.1.
Basically, I keep updating T until all L elements are below 0.1. Currently, it never stops and doesn't give the right T array, which is supposed to be [326; 307; 301; 301]
Hope this makes sense. All suggestions welcome
%Convergence Program
%start
clearvars; clc;
%Guess
T = zeros(4,1);
T(1) = input("T1? ");
T(2) = input("T2? ");
T(3) = input("T3? ");
T(4) = input("T4? ");
%Leftovers
L = zeros(4,1);
L(1) = abs(1000+T(2)-4*T(1));
L(2) = abs(600+T(1)+T(3)-4*T(2));
L(3) = abs(600+T(2)+T(4)-4*T(3));
L(4) = abs(600+2*T(3)-4*T(4));
%Largest
[rem, pos] = max(L);
%Loop
while rem > 0.1
if(pos==1)
T(1) = 0.25*(1000+T(2));
L(2) = abs(600+T(1)+T(3)-4*T(2));
L(3) = abs(600+T(2)+T(4)-4*T(3));
L(4) = abs(600+2*T(3)-4*T(4));
[rem, pos] = max(L);
elseif(pos==2)
T(2) = 0.25*(600+T(1)+T(3));
L(1) = abs(1000+T(2)-4*T(1));
L(3) = abs(600+T(2)+T(4)-4*T(3));
L(4) = abs(600+2*T(3)-4*T(4));
[rem, pos] = max(L);
elseif(pos==3)
T(3) = 0.25*(600+T(2)+T(4));
L(1) = abs(1000+T(2)-4*T(1));
L(2) = abs(600+T(1)+T(3)-4*T(2));
L(4) = abs(600+2*T(3)-4*T(4));
[rem, pos] = max(L);
elseif(pos==4)
T(4) = 0.25*(600+2*T(3));
L(1) = abs(1000+T(2)-4*T(1));
L(2) = abs(600+T(1)+T(3)-4*T(2));
L(3) = abs(600+T(2)+T(4)-4*T(3));
[rem, pos] = max(L);
elseif rem < 0.1
break
end
end

Accepted Answer

Voss
Voss on 25 Feb 2022
I'm not sure if this is what you're going for, but now the loop terminates. The only difference is now the code updates elements of L appropriately (I think) based on which element of T changed.
%Convergence Program
%start
clearvars; clc;
%Guess
T = zeros(4,1);
% T(1) = input("T1? ");
% T(2) = input("T2? ");
% T(3) = input("T3? ");
% T(4) = input("T4? ");
%Leftovers
L = zeros(4,1);
L(1) = abs(1000+T(2)-4*T(1));
L(2) = abs(600+T(1)+T(3)-4*T(2));
L(3) = abs(600+T(2)+T(4)-4*T(3));
L(4) = abs(600+2*T(3)-4*T(4));
%Largest
[rem, pos] = max(L);
%Loop
while rem > 0.1
if(pos==1)
% update T(1):
T(1) = 0.25*(1000+T(2));
% update only those elements of L that depend on T(1):
L(1) = abs(1000+T(2)-4*T(1));
L(2) = abs(600+T(1)+T(3)-4*T(2));
% L(3) = abs(600+T(2)+T(4)-4*T(3));
% L(4) = abs(600+2*T(3)-4*T(4));
[rem, pos] = max(L);
elseif(pos==2)
% update T(2):
T(2) = 0.25*(600+T(1)+T(3));
% update only those elements of L that depend on T(2):
L(1) = abs(1000+T(2)-4*T(1));
L(2) = abs(600+T(1)+T(3)-4*T(2));
L(3) = abs(600+T(2)+T(4)-4*T(3));
% L(4) = abs(600+2*T(3)-4*T(4));
[rem, pos] = max(L);
elseif(pos==3)
% update T(3):
T(3) = 0.25*(600+T(2)+T(4));
% update only those elements of L that depend on T(3):
% L(1) = abs(1000+T(2)-4*T(1));
L(2) = abs(600+T(1)+T(3)-4*T(2));
L(3) = abs(600+T(2)+T(4)-4*T(3));
L(4) = abs(600+2*T(3)-4*T(4));
[rem, pos] = max(L);
elseif(pos==4)
% update T(4):
T(4) = 0.25*(600+2*T(3));
% update only those elements of L that depend on T(4):
% L(1) = abs(1000+T(2)-4*T(1));
% L(2) = abs(600+T(1)+T(3)-4*T(2));
L(3) = abs(600+T(2)+T(4)-4*T(3));
L(4) = abs(600+2*T(3)-4*T(4));
[rem, pos] = max(L);
% elseif res < 0.1
% break
end
end
disp(T)
326.7935 307.2074 302.0362 301.0181
disp(L)
0.0334 0 0.0806 0
  4 Comments
Image Analyst
Image Analyst on 25 Feb 2022
I'm sure @_ knows that rem is a built-in function and it was just an oversight that he did not use a different name for that variable.
Voss
Voss on 25 Feb 2022
Edited: Voss on 25 Feb 2022
@Image Analyst: Not an oversight. I chose not to point that out because, in general, I try not to change things from the original code that are irrelevant to the problem at hand, in an attempt to illustrate the problem and solution as clearly as possible to the person asking the question, at their level of experience/understanding, without getting bogged down with details or considerations that have no bearing on the actual problem being addressed. To each his own.
But you're right, it is useful to point out that rem should not be used as a variable name.

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 25 Feb 2022
You're making the same mistake that nearly everyone who writes a while loop makes: you are not using a failsafe to prevent an infinite loop. I tell people this several times per week. So if your code never generates a condition to exit (because of a logic error like you have), then the loop will continue forever. The solution is to use a loop counter and maximum number of iterations that you expect as a failsafe:
loopCounter = 1;
maxIterations = 1000000; % Way more than you ever expect to be done.
while (remainder > 0.1) && (loopCounter <= maxIterations)
% Code to set remainder....
% Now at the end of the loop, increment the loop counter to make sure we don't go infinite
loopCounter = loopCounter + 1;
end

Community Treasure Hunt

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

Start Hunting!