How to Terminate a Loop After Reaching the Desired Value?

3 次查看(过去 30 天)
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.

采纳的回答

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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by