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

1 view (last 30 days)
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

Accepted Answer

the cyclist
the cyclist on 28 Sep 2019
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
the cyclist
the cyclist on 28 Sep 2019
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.
Nitish Reddy Kotkur
Nitish Reddy Kotkur on 21 Oct 2019
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.

More Answers (0)

Categories

Find more on Programming 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!