Variables in for loop not updating in following iterations

3 ビュー (過去 30 日間)
Abigail
Abigail 2023 年 1 月 28 日
コメント済み: Voss 2023 年 1 月 28 日
Hello,
I am trying to write a for loop with variables a and b that start at 1 and 6. The first iteration produces 0.5176 and 12, which is correct. However, all following iterations are the exact same. The second iteration should be 0.261 and 24. What am I doing wrong?
N=input('number of iterations')
a=1
b=6
for x=N
b=2*b;
a=sqrt((2-sqrt(4-a^2)));
L=(a*b)/2;
u=L/(sqrt(1-((a^2)/2)));
pi_est=(u+L)/2;
if pi_est<=3.141592
break
end
er=(u-L)/2;
end
pi_est
N

採用された回答

Voss
Voss 2023 年 1 月 28 日
The loop has only one iteration because N is a scalar
for x=N
To loop N times, you can use
for x=1:N
  2 件のコメント
Abigail
Abigail 2023 年 1 月 28 日
That makes so much sense, thank you very much.
Voss
Voss 2023 年 1 月 28 日
You're welcome!

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

その他の回答 (0 件)

カテゴリ

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