mihainan

Metode numerice

Mar 27th, 2014
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 0.43 KB | None | 0 0
  1. function x = solJacobi(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.             s=0;
  10.             for j = 1 : n
  11.                 if j==1
  12.                     ok = 0;
  13.                 endif
  14.                 if ok == 1
  15.                     s=s+A(i,j)*x(j);
  16.                 endif
  17.             endfor
  18.             xc(i) = (b(i) - s)/A(i,i);
  19.             if xc - xp < tol
  20.                 W = 0;
  21.             endif
  22.             xp = xc;
  23.         endfor
  24.         if W = 100
  25.             break;
  26.         endif
  27.     endfor
  28.     x = xc;
  29. endfunction
Advertisement
Add Comment
Please, Sign In to add comment