Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Matlab Cg algorithm

  1. Dec 27, 2008 #1
    I have to write a code in matlab for the algoritm below (preconditioned conjugate gradient).


    [​IMG]


    But in the code I've written there is a mistake (aside from the fact that I left out A and b) since it isn't converging.
    Code (Text):
     
    u = [ 0 ;zeros(n-2,1); 0];

    k = 0;
    r = b;

    %algorithm
    while r>1e-8
        z=inv(M)*r;
        k = k+1
       
        if k==1
            p = z;
            r_1 = b;
            z_1 = z;
               
        else
            beta = (r_1'*z_1)/(r_2'*z_2);
            p = z_1+ beta*p;
        end
       
        alpha = (r_1'*z_1)/(p'*A*p);
        u = u + alpha*p;
        r= r_1-alpha*A*p;

    r_2 = r_1;
    r_1 = r;

    z_2 = z_1;
    z_1 = z;

    end
     
  2. jcsd
  3. Dec 27, 2008 #2
    You have correctly posted your question under
    homework and coursework questions/Engineering Comp.Science & Technology.

    However, if you double-post here, in order to avoid duplicated work of responses, it would be nice if you quote the original post.
    https://www.physicsforums.com/showthread.php?t=281723

    Thank you.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Matlab Cg algorithm
  1. Ask an algorithm (Replies: 3)

  2. Knuth Algorithm (Replies: 3)

Loading...