anderkve / FYS3150

https://anderkve.github.io/FYS3150
26 stars 14 forks source link

Project 2, Problem 4 #53

Closed oysthaga closed 2 years ago

oysthaga commented 2 years ago

For some reason, my Jacobi Rotation implementation isn't working. There seems to be a problem with this while-loop:

while ( Amax > eps ) // Rotate until max of-diagonal value is close to 0.
    {
        // run jacobi_rotate
        Amax = max_offdiag_symmetric(A_m, k, l); // Maximum of-diagonal value
        jacobi_rotate(A_m, R, k, l); // One rotation

        iterations += 1;
        if (iterations = maxiter)
        {
            break; 
            std::cout << "\nBroke\n";
        }
        if (Amax <= eps)
        {
            converged = true;
        }
    }

because printing iterations give 0, yet converged = true. When I set the eigenvalues to A_m.diag I get the diagonal of the original matrix A, and eigenvectors=R gives the original R=(identity matrix). I have also tested that max_offdiag_symmetric(A,k,l)=49, so it shouldn't converge on the first iteration.

The full code is at https://github.com/oysthaga/Project-2. Any help would be highly appreciated.

JohanCarlsen commented 2 years ago

When you declare the variable Amax you are actually also defining it. If Amax starts out as 0, the condition Amax>eps is never satisfied, and the converged variable is immediately set to true.

oysthaga commented 2 years ago

Thank you.