You are on page 1of 4

HAMZA AHMED WAJEEH (12349) NC LAB (111678) TASK 8

Gauss Jacobi
S# Functions Tolerance No of Root value
Iterations
1 0.01 10 X = 1.4483854166666668
4x + y - z = 5 Y = -0.8390925925925927
-x + 3y + z = -4 Z = -0.04187222222222218
2x + 2y + 5z = 1 0.001 10 X= 1.4476423842592592
Y= -0.8355647878086421
Z= -0.045022662037037045
0.0001 20 X= 1.447775397810571
Y= -0.8358400982510288
Z= -0.0447569372685185
2 10x – y + 0 = 9 0.01 15 X= 0.99555
-x + 10y - 2z = 7 Y= 0.9572499999999999
0 – 2y + 10z = 6 Z= 0.7910999999999999
0.001 18 X= 0.995725
Y= 0.957775
Z= 0.79145
0.0001 22 X= 0.9957862499999999
Y= 0.9578887500000001
Z= 0.7915725
3 3x - y + z = 1 0.01 17 X= 0.6492954324586978
3x + 6y + 2z = 7 Y= 0.8697202425943923
3x + 3y + 7z = 4 Z= -0.0775506347663782
0.001 22 X= 0.6491148261065696
Y= 0.8683244182746704
Z= -0.07905571660691064
0.0001 35 X= 0.6491212308184214
Y= 0.8684041552884851
Z= -0.07896623033266247
CODE:
import numpy as np
A = np.array([[3, -1, 1], [3, 6, 2], [3, 3, 7]])
b = np.array([1, 7, 4])
x0 = float(input("Enter the initial guess: "))
max_iter = int(input("Enter maximum iterations: "))
tol = float(input("Enter tolerance level value: "))
def gauss_jacobi(A, b, x0, max_iter, tol):
n = len(A)
x = np.full(n, x0)
for k in range(max_iter):
x_prev = x.copy()
for i in range(n):
sigma = 0
for j in range(n):
if j != i:
sigma += A[i, j] * x_prev[j]
x[i] = (b[i] - sigma) / A[i, i]
HAMZA AHMED WAJEEH (12349) NC LAB (111678) TASK 8
if np.linalg.norm(x - x_prev) < tol:
return x
return x
x = gauss_jacobi(A, b, x0, max_iter, tol)
print("The solution vector is:", x)
print("x = ", x[0])
print("y = ", x[1])
print("z = ", x[2])

OUTPUT:
HAMZA AHMED WAJEEH (12349) NC LAB (111678) TASK 8
Gauss Siedel’s
S# Functions Tolerance No of Root value
Iterations
1 0.01 10 X = 1.445943287037037
4x + y - z = 5 Y = -0.8354880401234568
-x + 3y + z = -4 Z = -0.04418209876543209
2x + 2y + 5z = 1 0.001 13 X= 1.44781634998714
Y= -0.8358173037765774
Z= -0.04479961848422502
0.0001 20 X= 1.4477544213230882
Y= -0.835815320064229
Z= -0.04477564050354368
2 10x – y + 0 = 9 0.01 15 X= 0.9957475
-x + 10y - 2z = 7 Y= 0.9578737499999999
0 – 2y + 10z = 6 Z= 0.79157475
0.001 25 X= 0.9957475
Y= 0.9578737499999999
Z= 0.79157475
0.0001 29 X= 0.9957873749999999
Y= 0.9578936875
Z= 0.7915787375
3 3x - y + z = 1 0.01 17 X= 0.6484540906309614
3x + 6y + 2z = 7 Y= 0.8685707087067631
3x + 3y + 7z = 4 Z= -0.0787249140018819
0.001 22 X= 0.6490985409028817
Y= 0.8683590342158531
Z= -0.07891038933660065
0.0001 35 X= 0.6490898078508179
Y= 0.8684252258534579
Z= -0.07893501444468967
CODE:
import numpy as np
A = np.array([[3, -1, 1], [3, 6, 2], [3, 3, 7]])
b = np.array([1, 7, 4])
x0 = float(input("Enter the initial guess: "))
max_iter = int(input("Enter maximum iterations: "))
tol = float(input("Enter tolerance level value: "))
def gauss_seidel(A, b, x0, max_iter, tol):
n = len(A)
x = np.full(n, x0)
for k in range(max_iter):
x_prev = x.copy()
for i in range(n):
sigma = 0
for j in range(n):
if j != i:
sigma += A[i, j] * x[j]
x[i] = (b[i] - sigma) / A[i, i]
HAMZA AHMED WAJEEH (12349) NC LAB (111678) TASK 8
if np.linalg.norm(x - x_prev) < tol:
return x
return x
x = gauss_seidel(A, b, x0, max_iter, tol)
print("The solution vector is:", x)
print("x = ", x[0])
print("y = ", x[1])
print("z = ", x[2])

OUTPUT:

You might also like