Variables in for loop not updating in following iterations

2 Ansichten (letzte 30 Tage)
Abigail
Abigail am 28 Jan. 2023
Kommentiert: Voss am 28 Jan. 2023
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

Akzeptierte Antwort

Voss
Voss am 28 Jan. 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

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by