% Test scripts that illustrate various aspects of GMRES.
%
% Feb 1, 2022, P.G. Martinsson, UT-Austin

function main_gmres

rng(0)
n = 10;

%%% Generate A and b
V  = randn(n) + n*eye(n);
dd = 1 + 0.1*(1 - 2*rand(1,n));
%dd = ones(1,n); dd(1:3) = [1.1,1.2,1.3];
A  = V*diag(dd)*inv(V);
b  = randn(n,1);
x_exact = A\b;

%%% Arnoldi iteration
Q = zeros(n);
H = zeros(n);
Q(:,1) = b/norm(b);
for j = 1:(n-1)
  v        = A*Q(:,j);
  H(1:j,j) = Q(:,1:j)'*v;
  v        = v - Q(:,1:j)*H(1:j,j);
  H(j+1,j) = norm(v);
  v        = v - Q(:,1:j)*(Q(:,1:j)'*v); % Re-orthogonalization step.
  Q(:,j+1) = (1/norm(v))*v;
  y        = H(1:(j+1),1:j)\[norm(b);zeros(j,1)];
  x        = Q(:,1:j)*y;
  fprintf(1,'Step %3d: Error = %10.3e\n',j,max(abs(x - x_exact)))
end
H(:,n) = Q'*A*Q(:,n);

fprintf(1,'|A - Q*H*trans(Q)    = %10.3e\n',max(max(abs(A - Q*H*Q'))))
fprintf(1,'|I - Q*trans(Q)      = %10.3e\n',max(max(abs(eye(n) - Q*Q'))))

keyboard

return

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

