1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: Matlab Cg algorithm

  1. Dec 27, 2008 #1
    1. The problem statement, all variables and given/known data
    http://img184.imageshack.us/img184/8454/26645063th6.png [Broken]


    2. Relevant equations

    The code
    Code (Text):

    u = [ 0 ;zeros(n-2,1); 0]; % this has to be zero.
    k = 0;
    r = b;

    %algoritme
    while k<100
        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
     

    3. The attempt at a solution
    Somewhere is a mistake in the code (aside the fact that A and b aren't shown), because it doesn't converge. But I can't find it. Can someone help me?
     
    Last edited by a moderator: May 3, 2017
  2. jcsd
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

Can you offer guidance or do you also need help?
Draft saved Draft deleted