For and While loop give different answers

조회 수: 1 (최근 30일)
HAHA
HAHA 2020년 3월 23일
댓글: Rena Berman 2020년 5월 14일
% while loop
X1=1.5;
Y1=5;
Z1=26;
while Y1 < 30
cot = cotd(Y1);
while Z1 < 50
Zt1 = tand(Z1) * (((2.2) * X1^2));
check = (Zt1-cot)/cot;
if check <= 10^-2
break
end
Z1 = Z1+10^-5;
end
Xn1 = X1*sind(Z1);
X2 = sqrt((((0.2)* X1^2*sind(Z1)^2) + 2));
Z2 = asind(Xn1/X2);
Y2 = acotd(tand(Z2) * (((2.2) * X2^2)));
X3 = sqrt((((0.2)* X2^2*sind(Z2)^2) + 2));
Z3 = asind(Xn1/X3);
Y3 = acotd(tand(Z3) * (((2.2) * X3^2)));
X4 = sqrt((((0.2)* X3^2*sind(Z3)^2) + 2));
X4_2 = 1.6;
X4_1 = (X4 - X4_2);
check2 = X4_1/X4_2;
if check2 <= 10^-5
break
end
Y1 = Y1+10^-5;
end
--------------------
% for loop
X1=1.5;
for Y1 = (5:0.01:30)
cot = cotd(Y1);
for Z1 = 26:2:45
Zt1 = tand(Z1) * (((2.2) * X1^2));;
check = (Zt1-cot)/cot;
if check <=10^-2
break
end
Xn1 = X1*sind(Z1);
X2 = sqrt((((0.2)* X1^2*sind(Z1)^2) + 2));
Z2 = asind(Xn1/X2);
Y2 = acotd(tand(Z2) * (((2.2) * X2^2)));
X3 = sqrt((((0.2)* X2^2*sind(Z2)^2) + 2));
Z3 = asind(Xn1/X3);
Y3 = acotd(tand(Z3) * (((2.2) * X3^2)));
X4 = sqrt((((0.2)* X3^2*sind(Z3)^2) + 2));
X4_2 = 1.6;
X4_1 = (X4 - X4_2);
check2 = X4_1/X4_2;
if check2 <= 10^-5
break
end
end
end
I dont know why this two code end up with different velues of Y1, cot and check.
For While loop, check is -0.7888 but check is 0.3939 at for loop.
I wanna fix for loop and the values are same as while loop
Thank you
  댓글 수: 4
Stephen23
Stephen23 2020년 3월 24일
Original question from Google Cache:
"For and While loop give different answers"
% while loop
X1=1.5;
Y1=5;
Z1=26;
while Y1 < 30
cot = cotd(Y1);
while Z1 < 50
Zt1 = tand(Z1) * (((2.2) * X1^2));
check = (Zt1-cot)/cot;
if check <= 10^-2
break
end
Z1 = Z1+10^-5;
end
Xn1 = X1*sind(Z1);
X2 = sqrt((((0.2)* X1^2*sind(Z1)^2) + 2));
Z2 = asind(Xn1/X2);
Y2 = acotd(tand(Z2) * (((2.2) * X2^2)));
X3 = sqrt((((0.2)* X2^2*sind(Z2)^2) + 2));
Z3 = asind(Xn1/X3);
Y3 = acotd(tand(Z3) * (((2.2) * X3^2)));
X4 = sqrt((((0.2)* X3^2*sind(Z3)^2) + 2));
X4_2 = 1.6;
X4_1 = (X4 - X4_2);
check2 = X4_1/X4_2;
if check2 <= 10^-5
break
end
Y1 = Y1+10^-5;
end
--------------------
% for loop
X1=1.5;
for Y1 = (5:0.01:30)
cot = cotd(Y1);
for Z1 = 26:2:45
Zt1 = tand(Z1) * (((2.2) * X1^2));;
check = (Zt1-cot)/cot;
if check <=10^-2
break
end
Xn1 = X1*sind(Z1);
X2 = sqrt((((0.2)* X1^2*sind(Z1)^2) + 2));
Z2 = asind(Xn1/X2);
Y2 = acotd(tand(Z2) * (((2.2) * X2^2)));
X3 = sqrt((((0.2)* X2^2*sind(Z2)^2) + 2));
Z3 = asind(Xn1/X3);
Y3 = acotd(tand(Z3) * (((2.2) * X3^2)));
X4 = sqrt((((0.2)* X3^2*sind(Z3)^2) + 2));
X4_2 = 1.6;
X4_1 = (X4 - X4_2);
check2 = X4_1/X4_2;
if check2 <= 10^-5
break
end
end
end
I dont know why this two code end up with different velues of Y1, cot and check.
For While loop, check is -0.7888 but check is 0.3939 at for loop.
I wanna fix for loop and the values are same as while loop
Thank you
Rena Berman
Rena Berman 2020년 5월 14일
(Answers Dev) Restored edit

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

답변 (1개)

Geoff Hayes
Geoff Hayes 2020년 3월 23일
I formatted your code so it might be a little more clear what is going on. Here is the while loop code
while Y1 < 30
cot = cotd(Y1);
while Z1 < 50
Zt1 = tand(Z1) * (((2.2) * X1^2));
check = (Zt1-cot)/cot;
if check <= 10^-2
break
end
Z1 = Z1+10^-5;
end
% the remaining code is in the outer while loop <-----------------------
Xn1 = X1*sind(Z1);
X2 = sqrt((((0.2)* X1^2*sind(Z1)^2) + 2));
Z2 = asind(Xn1/X2);
Y2 = acotd(tand(Z2) * (((2.2) * X2^2)));
X3 = sqrt((((0.2)* X2^2*sind(Z2)^2) + 2));
Z3 = asind(Xn1/X3);
Y3 = acotd(tand(Z3) * (((2.2) * X3^2)));
X4 = sqrt((((0.2)* X3^2*sind(Z3)^2) + 2));
X4_2 = 1.6;
X4_1 = (X4 - X4_2);
check2 = X4_1/X4_2;
if check2 <= 10^-5
break
end
Y1 = Y1+10^-5;
end
Now contrast that with the for loop code
for Y1 = (5:0.01:30)
cot = cotd(Y1);
for Z1 = 26:2:45
Zt1 = tand(Z1) * (((2.2) * X1^2));;
check = (Zt1-cot)/cot;
if check <=10^-2
break
end
% the remaining code is in the inner for loop <-----------------------
Xn1 = X1*sind(Z1);
X2 = sqrt((((0.2)* X1^2*sind(Z1)^2) + 2));
Z2 = asind(Xn1/X2);
Y2 = acotd(tand(Z2) * (((2.2) * X2^2)));
X3 = sqrt((((0.2)* X2^2*sind(Z2)^2) + 2));
Z3 = asind(Xn1/X3);
Y3 = acotd(tand(Z3) * (((2.2) * X3^2)));
X4 = sqrt((((0.2)* X3^2*sind(Z3)^2) + 2));
X4_2 = 1.6;
X4_1 = (X4 - X4_2);
check2 = X4_1/X4_2;
if check2 <= 10^-5
break
end
end
You have a chunk of code that is evaluated in the outer while loop and that same code appears to be evaluated in the inner for loop. Please confirm which inner or outer loop should be evaluating this code and then make consistent for both.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by