Results 1 to 7 of 7

Math Help - mathlab--jacobi iteration method

  1. #1
    Junior Member
    Joined
    Nov 2008
    Posts
    54

    mathlab--jacobi iteration method

    how can ı solve this linear system with Gauss-seidel iteration method using mathlab codes??
    5x - y + z=10
    2x + 8y -z=11
    -x + y + 4z=3
    ı have some codes but how can ı implement on this question actually ı don't know please somebody help me.i am giving the codes;
    function [x,G,c] = gsmp(A,b,n,z)
    %
    % x = gsmp(A,b,n,z)
    %
    % Gauss-Seidel iteration on system A*x = b with printing
    % using matrix multiplication (not optimal)
    % n -- number of iterations
    % z -- initial vector (default 0)
    %
    % x -- final iterate
    % G -- Gauss-Seidel matrix
    % c -- Gauss-Seidel vector
    %

    if nargin <=3, z=0*b; end

    LD = tril(A);
    G = -LD\triu(A,1);
    c = LD\b;
    x=z;
    for i = 1:n
    x = G*x + c;
    fprintf(1,'%3d ',i)
    fprintf(1,'%5.5f ',x')
    fprintf(1,'\n')
    end
    Last edited by sah_mat; December 5th 2008 at 11:51 AM. Reason: not good looking
    Follow Math Help Forum on Facebook and Google+

  2. #2
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by sah_mat View Post
    how can ı solve this linear system with Gauss-seidel iteration method using mathlab codes??
    5x - y + z=10
    2x + 8y -z=11
    -x + y + 4z=3
    ı have some codes but how can ı implement on this question actually ı don't know please somebody help me.i am giving the codes;
    function [x,G,c] = gsmp(A,b,n,z)
    %
    % x = gsmp(A,b,n,z)
    %
    % Gauss-Seidel iteration on system A*x = b with printing
    % using matrix multiplication (not optimal)
    % n -- number of iterations
    % z -- initial vector (default 0)
    %
    % x -- final iterate
    % G -- Gauss-Seidel matrix
    % c -- Gauss-Seidel vector
    %

    if nargin <=3, z=0*b; end

    LD = tril(A);
    G = -LD\triu(A,1);
    c = LD\b;
    x=z;
    for i = 1:n
    x = G*x + c;
    fprintf(1,'%3d ',i)
    fprintf(1,'%5.5f ',x')
    fprintf(1,'\n')
    end

    Try this:

    Code:
    function [x] = gsmp(A,b,n,z)
    %
    % x = gsmp(A,b,n,z)
    % 
    % Gauss-Seidel iteration on system A*x = b 
    %
    % n -- number of iterations
    % z -- initial vector (default 0)
    %
    % x -- final iterate
     
     
     
    if nargin <=3, z=0*b; end
     
     
    L = tril(A);     %lower triangular part of A
    U = triu(A);   %upper triangular part of A
    D = diag(diag(A));  %diag matrix with same diag as A
     
    x=z;
     
    for i = 1:n
      x =(D+L)\(b-U*x);
      disp([i,x'])
    end
    with calling code:

    Code:
    A=[5,-1,1;2,8,-1;-1,1,4] 
    b=[10;11;3]
    x=gsmp(A,b,20,[1;1;1]);
    Reference

    CB
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Junior Member
    Joined
    Nov 2008
    Posts
    54
    first ı want to thank you captain,i have been searching this homework for about a week and recently i tried your code but it gives me an error like this ı don't know what to do;
    Code:
    ??? function [x] = gsmp(A,b,n,z)
        |
    Error: Function definitions are not permitted at the prompt or in scripts.
    but ı found another solution,but it is possible that my teacher won't accept it becouse she wants me to write a script and ı really going to be crazy ı have more exams to study whatever ı am givin my solution her check it please;
    Code:
    >> M = [5 -5 1; 2 8 -1; -1 1 4];
    b = [10 11 3]';
    D = diag( M ).^-1
    
    D =
    
        0.2000
        0.1250
        0.2500
    
    >> Moff = M - diag( diag( M ) )
    
    Moff =
    
         0    -5     1
         2     0    -1
        -1     1     0
    
    >> x = [0, 0, 0]'
    
    x =
    
         0
         0
         0
    
    >> for i = 1:100
        xold = x;
    
        for j = 1:3
          x(j) = D(j)*(b(j) - Moff(j, :)*x);
        end
    
        if norm( x - xold ) < 0.001 
           break;
        end
    end
    >> i
    
    i =
    
         9
    
    >> x
    
    x =
    
        2.6284
        0.8667
        1.1904
    Follow Math Help Forum on Facebook and Google+

  4. #4
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by sah_mat View Post
    first ı want to thank you captain,i have been searching this homework for about a week and recently i tried your code but it gives me an error like this ı don't know what to do;
    Code:
    ??? function [x] = gsmp(A,b,n,z)
        |
    Error: Function definitions are not permitted at the prompt or in scripts.
    You put the function definition in a file called gsmp.m on the search path, or add the directory with it to the search path.

    Also if a script file is required you put:

    Code:
    A=[5,-1,1;2,8,-1;-1,1,4] 
    b=[10;11;3]
    x=gsmp(A,b,20,[1;1;1])
    in a file in the same directory and call it something like script.m and invoke it at the command prompt with:

    Code:
    >>script
    CB
    Follow Math Help Forum on Facebook and Google+

  5. #5
    Junior Member
    Joined
    Nov 2008
    Posts
    54
    ı appreciate you captain blac for your helps ı tried your last advice and it realyy works,then ı send it to my teacher she said 'this m.file is too professional probably u did not write it ı want a simple one which u can write'' so ı want u too ask a more question captain ı wrote a solution code at my previous message can we write a gsmp.m file with using the codes in that solution if u help me ı really be glad ı have to study my other exams ı think i am in trouble with the mathlab.THANKS for your helps captain black.
    Follow Math Help Forum on Facebook and Google+

  6. #6
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by sah_mat View Post
    ı appreciate you captain blac for your helps ı tried your last advice and it realyy works,then ı send it to my teacher she said 'this m.file is too professional probably u did not write it ı want a simple one which u can write'' so ı want u too ask a more question captain ı wrote a solution code at my previous message can we write a gsmp.m file with using the codes in that solution if u help me ı really be glad ı have to study my other exams ı think i am in trouble with the mathlab.THANKS for your helps captain black.
    Put this in a script file:

    Code:
    M = [5 -5 1; 2 8 -1; -1 1 4];
    b = [10 11 3]';
    D = diag( M ).^-1
    
    Moff = M - diag( diag( M ) )
    
    x = [0, 0, 0]'
    
    for i = 1:100
        xold = x;
    
        for j = 1:3
          x(j) = D(j)*(b(j) - Moff(j, :)*x);
        end
    
        if norm( x - xold ) < 0.001 
           break;
        end
    end
    
    disp( i)
    
    disp( x)
    CB
    Follow Math Help Forum on Facebook and Google+

  7. #7
    Junior Member
    Joined
    Nov 2008
    Posts
    54
    it is really working thanks a lot CB.ı think ı can implement the same steps for solving with gauss-seidel method.
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. Jacobi Method rearrangement of linear system (help!)
    Posted in the Advanced Applied Math Forum
    Replies: 1
    Last Post: March 23rd 2010, 03:20 AM
  2. Replies: 3
    Last Post: March 6th 2010, 04:40 AM
  3. MAPLE - Jacobi & Gauss-Seidel Iteration
    Posted in the Math Software Forum
    Replies: 0
    Last Post: December 12th 2008, 08:07 AM
  4. Mathlab help Bisection Method
    Posted in the Math Software Forum
    Replies: 4
    Last Post: June 16th 2008, 07:05 AM
  5. Replies: 3
    Last Post: November 3rd 2007, 02:43 PM

Search Tags


/mathhelpforum @mathhelpforum