How to Terminate a Loop After Reaching the Desired Value?

4 visualizaciones (últimos 30 días)
Sarah Evans
Sarah Evans el 5 de Mayo de 2021
Respondida: KSSV el 5 de Mayo de 2021
Right now I need to write a loop of code that repeats the function
r(n+1)=L*r(n)
(Such that the 'r' value on the left = the matrix 'L' times the value of 'r' that came before the value on the left.)
Until r converges/stops changing (or until r(n+1)=r(n)).
All I have so far is
while r(n+1) ~= r(n)
r(n+1)=L*r(n)
end
Does anybody know how to stop a loop when a series converges? I'm so lost.

Respuesta aceptada

KSSV
KSSV el 5 de Mayo de 2021
tol = 10^-5 ; % fix your tolerance value
while abs(r(n+1)-r(n))>tol
r(n+1)=L*r(n)
end

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