Variables in for loop not updating in following iterations

2 visualizaciones (últimos 30 días)
Abigail
Abigail el 28 de En. de 2023
Comentada: Voss el 28 de En. de 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

Respuesta aceptada

Voss
Voss el 28 de En. de 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
  2 comentarios
Abigail
Abigail el 28 de En. de 2023
That makes so much sense, thank you very much.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by