# NLA-5.2

## 2009 days 전, jhlee2chn 작성

Algorithm 5.2.1 (Conjugate Gradient Method for solving LSE with p.d. matrix)

A = matrix(RDF,[[16.0, 4.0, 8.0, 4.0], [4.0, 10.0, 8.0, 4.0], [8.0, 8.0, 12.0, 10.0], [4.0, 4.0, 10.0, 12.0]]) print "A=" print A b = vector(RDF,[32.0, 26.0, 38.0, 30.0]) print "b=" print b # Conjugate Gradient x = 10*random_vector(RDF, 4) tol = 10^(-8);r = b-A*x;k = 0 while r.norm(Infinity) > tol: k = k + 1 if k == 1: p = r else: bet=r.inner_product(r)/r_old.inner_product(r_old) p = r + bet*p q = A*p alp = r.inner_product(r)/(p.inner_product(q)) x = x + alp*p;r_old=r;r = r-alp*q print "x=", x print "||r||=", r.norm(Infinity) print "exact solution=", A.solve_right(b) print k
 A= [16.0 4.0 8.0 4.0] [ 4.0 10.0 8.0 4.0] [ 8.0 8.0 12.0 10.0] [ 4.0 4.0 10.0 12.0] b= (32.0, 26.0, 38.0, 30.0) x= (1.0, 1.0, 1.0, 1.0) ||r||= 4.63185045874e-13 exact solution= (1.0, 1.0, 1.0, 1.0) 4 A= [16.0 4.0 8.0 4.0] [ 4.0 10.0 8.0 4.0] [ 8.0 8.0 12.0 10.0] [ 4.0 4.0 10.0 12.0] b= (32.0, 26.0, 38.0, 30.0) x= (1.0, 1.0, 1.0, 1.0) ||r||= 4.63185045874e-13 exact solution= (1.0, 1.0, 1.0, 1.0) 4

Algorithm 5.1.1  (Steepest Descent Method for solving LSE with p.d. matrix)

A = matrix(RDF,[[16.0, 4.0, 8.0, 4.0], [4.0, 10.0, 8.0, 4.0], [8.0, 8.0, 12.0, 10.0], [4.0, 4.0, 10.0, 12.0]]) print "A=" print A b = vector(RDF,[32.0, 26.0, 38.0, 30.0]) print "b=" print b # Steepest Descent x = 10*random_vector(RDF, 4) tol = 10^(-8) r = b-A*x k = 0 while r.norm(Infinity) > tol: k = k+1 q = A*r alp = r.inner_product(r)/(r.inner_product(q)) x = x + alp*r r=b-A*x print "x=", x print "||r||=", r.norm(Infinity) print "exact solution=", A.solve_right(b) print k
 A= [16.0 4.0 8.0 4.0] [ 4.0 10.0 8.0 4.0] [ 8.0 8.0 12.0 10.0] [ 4.0 4.0 10.0 12.0] b= (32.0, 26.0, 38.0, 30.0) x= (0.999999990799, 0.999999981379, 1.00000003886, 0.999999976071) ||r||= 9.81033920766e-09 exact solution= (1.0, 1.0, 1.0, 1.0) 1064 A= [16.0 4.0 8.0 4.0] [ 4.0 10.0 8.0 4.0] [ 8.0 8.0 12.0 10.0] [ 4.0 4.0 10.0 12.0] b= (32.0, 26.0, 38.0, 30.0) x= (0.999999990799, 0.999999981379, 1.00000003886, 0.999999976071) ||r||= 9.81033920766e-09 exact solution= (1.0, 1.0, 1.0, 1.0) 1064