フィルターのクリア

How to end an indefinite loop?

3 ビュー (過去 30 日間)
Hosoda Hung
Hosoda Hung 2022 年 3 月 1 日
編集済み: Voss 2022 年 3 月 1 日
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. I had this issue previously for a smaller scale, but it was solved by updating elements of L based on which element of T change. All suggestion welcome
***Edit: Is there is a more efficient way to ask for inputs that will be elements in the T array aside from input function? I was looking in inputdlg function but I'm unsure how to assign the answers in an array.
%Start
clearvars; clc; disp("Problem 2")
%Guess
T = zeros(12,1);
T(1) = input("T1? ");
T(2) = input("T2? ");
T(3) = input("T3? ");
T(4) = input("T4? ");
T(5) = input("T5? ");
T(6) = input("T6? ");
T(7) = input("T7? ");
T(8) = input("T8? ");
T(9) = input("T9? ");
T(10) = input("T10? ");
T(11) = input("T11? ");
T(12) = input("T12? ");
%Leftover
L = zeros(12,1);
L(1) = abs(215-8*T(1)+2*T(2)+4*T(4));
L(2) = abs(2*T(1)-8*T(2)+2*T(3)+4*T(5)+15);
L(3) = abs(2*T(2)-9*T(3)+2*T(6)+107.5);
L(4) = abs(T(1)-4*T(4)+T(5)+T(7)+100);
L(5) = abs(T(2)+T(4)-4*T(5)+T(6)+T(8));
L(6) = abs(T(3)+2*T(5)+T(9)+100-9*T(6));
L(7) = abs(T(4)-4*T(7)+T(8)+T(10)+100);
L(8) = abs(T(5)+T(7)+T(9)+T(11)-4*T(8));
L(9) = abs(T(6)+2*T(8)+T(12)+100-9*T(9));
L(10) = abs(2*T(7)+T(11)+100-4*T(10));
L(11) = abs(2*T(8)+T(10)+T(12)-4*T(11));
L(12) = abs(T(9)+T(11)+50-4.5*T(12));
%Max leftover
[leftover, pos] = max(L);
%Iteration Count
iterations = 0;
%Loop
while residual > 0.1
if(pos==1)
T(1) = (215+2*T(2)+4*T(4))/8;
L(1) = abs(215-8*T(1)+2*T(2)+4*T(4));
L(2) = abs(2*T(1)-8*T(2)+2*T(3)+4*T(5)+15);
L(4) = abs(T(1)-4*T(4)+T(5)+T(7)+100);
[leftover, pos] = max(L);
iterations = iterations + 1;
elseif(pos==2)
T(2) = (2*T(1)+2*T(3)+4*T(5)+15)/8;
L(1) = abs(215-8*T(1)+2*T(2)+4*T(4));
L(2) = abs(2*T(1)-8*T(2)+2*T(3)+4*T(5)+15);
L(3) = abs(2*T(2)-9*T(3)+2*T(6)+107.5);
L(5) = abs(T(2)+T(4)-4*T(5)+T(6)+T(8));
[leftover, pos] = max(L);
iterations = iterations + 1;
elseif(pos==3)
T(3) = (2*T(2)+2*T(6)+107.5)/9;
L(2) = abs(2*T(1)-8*T(2)+2*T(3)+4*T(5)+15);
L(3) = abs(2*T(2)-9*T(3)+2*T(6)+107.5);
L(6) = abs(T(3)+2*T(5)+T(9)+100-9*T(6));
[leftover, pos] = max(L);
iterations = iterations + 1;
elseif(pos==4)
T(4) = (T(1)+T(5)+T(7)+100)/4;
L(1) = abs(215-8*T(1)+2*T(2)+4*T(4));
L(4) = abs(T(1)-4*T(4)+T(5)+T(7)+100);
L(5) = abs(T(2)+T(4)-4*T(5)+T(6)+T(8));
L(7) = abs(T(4)-4*T(7)+T(8)+T(10)+100);
[leftover, pos] = max(L);
iterations = iterations + 1;
elseif(pos==5)
T(5) = (T(2)+T(4)+T(6)+T(8))/4;
L(2) = abs(2*T(1)-8*T(2)+2*T(3)+4*T(5)+15);
L(4) = abs(T(1)-4*T(4)+T(5)+T(7)+100);
L(5) = abs(T(2)+T(4)-4*T(5)+T(6)+T(8));
L(6) = abs(T(3)+2*T(5)+T(9)+100-9*T(6));
L(8) = abs(T(5)+T(7)+T(9)+T(11)-4*T(8));
[leftover, pos] = max(L);
iterations = iterations + 1;
elseif(pos==6)
T(6) = (T(3)+2*T(5)+T(9)+100)/9;
L(3) = abs(2*T(2)-9*T(3)+2*T(6)+107.5);
L(5) = abs(T(2)+T(4)-4*T(5)+T(6)+T(8));
L(6) = abs(T(3)+2*T(5)+T(9)+100-9*T(6));
L(9) = abs(T(6)+2*T(8)+T(12)+100-9*T(9));
[leftover, pos] = max(L);
iterations = iterations + 1;
elseif(pos==7)
T(7) = (T(4)+T(8)+T(10)+100)/4;
L(4) = abs(T(1)-4*T(4)+T(5)+T(7)+100);
L(7) = abs(T(4)-4*T(7)+T(8)+T(10)+100);
L(8) = abs(T(5)+T(7)+T(9)+T(11)-4*T(8));
L(10) = abs(2*T(7)+T(11)+100-4*T(10));
[leftover, pos] = max(L);
iterations = iterations + 1;
elseif(pos==8)
T(8) = (T(5)+T(7)+T(9)+T(11))/4;
L(5) = abs(T(2)+T(4)-4*T(5)+T(6)+T(8));
L(7) = abs(T(4)-4*T(7)+T(8)+T(10)+100);
L(8) = abs(T(5)+T(7)+T(9)+T(11)-4*T(8));
L(9) = abs(T(6)+2*T(8)+T(12)+100-9*T(9));
L(11) = abs(2*T(8)+T(10)+T(12)-4*T(11));
[leftover, pos] = max(L);
iterations = iterations + 1;
elseif(pos==9)
T(9) = (T(6)+2*T(8)+T(12)+100);
L(6) = abs(T(3)+2*T(5)+T(9)+100-9*T(6));
L(8) = abs(T(5)+T(7)+T(9)+T(11)-4*T(8));
L(9) = abs(T(6)+2*T(8)+T(12)+100-9*T(9));
[leftover, pos] = max(L);
iterations = iterations + 1;
elseif(pos==10)
T(10) = (2*T(7)+T(11)+100)/4;
L(7) = abs(T(4)-4*T(7)+T(8)+T(10)+100);
L(10) = abs(2*T(7)+T(11)+100-4*T(10));
L(11) = abs(2*T(8)+T(10)+T(12)-4*T(11));
[leftover, pos] = max(L);
iterations = iterations + 1;
elseif(pos==11)
T(11) = (2*T(8)+T(10)+T(12))/4;
L(8) = abs(T(5)+T(7)+T(9)+T(11)-4*T(8));
L(10) = abs(2*T(7)+T(11)+100-4*T(10));
L(11) = abs(2*T(8)+T(10)+T(12)-4*T(11));
[leftover, pos] = max(L);
iterations = iterations + 1;
elseif(pos==12)
T(12) = (T(9)+T(11)+50)/4.5;
L(9) = abs(T(6)+2*T(8)+T(12)+100-9*T(9));
L(11) = abs(2*T(8)+T(10)+T(12)-4*T(11));
L(12) = abs(T(9)+T(11)+50-4.5*T(12));
[leftover, pos] = max(L);
iterations = iterations + 1;
elseif leftover < 0.1
break
end
end
  7 件のコメント
Walter Roberson
Walter Roberson 2022 年 3 月 1 日
str2double() inputdlg
Or consider uitable()
Hosoda Hung
Hosoda Hung 2022 年 3 月 1 日
編集済み: Hosoda Hung 2022 年 3 月 1 日
I just used zeros for all inputs. The goal was for inputs to not matter since the program should've eventually converged to the right solution.

サインインしてコメントする。

採用された回答

Voss
Voss 2022 年 3 月 1 日
編集済み: Voss 2022 年 3 月 1 日
Regarding why the loop never stops, I believe there is a mistake here:
T(9) = (T(6)+2*T(8)+T(12)+100);
That line should be:
T(9) = (T(6)+2*T(8)+T(12)+100)/9;
(divide by 9). Change that line (and change "residual" to "leftover") and the loop will stop.
  3 件のコメント
Walter Roberson
Walter Roberson 2022 年 3 月 1 日
Ah!
But why /9 instead of dividing by a different constant ?
Voss
Voss 2022 年 3 月 1 日
編集済み: Voss 2022 年 3 月 1 日
To be consistent with the others, which are all "solving for" their T(i) using the expressions for L.
Specifically, the coefficient of T(9) is -9 here:
L(9) = abs(T(6)+2*T(8)+T(12)+100-9*T(9));
When L(9) is the maximum element of L, T(9) is updated using that relation (with L(9) = 0 implied):
T(9) = (T(6)+2*T(8)+T(12)+100)/9;

サインインしてコメントする。

その他の回答 (2 件)

Benjamin Thompson
Benjamin Thompson 2022 年 3 月 1 日
sscanf and textscan can process a string with multiple entries for T into a vector if that is a better solution for you

Walter Roberson
Walter Roberson 2022 年 3 月 1 日
%Start
clearvars; clc; disp("Problem 2")
Problem 2
%Guess
T = zeros(12,1);
%{
T(1) = input("T1? ");
T(2) = input("T2? ");
T(3) = input("T3? ");
T(4) = input("T4? ");
T(5) = input("T5? ");
T(6) = input("T6? ");
T(7) = input("T7? ");
T(8) = input("T8? ");
T(9) = input("T9? ");
T(10) = input("T10? ");
T(11) = input("T11? ");
T(12) = input("T12? ");
%}
%Leftover
L = zeros(12,1);
L(1) = abs(215-8*T(1)+2*T(2)+4*T(4));
L(2) = abs(2*T(1)-8*T(2)+2*T(3)+4*T(5)+15);
L(3) = abs(2*T(2)-9*T(3)+2*T(6)+107.5);
L(4) = abs(T(1)-4*T(4)+T(5)+T(7)+100);
L(5) = abs(T(2)+T(4)-4*T(5)+T(6)+T(8));
L(6) = abs(T(3)+2*T(5)+T(9)+100-9*T(6));
L(7) = abs(T(4)-4*T(7)+T(8)+T(10)+100);
L(8) = abs(T(5)+T(7)+T(9)+T(11)-4*T(8));
L(9) = abs(T(6)+2*T(8)+T(12)+100-9*T(9));
L(10) = abs(2*T(7)+T(11)+100-4*T(10));
L(11) = abs(2*T(8)+T(10)+T(12)-4*T(11));
L(12) = abs(T(9)+T(11)+50-4.5*T(12));
%Max leftover
[leftover, pos] = max(L);
%Iteration Count
iterations = 0;
%Loop
while leftover > 0.1 & iterations < 10
if(pos==1)
T(1) = (215+2*T(2)+4*T(4))/8;
L(1) = abs(215-8*T(1)+2*T(2)+4*T(4));
L(2) = abs(2*T(1)-8*T(2)+2*T(3)+4*T(5)+15);
L(4) = abs(T(1)-4*T(4)+T(5)+T(7)+100);
[leftover, pos] = max(L)
iterations = iterations + 1;
elseif(pos==2)
T(2) = (2*T(1)+2*T(3)+4*T(5)+15)/8;
L(1) = abs(215-8*T(1)+2*T(2)+4*T(4));
L(2) = abs(2*T(1)-8*T(2)+2*T(3)+4*T(5)+15);
L(3) = abs(2*T(2)-9*T(3)+2*T(6)+107.5);
L(5) = abs(T(2)+T(4)-4*T(5)+T(6)+T(8));
[leftover, pos] = max(L)
iterations = iterations + 1;
elseif(pos==3)
T(3) = (2*T(2)+2*T(6)+107.5)/9;
L(2) = abs(2*T(1)-8*T(2)+2*T(3)+4*T(5)+15);
L(3) = abs(2*T(2)-9*T(3)+2*T(6)+107.5);
L(6) = abs(T(3)+2*T(5)+T(9)+100-9*T(6));
[leftover, pos] = max(L)
iterations = iterations + 1;
elseif(pos==4)
T(4) = (T(1)+T(5)+T(7)+100)/4;
L(1) = abs(215-8*T(1)+2*T(2)+4*T(4));
L(4) = abs(T(1)-4*T(4)+T(5)+T(7)+100);
L(5) = abs(T(2)+T(4)-4*T(5)+T(6)+T(8));
L(7) = abs(T(4)-4*T(7)+T(8)+T(10)+100);
[leftover, pos] = max(L)
iterations = iterations + 1;
elseif(pos==5)
T(5) = (T(2)+T(4)+T(6)+T(8))/4;
L(2) = abs(2*T(1)-8*T(2)+2*T(3)+4*T(5)+15);
L(4) = abs(T(1)-4*T(4)+T(5)+T(7)+100);
L(5) = abs(T(2)+T(4)-4*T(5)+T(6)+T(8));
L(6) = abs(T(3)+2*T(5)+T(9)+100-9*T(6));
L(8) = abs(T(5)+T(7)+T(9)+T(11)-4*T(8));
[leftover, pos] = max(L)
iterations = iterations + 1;
elseif(pos==6)
T(6) = (T(3)+2*T(5)+T(9)+100)/9;
L(3) = abs(2*T(2)-9*T(3)+2*T(6)+107.5);
L(5) = abs(T(2)+T(4)-4*T(5)+T(6)+T(8));
L(6) = abs(T(3)+2*T(5)+T(9)+100-9*T(6));
L(9) = abs(T(6)+2*T(8)+T(12)+100-9*T(9));
[leftover, pos] = max(L)
iterations = iterations + 1;
elseif(pos==7)
T(7) = (T(4)+T(8)+T(10)+100)/4;
L(4) = abs(T(1)-4*T(4)+T(5)+T(7)+100);
L(7) = abs(T(4)-4*T(7)+T(8)+T(10)+100);
L(8) = abs(T(5)+T(7)+T(9)+T(11)-4*T(8));
L(10) = abs(2*T(7)+T(11)+100-4*T(10));
[leftover, pos] = max(L)
iterations = iterations + 1;
elseif(pos==8)
T(8) = (T(5)+T(7)+T(9)+T(11))/4;
L(5) = abs(T(2)+T(4)-4*T(5)+T(6)+T(8));
L(7) = abs(T(4)-4*T(7)+T(8)+T(10)+100);
L(8) = abs(T(5)+T(7)+T(9)+T(11)-4*T(8));
L(9) = abs(T(6)+2*T(8)+T(12)+100-9*T(9));
L(11) = abs(2*T(8)+T(10)+T(12)-4*T(11));
[leftover, pos] = max(L)
iterations = iterations + 1;
elseif(pos==9)
disp('before')
disp(T)
disp(L)
T(9) = (T(6)+2*T(8)+T(12)+100);
L(6) = abs(T(3)+2*T(5)+T(9)+100-9*T(6));
L(8) = abs(T(5)+T(7)+T(9)+T(11)-4*T(8));
L(9) = abs(T(6)+2*T(8)+T(12)+100-9*T(9));
[leftover, pos] = max(L)
iterations = iterations + 1;
disp('after')
disp(T)
disp(L)
elseif(pos==10)
T(10) = (2*T(7)+T(11)+100)/4;
L(7) = abs(T(4)-4*T(7)+T(8)+T(10)+100);
L(10) = abs(2*T(7)+T(11)+100-4*T(10));
L(11) = abs(2*T(8)+T(10)+T(12)-4*T(11));
[leftover, pos] = max(L)
iterations = iterations + 1;
elseif(pos==11)
T(11) = (2*T(8)+T(10)+T(12))/4;
L(8) = abs(T(5)+T(7)+T(9)+T(11)-4*T(8));
L(10) = abs(2*T(7)+T(11)+100-4*T(10));
L(11) = abs(2*T(8)+T(10)+T(12)-4*T(11));
[leftover, pos] = max(L)
iterations = iterations + 1;
elseif(pos==12)
T(12) = (T(9)+T(11)+50)/4.5;
L(9) = abs(T(6)+2*T(8)+T(12)+100-9*T(9));
L(11) = abs(2*T(8)+T(10)+T(12)-4*T(11));
L(12) = abs(T(9)+T(11)+50-4.5*T(12));
[leftover, pos] = max(L)
iterations = iterations + 1;
elseif leftover < 0.1
break
end
end
leftover = 126.8750
pos = 4
leftover = 131.7188
pos = 7
leftover = 165.8594
pos = 10
leftover = 126.8750
pos = 1
leftover = 107.5000
pos = 3
leftover = 124.3576
pos = 2
leftover = 111.9444
pos = 6
leftover = 112.4383
pos = 9
before
42.7344 15.5447 11.9444 31.7188 0 12.4383 32.9297 0 0 41.4648 0 0
31.0894 0.0000 55.9660 48.7891 59.7017 0.0000 41.4648 32.9297 112.4383 0 41.4648 50.0000
leftover = 899.5062
pos = 9
after
42.7344 15.5447 11.9444 31.7188 0 12.4383 32.9297 0 112.4383 41.4648 0 0
31.0894 0.0000 55.9660 48.7891 59.7017 112.4383 41.4648 145.3680 899.5062 0 41.4648 50.0000
before
42.7344 15.5447 11.9444 31.7188 0 12.4383 32.9297 0 112.4383 41.4648 0 0
31.0894 0.0000 55.9660 48.7891 59.7017 112.4383 41.4648 145.3680 899.5062 0 41.4648 50.0000
leftover = 899.5062
pos = 9
after
42.7344 15.5447 11.9444 31.7188 0 12.4383 32.9297 0 112.4383 41.4648 0 0
31.0894 0.0000 55.9660 48.7891 59.7017 112.4383 41.4648 145.3680 899.5062 0 41.4648 50.0000
  1 件のコメント
Walter Roberson
Walter Roberson 2022 年 3 月 1 日
You have
elseif(pos==9)
T(9) = (T(6)+2*T(8)+T(12)+100);
L(6) = abs(T(3)+2*T(5)+T(9)+100-9*T(6));
L(8) = abs(T(5)+T(7)+T(9)+T(11)-4*T(8));
L(9) = abs(T(6)+2*T(8)+T(12)+100-9*T(9));
[leftover, pos] = max(L);
iterations = iterations + 1;
Suppose that you reach this section of code, and suppose that last time it calculated new values (different from the existing ones), but "somehow" L(9) still ended up as the largest value, so pos ends up as 9 again.
Now under that circumstance, you reach the pos==9 case again. What will you calculate this time that is different than what you calculated last time?
T(9) = (T(6)+2*T(8)+T(12)+100);
That is based on T(6), T(8), T(12), none of which the pos==9 modifies. So if you reach pos==9 twice in a row, the T(9) that are calculated will be exactly the same.
L(6) = abs(T(3)+2*T(5)+T(9)+100-9*T(6));
The previous pos==9 did not alter T(3), T(5), or T(6), and we have just shown that T(9) will be calculated the same way both iterations. So L(6) is going to be the same for both iterations.
L(8) = abs(T(5)+T(7)+T(9)+T(11)-4*T(8));
T(5), T(7), T(8), T(11) are not being changed by the pos==9 section, and we just showed that T(9) will be calculated the same way if you reach this section twice in a row. So L(8) is going to be the same as the previous calculation.
L(9) = abs(T(6)+2*T(8)+T(12)+100-9*T(9));
T(6), T(8), T(12) are not being changed by the section of code, T(9) will be the same as the previous time. So L(9) is going to have the same outcome.
[leftover, pos] = max(L);
So if the previous time you reached pos==9 and it happened to still have L(9) as the maximum value after the calculation... then L(9) is going to be exactly the same as before, and pos would be assigned 9 again.
It looks to me as if all of the pos== sections have the same issue: if you cannot guarantee that the corresponding new L(pos) value will be smaller than at least one other existing L value, then you are caught in a loop.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by