The code seems to be correct. For example, the following test works for me (both linalg.solve and sweepestDescent give a great answer):
import numpy as np n = 100 A = np.random.random(size=(n,n)) + 10 * np.eye(n) print(np.linalg.eig(A)[0]) b = np.random.random(size=(n,1)) x, xs, r = steepestDescent(A,b, numIter=50) print(x - np.linalg.solve(A,b))
The problem is math. This algorithm is guaranteed to converge to the correct solution if A is a positive definite matrix. By adding a decimal identity matrix to a random matrix, we increase the probability that all eigenvalues ββare positive
If you check with large random matrices (e.g. A = random.random(size=(n,n)) , you will almost certainly have a negative eigenvalue and the algorithm will not converge.
source share