MATLAB: While loop for tolerance between two vectors

tolerancewhile loop

Please can you help me to find a code for a while loop;
pseudo code
code to calculate vector 1
code to calculate vector 2
while the distance between vector 1 and vector 2 is greater than a tolerance do;
replace vector 1 with vector 2
code to work out the new vector 2
end
outputs the first "vector 2" for which the tolerance is met

Best Answer

vector1 = rand(1, 100);
vector2 = rand(1, 100);
while norm(vector1 - vector2) > tol
vector1 = vector2;
vector2 = rand(1, 100);
end
Please note, that this does not necessarily stop in finite time. Therefore a good programmer would add a counter:
counter = 0;
counterLimit = 1e6;
while norm(vector1 - vector2) > tol && counter < counterLimit
counter = counter + 1;
...
end
if count == counterLimit
warning('failed')
end