MATLAB Answers

0

how to break from the following for loop when beta( J+1) become zero

Asked by Nitish Reddy Kotkur on 28 Sep 2019
Latest activity Commented on by Nitish Reddy Kotkur on 21 Oct 2019 at 23:42
for j=2:inf
w = A*V(:,j) - beta(j)*V(:,j-1);
alpha(j) = w'*V(:,j);
w = w - alpha(j)*V(:,j);
beta(j+1) = norm(w,2);
V(:,j+1) = w/beta(j+1);
loopcnt = loopcnt + 1;
end

  0 Comments

Sign in to comment.

1 Answer

Answer by the cyclist
on 28 Sep 2019
 Accepted Answer

Put this inside your loop:
if beta(j+1) == 0
break
end
You might not want to check for exact equality, because of possible floating point error. Instead, you could check like this
if abs(beta(j+1)) < 1.e-8
break
end
or use some other suitably small tolerance.

  3 Comments

if beta(j+1) == 0
break
end
i tried this but still the loop is iterating infinite times
if abs(beta(j+1)) < 1.e-8
break
end
and the problem with the above one is, i guess its terminating even before the value is zero there by resulting a wrong computation
As I said, you need to try a "suitably small tolerance". You could try a smaller power.
Be aware that beta(j+1) may never be exactly zero, due to floating-point accuracy.
function [] = lanczos(A, m)
this is a function whick takes A as input which is a matrix and reduces it to smaller size .And i have got a for loop
for j=2:inf
w = A*V(:,j) - beta(j)*V(:,j-1);
alpha(j) = w'*V(:,j);
w = w - alpha(j)*V(:,j);
beta(j+1) = norm(w,2);
loopcnt = loopcnt + 1;
if abs(beta(j+1)) = 0
break
end
now i need the control to come out of loop when beta(j+1) is equal to zero ,but its looping continuously may be because of some floating point error.so i tried something like this
if abs(beta(j+1)) < 0.0001 should come out of loop which worked fine for smaller matrix sizes.
but when matrix size got bigger even its not working and loop is running continuosly can some one suggest me a way to avoid this problem and run the loop properly and break it when beta(j+1) become zero

Sign in to comment.