-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjacobi.py
More file actions
36 lines (28 loc) · 800 Bytes
/
jacobi.py
File metadata and controls
36 lines (28 loc) · 800 Bytes
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
# Author: Alex Gezerlis
# Numerical Methods in Physics with Python (CUP, 2020)
from triang import testcreate, testsolve
import numpy as np
def termcrit(xolds,xnews):
errs = np.abs((xnews - xolds)/xnews)
return np.sum(errs)
def jacobi(A,bs,kmax=50,tol=1.e-6):
n = bs.size
xnews = np.zeros(n)
for k in range(1,kmax):
xs = np.copy(xnews)
for i in range(n):
slt = A[i,:i]@xs[:i]
sgt = A[i,i+1:]@xs[i+1:]
xnews[i] = (bs[i] - slt - sgt)/A[i,i]
err = termcrit(xs,xnews)
print(k, xnews, err)
if err < tol:
break
else:
xnews = None
return xnews
if __name__ == '__main__':
n = 4; val = 21
A, bs = testcreate(n,val)
A += val*np.identity(n)
testsolve(jacobi,A,bs)