Download as pdf or txt
Download as pdf or txt
You are on page 1of 35

A Virtual Surfer Of The Web

function [A,b,nr] = sysRerrange(A,b)


%
n = size(A,1); % Determine number of rows
nr = zeros(n,1); % Preallocate array new row indices
for i = 1 : n % Each row
% Find location max element in modulus
[~, k] = max(abs(A(i,:)));
nr(k) = i; % Set row i to row k
end
% Abs. of rearrange matrix, A according to elements in nr
B = abs(A(nr,:));

d = diag(B); % Abs diagonal elements of B


B1 = tril(B,-1) + triu(B,1); % Matrix of off diagonals of B
sumoffdiag = sum(B1,2); % Sum
flag = islogical(d - sumoffdiag > 0 );

if flag
disp('System matrix is strictly diagonally dominant')
disp('Jacobi & Gauss Seidel converge for arbitrary initial solution')
else
disp('System matrix is not strictly diagonally dominant')
disp('Convergence not guaranteed Jacobi & Gauss Seidel methods')
disp('Judicial/wise choice of initial solution necessary')
end
A = A(nr,:);
b = b(nr);
def JacobiSolve(A, b, Eps):
import numpy as np
n = len(b)
# First two arbitrary solution guesses
x0, x = np.zeros((n, 1), 'float'), np.ones((n, 1), 'float')
k = 0 # Initialize counter
while np.linalg.norm(x-x0, np.inf) >= Eps: #Convergence test
x0 = x.copy() # Set current/new solution to old/previous
for i in range(n): # In each row, i
x[i] = b[i] # Set b(i) to x(i)
for j in range(n): # In each column j of row i
if j != i: # If not a diagonal element, i not equal to j
x[i] -= A[i][j]*x0[j] # Update x(i)
x[i] /= A[i][i]
print( "k =", k)
print("x = \n", x)
#return (x, k)
function [x, k] = JacobiSolve(A, b, Eps)
n = length(b) ; % Get length of rhs
x0 = zeros(3, 1) ; % First arbitrary solution (old/previous) guess
x = ones(size(x0)) ; % Second arbitrary (current/new) solution guess
k = 0;
while norm(x-x0, inf) >= Eps % Test for convergence using vector inf-norm
x0 = x ; % Set current/new solution to old/previous
for i = 1 : n % In each row, i
x(i) = b(i) ; % Set b(i) to x(i)
for j = 1 : n % In each column j of row i
if j ~= i % If not a diagonal element, i not equal to j
x(i) = x(i) - A(i, j)*x0(j) ; % Update x(i)
end
end % End of column j
x(i) = x(i) / A(i, i) ;
end % End of row i
k = k + 1; % Update counter
end % End of one iteration

JacobiSolve = function(A, b, Eps){


n = length(b) ; # Get length of rhs
x0 = matrix(0,3,1) ; # First arbitrary solution (old/previous) guess
x = matrix(1,3,1) ; # Second arbitrary (current/new) solution guess
k = 0; # Initialize iterative counter
# Main iteration begins here
while (norm(x-x0, 'i') >= Eps){ # Test for convergence
x0 = x ; # Set current/new solution to old/previous
for (i in 1 : n) {# In each row, i
x[i] = b[i] ; # Set b(i) to x(i)
for (j in 1 : n) { # In each column j of row i
if (j != i) {# If not a diagonal element, i not equal to j
x[i] = x[i] - A[i, j]*x0[j] ; # Update x(i)
}
} # End of column j
x[i] = x[i] / A[i, i] ;
} # End of row i
k = k + 1; # Update counter
} # End of one iteration
# Output x and k
return(list(x, k))
}
function [x, Iters] = gaussSeidelSolve(A, b, Eps, x0)
Iters = 1; % Initialize iterative counter
x = GaussSeidel(A, b, x0); % Get 1st improved solution

%% Solving Gauss-Seidel Repetitively/iteratively until convergence


while norm(x-x0, inf) >= Eps % Test for convergence
x0 = x ; % Replace old solution with improved solution
x = GaussSeidel(A, b, x0); % Get next improved solution
Iters = Iters + 1 ; % Update counter
end
%x = x1 ; %
end

%% Main Gauss-Seidel Method for one iteration


function x1 = GaussSeidel(A, b, x0)
x1 = zeros(size(x0)) ; %Preallocate new solution
n = length(b) ; % Get dimension of rhs
for i = 1 : n % For each row, i
x1(i) = b(i) ; % Set b(i) to x(i)
% Loop finds sum of rows of elements of L*x
for j = 1 : i-1
x1(i) = x1(i)-A(i,j)*x1(j) ;
end
% Loop finds sum of rows of elements of U*x
for j = i+1 : n
x1(i) = x1(i) - A(i, j)*x0(j) ;
end
x1(i) = x1(i)/A(i,i) ; % Divide row sum by ith diagonal element
end
end

You might also like