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

1 次查看(过去 30 天)
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

采纳的回答

the cyclist
the cyclist 2019-9-28
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 个评论
the cyclist
the cyclist 2019-9-28
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 2019-10-21
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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by