Note that there are some explanatory texts on larger screens.

plurals
  1. POmatlab equality check going wrong. decides true on an arbitrary moment
    primarykey
    data
    text
    <p>THE CODE:</p> <pre><code>function[E] = eig_noshift(A) A_k = A; for(i=0:inf) [Qk,Rk] = qr(A_k); A_k1 = Rk*Qk; diag(A_k1) diag(A_k) isequal(diag(A_k1),diag(A_k)) if(isequal(diag(A_k1),diag(A_k))) break end A_k = A_k1; end E = diag(A_k); </code></pre> <p>I'm creating a matlab method that calculates a matrix' eigenvalues with the QR-algorithm. (which works) i'm trying to break the for loop if the next matrix diagonal is equal to the current diagonal (algorithm has converged)</p> <p>When executing the algorithm I get following output:</p> <pre><code>&gt;&gt; eig_noshift(A0) Warning: FOR loop index is too large. Truncating to 281474976710655. &gt; In eig_noshift at 3 ans = 4.8419 1.2591 -0.0011 ans = -22.2000 -14.6000 42.9000 ans = 0 ans = 4.9434 1.0611 0.0954 ans = 4.8419 1.2591 -0.0011 ans = 0 ans = 4.9881 1.0123 0.0996 ans = 4.9434 1.0611 0.0954 ans = 0 ans = 4.9976 1.0024 0.1000 ans = 4.9881 1.0123 0.0996 ans = 0 ans = 4.9995 1.0005 0.1000 ans = 4.9976 1.0024 0.1000 ans = 0 ans = 4.9999 1.0001 0.1000 ans = 4.9995 1.0005 0.1000 ans = 0 ans = 5.0000 1.0000 0.1000 ans = 4.9999 1.0001 0.1000 ans = 0 ans = 5.0000 1.0000 0.1000 ans = 5.0000 1.0000 0.1000 ans = 0 ans = 5.0000 1.0000 0.1000 ans = 5.0000 1.0000 0.1000 ans = 0 --- a few dozen more iterations that are the same --- ans = 5.0000 1.0000 0.1000 ans = 5.0000 1.0000 0.1000 ans = 0 ans = 5.0000 1.0000 0.1000 ans = 5.0000 1.0000 0.1000 ans = 1 </code></pre> <p>The 0/1 values are as you can see the truth value of checking the equality between the previous 2 vectors outputted. As you can see, the algo should have converged/stopped a long while ago, but didn't until some arbetrary moment. What am I doing wrong?</p> <p><strong>SOLUTION:</strong></p> <p>ofcourse I forgot to put matlab in format long and didn't think about it straight on. (I don't use matlab all that often) anyway the solution I have is adding a precision boundary to the method, resulting in following code:</p> <pre><code>function[E] = eig_noshift(A, prec) A_k = A; v_prec = [prec;prec;prec]; for(i=0:inf) [Qk,Rk] = qr(A_k); A_k1 = Rk*Qk; if(diag(A_k1) - diag(A_k) &lt; v_prec) break end A_k = A_k1; end E = diag(A_k); </code></pre> <p>Method stops when asked precision has been reached. cheers!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload