Iterative loop storing variables
조회 수: 2 (최근 30일)
이전 댓글 표시
So I'm using a 'while-end' loop for a particular code in MATLAB, the particular criterion to end the loop I'm using is such that the value for a variable between the nth iteration and nth-1 iteration is a pre-defined value.
e.g.
Inputs for: variables, constants, etc.
while: abs(variable 'nth' iteration - variable 'nth-1' iteration) >= pre-defined value
- - - MATLAB CODE HERE - - - ETC.
end
I was just wondering how would I perhaps go about scripting it, do I have to store the variable each iteration and bring it back into the next iteration? I'm struggling to see how to do this exactly, I'm not particularly use to loops in MATLAB.
I would appreciate a push in the right direction!
Conor.
댓글 수: 0
답변 (1개)
Cedric
2015년 1월 29일
Here is an example
a = 100 ; % Initial value.
a_last = Inf ;
threshold = 5 ;
while abs(a - a_last) >= threshold
% Save previous value of a.
a_last = a ;
% Compute new value.
a = a - sqrt( a ) ;
% Do whatever else..
disp( a ) ;
end
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!