%
% Jacobi and GS for Hilbert linear equations  
%
clearvars; 

n = 32;
H = hilb(n);        % Hilbert matrix  
xtrue = ones(n,1);  % true solution 
b = H*xtrue;        % right-hand side

tol = 1e-4;
IterMax = 200;
x0 = zeros(n,1);

omega = 1.5; 
[x, iter, relres, flag] = sor(H, b, x0, tol, IterMax, omega);
if flag == 1
    fprintf('SOR: Iter=%d, relres=%.4e\n', iter, norm(b-H*x)/norm(b));
else
    fprintf('SOR: 不收敛！relres=%.4e, \n', norm(b-H*x)/norm(b))
end

[x, iter, relres, flag] = CG(H, b, x0, tol, IterMax); 
if flag == 1
    fprintf('CG : Iter=%d, relres=%.4e\n', iter, norm(b-H*x)/norm(b));
else
    fprintf('CG : 不收敛！relres=%.4e\n', norm(b-H*x)/norm(b));
end
%%%%% ========== End of the main program ===========================


% SOR iteration
function [x, iter, relres, flag] = sor(A, b, x, tol, IterMax, omega)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%  这里是你的代码  %%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
end


%%%%% CG method %%%%%
function [x, iter, relres, flag] = CG(A, b, x, tol, IterMax)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%  这里是你的代码  %%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
end

