How to end an indefinite loop?
3 次查看(过去 30 天)
显示 更早的评论
My goal;
- Guess values of T
- Use T to calculate L
- Find largest L element
- Update the T element correlating to the largest L element. Keep other T's unchanged.
- Use updated T array to find L again.
- 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 个评论
采纳的回答
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 个评论
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
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
0 个评论
Walter Roberson
2022-3-1
%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 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
1 个评论
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 Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!