Variables in for loop not updating in following iterations

2 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

Voss
Voss il 28 Gen 2023
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

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by