Trouble with While and If loops
Mostrar comentarios más antiguos
Hi, I am trying to test out an iterative process to converge on one value. I am trying to use while and if statements to converge on a test value of 5; however, my process causes matlab to infinitely loop between 5.00 and 5.100 rather than stopping at 5. My code is below and any advice would be much appreciated. Thanks in advance.
J. Ryan Kersh
while n~= 5
if n<5
n=n+.1
end
if n>5
n=n-.1
end
end
1 comentario
Yatin
el 14 de Oct. de 2013
What is the initial value of n that you are using? Based on your increments, I think that your values of n would be typically oscillating between some constant values around 5.00. May be using function like abs(), floor(), ceil() will be useful in this case for convergence.
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!