MATLAB Answers

# For and While loop give different answers

조회 수: 6(최근 30일)
HAHA 23 Mar 2020
댓글: Rena Berman 14 May 2020 16:33
% 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

표시 이전 댓글 수: 1
HAHA 23 Mar 2020
even tho I make same increment, answer is different.....
Stephen Cobeldick 24 Mar 2020
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 14 May 2020 16:33
(Answers Dev) Restored edit

로그인 to comment.

### 답변(1개)

Geoff Hayes 23 Mar 2020
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.

#### 댓글 수: 0

로그인 to comment.

이 질문에 답변하려면 로그인을(를) 수행하십시오.

Translated by