How to Terminate a Loop After Reaching the Desired Value?

3 views (last 30 days)
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.

Accepted Answer

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

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!