View Single Post
Mentor
P: 20,433

for loops in Matlab

You want your while loop to look something like this:

while hc(n) - hc(n-1) > 0.001
% Do some further calculations for hc(n) and hc(n-1)

end
final_result = hc(n)
For the while loop to end, it must be that eventually hc(n) - hc(n-1) < 0.001. If for some reason this doesn't happen, the loop is an infinite loop.

When the loop exits, the value of hc(n) is assigned to final_result, and this causes Ans = 3.123458 (whatever)
to be displayed.