For and While loop give different answers

2 次查看(过去 30 天)
% 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

请先登录,再进行评论。

回答(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 中查找有关 Function Creation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by