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).

    http://img265.imageshack.us/img265/3371/82526305tx0.png [Broken]

    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;

    while r>1e-8
        k = k+1
        if k==1
            p = z;
            r_1 = b;
            z_1 = z;
            beta = (r_1'*z_1)/(r_2'*z_2);
            p = z_1+ beta*p;
        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;

    Last edited by a moderator: May 3, 2017
  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.

    Thank you.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook