How to compare previous results in iteration?
    5 views (last 30 days)
  
       Show older comments
    
I have a program with for loop having 1to 500. I need to compare the output of previous and current iteration, if the outputs are matching the program has to stop. Otherwise the loop has to continue till the match is achieved.
For example:
for i=1 to 500
[Some mathematical calcultion]
output= 109;
end
Expected output:
iteration has to stop when output [i] = output [i-1]
0 Comments
Answers (1)
  Torsten
      
      
 on 22 Jan 2023
        
      Edited: Torsten
      
      
 on 22 Jan 2023
  
      You should never check whether two variables are equal. Always check whether they are approximately equal:
difference = 1.0;
output_old = 0.0;
tolerance = 1e-6;
while difference > tolerance
  [Some mathematical calculations]
  output = ...;
  difference = abs(output_old-output);
  output_old = output;
end
3 Comments
See Also
Categories
				Find more on Logical 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!
