Skip to content

Instantly share code, notes, and snippets.

@lettergram
Created March 19, 2015 00:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lettergram/128a515b2818585979d7 to your computer and use it in GitHub Desktop.
Save lettergram/128a515b2818585979d7 to your computer and use it in GitHub Desktop.
import numpy as np
from scipy.linalg import solve
def jacobi(A, b, x, n):
D = np.diag(A)
R = A - np.diagflat(D)
for i in range(n):
x = (b - np.dot(R,x))/ D
return x
'''___Main___'''
A = np.array([[4.0, -2.0, 1.0], [1.0, -3.0, 2.0], [-1.0, 2.0, 6.0]])
b = [1.0, 2.0, 3.0]
x = [1.0, 1.0, 1.0]
n = 25
x = jacobi(A, b, x, n)
print solve(A, b)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment