math4610

Routine Name: jacobiIteration

Author: David Merkley

Language: Python

Description/Purpose: Performs jacobi iteration

Input: a vector and matrix and starting point

Output: the operation

Implementation/Code:

def JacobiIteration(A, b, x0, tol=1e-10, maxiter=1e3):
    n = len(b)
    tol2 = tol * tol
    err2 = 2*tol
    it = 0
    while err2 > tol2 and it < maxiter:
        r = residual(A, b, x0)
        x1 = [x0[i] + r[i] / A[i][i] for i in range(n)]
        err2 = l22err(x1, x0)
        x0 = x1
        it += 1
    if it == maxiter:
        print("Max Iter")
    return x1