mihainan

Gauss- Seidel

Mar 27th, 2014
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 0.46 KB | None | 0 0
  1. function x = GaussSeidel(A,b,x0,tol,maxiter)
  2.     [n n] = size(A)
  3.     xc = zeros(n,1);
  4.     xp = x0;
  5.     W = 1;
  6.     for k = 1 : maxiter
  7.         for i = 1 : n
  8.             ok = 1;
  9.             s1=0;
  10.             s2=0;
  11.                 for j = 1: i-1
  12.                     s1 = s1 + A(i,j)*xc(j);
  13.                 endfor
  14.                 for j = i+1 : n
  15.                     s2 = s2 + A(i,j)*xp(j);
  16.                 endfor
  17.  
  18.             xc(i) = (b(i) - s1 - s2)/A(i,i);
  19.             if xc - xp < tol
  20.                 W = 0;
  21.             endif
  22.             xp = xc;
  23.         endfor
  24.         if W = 0
  25.             break;
  26.         endif
  27.     endfor
  28.     x = xc;
  29. endfunction
Advertisement
Add Comment
Please, Sign In to add comment