Borrar filtros
Borrar filtros

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

1 visualización (últimos 30 días)
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

Respuesta aceptada

the cyclist
the cyclist el 28 de Sept. de 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 comentarios
the cyclist
the cyclist el 28 de Sept. de 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 el 21 de Oct. de 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

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by