def problem18Verify(A2, B2): ex2 = AxClass(A2) x = B2.copy() x, i = conjGrad(ex2,x,B2) print("\nVerification for problem 18:") print("\nNumber of iterations =", i) print("\nThe solution is:\n", x)
def problem19Verify(A3, B3): ex2 = AxClass(A3) x = B3.copy() x, i = conjGrad(ex2,x,B3) print("\nVerification for problem 19:") print("\nNumber of iterations =", i) print("\nThe solution is:\n", x)
def problem19(): b = np.array([0.0, 0.0, 100.0, 0.0, 0.0, 100.0, 200.0, 200.0, 300.0],dtype=float) n = 9 x = np.zeros(n) x, numIter = conjGrad(Ax2, x, b) print("\nNumber of iterations =",numIter) print("\nThe solution for 19 is:\n",x) input("\nPress return to exit")
def problem18(): n = eval(input("Number of equations ==> ")) b = np.zeros(n) b[n-1] = 100.00 x = np.zeros(n) x, numIter = conjGrad(Ax,x,b) print("\nNumber of iterations =", numIter) print("\nThe solution for 18 is:\n", x) input("\nPress return to exit")
# fill in the other 8 rows of Ax (2 points per row of Ax for # a total of 18 points) Ax[1] = v[0] - 4.0*v[1] + v[2] + v[4] Ax[2] = v[1] - 4.0*v[2] + v[5] Ax[3] = v[0] - 4.0*v[3] + v[4] + v[6] Ax[4] = v[1] + v[3] - 4.0*v[4] + v[5] + v[7] Ax[5] = v[2] + v[4] - 4.0*v[5] + v[8] Ax[6] = v[3] - 4.0*v[6] + v[7] Ax[7] = v[4] + v[6] - 4.0*v[7] + v[8] Ax[8] = v[5] + v[7] - 4.0*v[8] return Ax b = array([0,0,100,0,0,100,200,200,300])*(-1.0) x = zeros((9))*1.0 tol = 1e-06 s1,numIter = conjGrad(Ax, x, b, tol) # 5 points for correct call here print("\nThe solution is:\n",s1) print("\nNumber of iterations =",numIter, "using Tol: ", 1e-06) print("\n CG Convergence Test") print("Iterations Tolerance") # # Use the loop below to call conjGrad with various tolerances, # compare the number of iterations. # for tol in [1e-02, 1e-04, 1e-06, 1e-08, 1e-10, 1e-12, 1e-14, 1e-16]: x = zeros((9),dtype=float) s2,numIter = conjGrad(Ax, x, b, tol) # 5 points for correct call here print('%6d %8.1e'%(numIter,tol)) print("\nError between vectors obtained with tol=1e-06 and tol=1e-16: ", linalg.norm(s1-s2), "\n")
[0., 1., 0., 1., -4., 1., 0., 1., 0.], [0., 0., 1., 0., 1., -4., 0., 0., 1.], [0., 0., 0., 1., 0., 0., -4., 1., 0.], [0., 0., 0., 0., 1., 0., 1., -4., 1.], [0., 0., 0., 0., 0., 1., 0., 1., -4.]] Ax = array(Ax) Ax = dot(Ax, v) return Ax # b and x are the right-hand side and solution vectors from problem 19 b = array([0, 0, 100, 0, 0, 100, 200, 200, 300]) * (-1.0) x = zeros((9)) * 1.0 tol = 1e-06 # Solves the equation using a tolerance of 1e-6 s1, numIter = conjGrad(Ax, x, b, tol=tol) print("\nThe solution is:\n", s1) print("\nNumber of iterations =", numIter, "using Tol: ", 1e-06) print("\n CG Convergence Test") print("Iterations Tolerance") # # Use the loop below to call conjGrad with various tolerances, # compare the number of iterations. # for tol in [1e-02, 1e-04, 1e-06, 1e-08, 1e-10, 1e-12, 1e-14, 1e-16]: x = zeros((9), dtype=float) s2, numIter = conjGrad(Ax, x, b, tol=tol) print('%6d %8.1e' % (numIter, tol)) # Prints the error measure between the solutions with tolerances of
#!/usr/bin/python ## example2_18 import numpy from conjGrad import * def Ax(v): n = len(v) Ax = numpy.zeros((n), dtype=float) Ax[0] = 2.0 * v[0] - v[1] + v[n - 1] Ax[1:n - 1] = -v[0:n - 2] + 2.0 * v[1:n - 1] - v[2:n] Ax[n - 1] = -v[n - 2] + 2.0 * v[n - 1] + v[0] return Ax n = eval(raw_input("Number of equations ==> ")) b = numpy.zeros((n), dtype=float) b[n - 1] = 1.0 x = numpy.zeros((n), dtype=float) x, numIter = conjGrad(Ax, x, b) print "\nThe solution is:\n", x print "\nNumber of iterations =", numIter
#!/usr/bin/python3 ## example2_18 on p. 97 of Kiusalaas, 3rd Ed. from numpy import zeros, float64, sqrt from conjGrad import * def Ax(v): # define Matrix-vector multiplication function n = len(v) Ax = zeros((n), dtype=float64) Ax[0] = 2.0 * v[0] - v[1] + v[n - 1] Ax[1:n - 1] = -v[0:n - 2] + 2.0 * v[1:n - 1] - v[2:n] Ax[n - 1] = -v[n - 2] + 2.0 * v[n - 1] + v[0] return Ax n = eval(input("Number of equations ==> ")) b = zeros((n), dtype=float64) b[n - 1] = 1.0 x = zeros((n), dtype=float64) x, numIter = conjGrad(Ax, x, b, 1.0e-2) print("\nThe solution is:\n", x) print("\nNumber of iterations =", numIter) input("\nPress return to exit")
[1,0,0,-4,1,0,1,0,0], [0,1,0,1,-4,1,0,1,0], [0,0,1,0,1,-4,0,0,1], [0,0,0,1,0,0,-4,1,0], [0,0,0,0,1,0,1,-4,1], [0,0,0,0,0,1,0,1,-4]]) Ax = dot(A, v) #matrix mult of A*v return Ax b = array([0,0,100,0,0,100,200,200,300])*(-1.0) #solution vector x = zeros((9))*1.0 tol = 1e-06 #set the tolerance s1,numIter = conjGrad(Ax, x, b, tol) print("\nThe solution is:\n",s1) print("\nNumber of iterations =",numIter, "using Tol: ", 1e-06) print("\n CG Convergence Test") print("Iterations Tolerance") # # Use the loop below to call conjGrad with various tolerances, # compare the number of iterations. # for tol in [1e-02, 1e-04, 1e-06, 1e-08, 1e-10, 1e-12, 1e-14, 1e-16]: x = zeros((9),dtype=float) s2,numIter = conjGrad(Ax, x, b, tol) print('%6d %8.1e'%(numIter,tol)) print("\nError between vectors obtained with tol=1e-06 and tol=1e-16: ", linalg.norm(s1-s2), "\n")
Ax[2] = -4.0 * v[2] + 1.0 * (v[1] + v[5]) Ax[3] = -4.0 * v[3] + 1.0 * (v[0] + v[4] + v[6]) Ax[4] = -4.0 * v[4] + 1.0 * (v[1] + v[3] + v[5] + v[7]) Ax[5] = -4.0 * v[5] + 1.0 * (v[2] + v[4] + v[8]) Ax[6] = -4.0 * v[6] + 1.0 * (v[3] + v[7]) Ax[7] = -4.0 * v[7] + 1.0 * (v[4] + v[6] + v[8]) Ax[8] = -4.0 * v[8] + 1.0 * (v[5] + v[7]) return Ax b = array([0,0,100,0,0,100,200,200,300])*(-1.0) x = zeros((9))*1.0 tol = 1e-06 #Conjugate Gradient Method being called with the parameters Ax as the function #x being the temperature variables and b being the - solution vector, and a 1e-06 tolerance. s1,numIter = conjGrad(Ax, x, b, 1e-06) print("\nThe solution is:\n",s1) print("\nNumber of iterations =",numIter, "using Tol: ", 1e-06) print("\n CG Convergence Test") print("Iterations Tolerance") # # Use the loop below to call conjGrad with various tolerances, # compare the number of iterations. # for tol in [1e-02, 1e-04, 1e-06, 1e-08, 1e-10, 1e-12, 1e-14, 1e-16]: x = zeros((9),dtype=float) #calling the conjGrad function with a varying tolerance and printing out the new solutions. s2,numIter = conjGrad(Ax, x, b, tol) print('%6d %8.1e'%(numIter,tol))
#!/usr/bin/python ## example2_18 from numarray import zeros,Float64,sqrt from conjGrad import * def Ax(v): n = len(v) Ax = zeros((n),type=Float64) Ax[0] = 2.0*v[0] - v[1]+v[n-1] Ax[1:n-1] = -v[0:n-2] + 2.0*v[1:n-1] -v [2:n] Ax[n-1] = -v[n-2] + 2.0*v[n-1] + v[0] return Ax n = eval(raw_input("Number of equations ==> ")) b = zeros((n),type=Float64) b[n-1] = 1.0 x = zeros((n),type=Float64) x,numIter = conjGrad(Ax,x,b) print "\nThe solution is:\n",x print "\nNumber of iterations =",numIter raw_input("\nPress return to exit")