Professional Documents
Culture Documents
Num 2
Num 2
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