-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathJacobi.py
37 lines (29 loc) · 816 Bytes
/
Jacobi.py
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
import numpy as np
ITERATION_LIMIT = 100
# initialize the matrix
A = np.array([[-1.,2.,0.],[0.,2.,-5.],[1.,1.,1.]])
# initialize the RHS vector
b = np.array([1., 0., 50.])
# prints the system
print("System:")
for i in range(A.shape[0]):
row = ["{}*x{}".format(A[i, j], j + 1) for j in range(A.shape[1])]
print(" + ".join(row), "=", b[i])
print()
#x = np.zeros_like(b)
x = [29.,15.,6.]
for it_count in range(ITERATION_LIMIT):
print("Current solution:", x)
x_new = np.zeros_like(x)
for i in range(A.shape[0]):
s1 = np.dot(A[i, :i], x[:i])
s2 = np.dot(A[i, i + 1:], x[i + 1:])
x_new[i] = (b[i] - s1 - s2) / A[i, i]
if np.allclose(x, x_new, atol=1e-10):
break
x = x_new
print("Solution:")
print(x)
error = np.dot(A, x) - b
print("Error:")
print(error)