19 views (last 30 days)

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

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

Christine Tobler
on 28 Oct 2019

There has been a lot of research on how to deal with floating-point error in the Lanczos algorithm, see the Wikipedia page for some discussion.

However, even in exact arithmetic, you should only expect beta(j) to become zero reliably after having performed n iterations (where the matrix A is of size n-by-n). Now usually, the goal of the Lanczos algorithm is to converge in significantly fewer iterations than that, so this is not usually a good convergence criterion.

It's still a good idea to check if beta(j) has become zero, because this would be a breakdown condition - if beta(j) becomes zero, it means that a complete eigenspace of A has been constructed. Either all information you need is already contained in this space, or you need to restart another Lanczos iteration with a new random vector.

Opportunities for recent engineering grads.

Apply TodayFind the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
## 4 Comments

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/486665-how-to-avoid-floating-point-error-and-figure-out-a-way-to-break-out-of-this-loop-such-that-it-genera#comment_758834

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/486665-how-to-avoid-floating-point-error-and-figure-out-a-way-to-break-out-of-this-loop-such-that-it-genera#comment_758834

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/486665-how-to-avoid-floating-point-error-and-figure-out-a-way-to-break-out-of-this-loop-such-that-it-genera#comment_758842

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/486665-how-to-avoid-floating-point-error-and-figure-out-a-way-to-break-out-of-this-loop-such-that-it-genera#comment_758842

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/486665-how-to-avoid-floating-point-error-and-figure-out-a-way-to-break-out-of-this-loop-such-that-it-genera#comment_758849

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/486665-how-to-avoid-floating-point-error-and-figure-out-a-way-to-break-out-of-this-loop-such-that-it-genera#comment_758849

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/486665-how-to-avoid-floating-point-error-and-figure-out-a-way-to-break-out-of-this-loop-such-that-it-genera#comment_758860

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/486665-how-to-avoid-floating-point-error-and-figure-out-a-way-to-break-out-of-this-loop-such-that-it-genera#comment_758860

Sign in to comment.