-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2.5_CP1.py
More file actions
56 lines (44 loc) · 1.37 KB
/
Copy path2.5_CP1.py
File metadata and controls
56 lines (44 loc) · 1.37 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
47
48
49
50
51
52
53
54
55
56
# Use the Jacobi Method to solve the spare system within six correct decimal places
# (forward error in the infinity norm) for n = 100 and n = 100000. The correct solution is [1,...,1].
# Report the number of steps needed and the backward error. The system;
# Special thanks to riverlike14 on GitHub.
# https://github.com/riverlike14/Numerical_Analysis/blob/master/Ch_02/Ch_2_5.ipynb
import numpy as np
def jacobi(A, b, num_iter, initial_guess=0):
n = len(A)
D = np.diag(A)
R = A - np.diag(D)
x = initial_guess * np.ones(n)
for j in range(num_iter):
x = (b - R.dot(x)) / D
return x
def sparseMatrixSetup(n):
A = np.zeros((n, n))
for i in range(n):
A[i, i] = 3
if i < n - 1:
A[i, i + 1] = -1
A[i + 1, i] = -1
return A
def bSetup(A):
(n, m) = np.shape(A)
b = np.ones((n, 1))
b[-1], b[0] = 2, 2
return b
def main(n):
A = sparseMatrixSetup(n)
b = bSetup(A)
x_true = np.ones(n) # det står i oppgaven
j = 0
guess = np.zeros(n)
while True:
guess = jacobi(A, b, 1, guess)
j += 1
forward_error = abs(guess - x_true).max()
if forward_error < 5e-7:
break
backward_error = abs(A.dot(guess) - b).max()
print("Number of iterations:", j)
print("Backward error:", backward_error)
if __name__ == '__main__':
main(100)