diff --git a/hw1-conjugate-gradient/conjugate_gradient.py b/hw1-conjugate-gradient/conjugate_gradient.py index a22b22b1..3490f337 100644 --- a/hw1-conjugate-gradient/conjugate_gradient.py +++ b/hw1-conjugate-gradient/conjugate_gradient.py @@ -1 +1,26 @@ +#THIS PROGRAM RUNS THE CONJUGATE GRADIENT ALGORITHM + +# Import Libraries import numpy as np +import scipy as sp +import matplotlib.pyplt as plt + +# Input Arguments +A = [[2.0, 3.0, 5.0], [2.0, 3.0 , 6.0], [3.0, 6.0, 9.0]] +b = [2.0, 3.0, 5.0] + +float tolerance = 0.0005 + +# Define the function conjgrad +# DOES : CG algorithm +def conjgrad(A, b, x, tolerance): + # Initialization + int k = 0 + r = b - np.matmul(A, x) + p = r + + while(np.sqrt(np.sum(r**2)>tolerance): + k=k+1 + + return(r); +