-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGauss_Jacobi_Method.py
More file actions
46 lines (35 loc) · 1.09 KB
/
Gauss_Jacobi_Method.py
File metadata and controls
46 lines (35 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import numpy as np
import copy
def gauss_jacobi(A, b):
n = len(A)
x0 = np.array([0.0]*n)
x = copy.deepcopy(x0)
tolerence = 0.001
k = 1
while (k <= 100):
for i in range(n):
sum_a = 0.0
for j in range(n):
if j != i:
sum_a += float(A[i][j]*x0[j])
x[i] = float((b[i]-sum_a)/A[i][i])
diff = float(np.linalg.norm(x-x0)/np.linalg.norm(x))
print(k, 'th, ieration', x)
if (diff < tolerence):
print('Iterations completed!')
print(f'Solutions of the system of the equations are : {x}')
break
k = k+1
x0 = copy.deepcopy(x)
if (k >= 100):
print('\nGauss-Jacobi Method fails :(')
else:
print('Gauss-Jacobi Method is successful :)')
m = int(input('How many unknowns are there : '))
A = [0]*m
for i in range(len(A)):
A[i] = list(
map(float, input(f"Enter the coefficients of equation--{i+1} : ").split(" ")))
b = [0]*m
b = list(map(float, input('Enter the solution array : ').split(" ")))
gauss_jacobi(A, b)