Variables in for loop not updating in following iterations

조회 수: 2 (최근 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 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