Advertisement
gareins

DN1

Apr 12th, 2015
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.17 KB | None | 0 0
  1. function [X,ctr_j,ctr_g,ctr_s] = iter(A,b,x0,nat,omega)
  2.   %
  3.   % if X==x0 and ctr_j=ctr_g=ctr_s=0
  4.   %   failed
  5.   %
  6.  
  7.   N = length(A);
  8.   j_ctr = 0;
  9.   g_ctr = 0;
  10.   s_ctr = 0;
  11.   X = x0;
  12.  
  13.   % pd detection
  14.   B = abs(A)';
  15.  
  16.   % required, not sufifcient
  17.   d = 2*max(B) < sum(B);
  18.   if(find(d))
  19.     % cannot build p-d
  20.     return;
  21.   end
  22.  
  23.   % sufficient: every max alament in its column
  24.   [_, perm] = max(B);
  25.   if length(unique(perm)) ~= N
  26.     % cannot build p-d
  27.     return;
  28.   end
  29.  
  30.   % tests done -> permutate matrix A for it to be diagonally dominant
  31.   A = A(:, perm);
  32.   Ar = A - eye(N).*A;
  33.   Ad = diag(1./diag(A));
  34.  
  35.   % for jacobi and g-s
  36.   T = -Ad*Ar;
  37.   C = Ad*b;
  38.  
  39.   % jacobi
  40.   ctr_j = 0;
  41.   x = x0;
  42.   while(max(abs(A*x-b)) > nat)
  43.     x = T*x + C;
  44.     ctr_j = ctr_j+1;
  45.   end
  46.   X(perm) = x;
  47.  
  48.   % GS
  49.   ctr_g = 0;
  50.   x = x0;
  51.   while(max(abs(A*x-b)) > nat)
  52.     for j = [1:N]
  53.       x(j) = T(j,:)*x + C(j);
  54.     end
  55.     ctr_g = ctr_g+1;
  56.   end
  57.  
  58.   % sor
  59.   ctr_s = 0;
  60.   x = x0;
  61.   while(max(abs(A*x-b)) > nat)
  62.     for j = [1:N]
  63.       resid = Ad(j,j) * (b(j) - A(j,:)*x);
  64.       x(j) = x(j) + omega*resid;
  65.     end
  66.     ctr_s = ctr_s+1;
  67.   end
  68. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement