% Linear Algebra: Theory and Applications
% Ward Cheney & David Kincaid
% JBPub.com (c) 2008 
% File: exp8-3-4.m
% Example 8.3.4
%
format long
A=[2 -0.3 -0.2; -0.3 2 -0.1; -0.2 -0.1 2]
b=[7;5;3]
epsi=1.0e-15;
kmax=20;
%===============================================================
% Conjugate gradient algorithm
%===============================================================
k=0;
x = [0;0;0]
r = b - A*x;
rho = r'*r;
while (sqrt(rho)>epsi*sqrt(b'*b) & k<kmax)
  k = k+1
  if k==1 
    p = r; 
  else
    beta = rho/rhoold; p = r + beta*p;
  end
  w = A*p;
  alpha = rho/(p'*w);
  x = x + alpha*p
  r = r - alpha*w;
  rhoold = rho;
  rho = r'*r;
end
eig(A)


