Professional Documents
Culture Documents
Numerical Computation
Submitted By:
Abdul Ahad Naeem
FA20-BEE-003
Submitted to:
Dr. Iftikhar Ahmed Shb
Code:
import numpy as np
import matplotlib.pyplot as plt
def is_diagonally_dominant(A):
n = A.shape[0]
for i in range(n):
row_sum = np.sum(np.abs(A[i, :])) - np.abs(A[i, i])
if np.abs(A[i, i]) <= row_sum:
return False
return True
if not is_diagonally_dominant(A):
print("The coefficient matrix is not diagonally dominant. The method may no
return None, error_list
for _ in range(max_iterations):
for i in range(n):
sigma = np.dot(A[i, :n], x) - A[i, i] * x[i]
x_new[i] = (b[i] - sigma) / A[i, i]
error = np.linalg.norm(x_new - x)
error_list.append(error)
np.copy(x_new) return x,
error_list
A = np.array([[10, 2, 1],
[1, 7, 1],
[2, 3, 10]], dtype=float)
b = np.array([7, -8, 6], dtype=float)
plt.yscale('log') plt.xlabel("Iteration")
plt.ylabel("Error") plt.title("Convergence of Gauss-Jacobi
Method") plt.grid() plt.show()
Solution: [ 0.89384082 -1.38718367 0.83740408] Number
of iterations: 4