% Math 5620 - Spring 2012
% Problem 2 - Sample code
%
% Things you need to change/implement
%  - the matrices and rhs.
%  - jacobi.m, gs.m, sor.m (see sample code)
%
% Your Name.

% tolerance to use
tol = 1e-3;

% systems to solve:
%  we specify three cell arrays containing: name of system, matrix and RHS.
% (TO BE CHANGED)

names{1} = 'part a';
As{1} = [ 3 -1 1
          3 6 2 
	  3 3 7 ];
bs{1} = [1; 0; 4];

names{2} = 'part b';
As{2} = [ 2 -1 0
          -1 2 -1
	  0 -1  2 ];
bs{2} = [2;3;1];

% do computations
for i=1:length(As),
 % Jacobi method
 fprintf([names{i},' ','Jacobi\n']);
 [xjac,jjac] = jacobi(As{i},bs{i},zeros(size(bs{i})),100,tol);
 xjacs{i} = xjac; jjacs{i} = jjac; % store solution and iteration history

 % Gauss Seidel
 fprintf([names{i},' ','Gauss-Seidel\n']);
 [xgs,jgs] = gs(As{i},bs{i},zeros(size(bs{i})),100,tol);
 xgss{i} = xgs; jgss{i} = jgs; % store solution and iteration history

 % SOR
 fprintf([names{i},' ','SOR\n']);
 [xsor,jsor] = sor(As{i},bs{i},zeros(size(bs{i})),1.2,100,tol);
 xsors{i} = xsor; jsors{i} = jsor; % store solution and iteration history
end;

% print tables
fprintf('%10s %10s %10s %10s\n',' ','Jacobi','GS','SOR');
for i=1:length(As),
 fprintf('%10s %10d %10d %10d\n',names{i},jjacs{i},jgss{i},jsors{i});
end;
fprintf('\n\n');
format long

% print solutions
fprintf('solutions with different methods are:\n');
for i=1:length(As),
 fprintf([names{i} '\n']);
 fprintf('   Jacobi: '); fprintf('%13.9g ',xjacs{i}); fprintf('\n');
 fprintf('   GS:     '); fprintf('%13.9g ',xgss{i}); fprintf('\n');
 fprintf('   SOR:    '); fprintf('%13.9g ',xsors{i}); fprintf('\n');
end;


% Sample output
%  >> prob2      
%  part a Jacobi
%  x=[   0.00000000    0.00000000    0.00000000 ]
%  x=[   0.33333333    0.00000000    0.57142857 ]
%  x=[   0.14285714   -0.35714286    0.42857143 ]
%  part a Gauss-Seidel
%  x=[   0.00000000    0.00000000    0.00000000 ]
%  x=[   0.33333333   -0.16666667    0.50000000 ]
%  x=[   0.11111111   -0.22222222    0.61904762 ]
%  part a SOR
%  x=[   0.00000000    0.00000000    0.00000000 ]
%  x=[   0.40000000   -0.24000000    0.60342857 ]
%  x=[  -0.01737143   -0.18294857    0.66805029 ]
%  part b Jacobi
%  x=[   0.00000000    0.00000000    0.00000000 ]
%  x=[   1.00000000    1.50000000    0.50000000 ]
%  x=[   1.75000000    2.25000000    1.25000000 ]
%  part b Gauss-Seidel
%  x=[   0.00000000    0.00000000    0.00000000 ]
%  x=[   1.00000000    2.00000000    1.50000000 ]
%  x=[   2.00000000    3.25000000    2.12500000 ]
%  part b SOR
%  x=[   0.00000000    0.00000000    0.00000000 ]
%  x=[   1.20000000    2.52000000    2.11200000 ]
%  x=[   2.47200000    4.04640000    2.60544000 ]
%                 Jacobi         GS        SOR
%      part a         10          7         12
%      part b         18         11          6
%  
%  
%  solutions with different methods are:
%  part a
%     Jacobi:    0.03507839   -0.23692617   0.657801451 
%     GS:      0.0351508066  -0.236828388   0.657861821 
%     SOR:     0.0348846894  -0.236647413   0.657901339 
%  part b
%     Jacobi:    3.24414062    4.49121094    2.74414062 
%     GS:        3.24755859    4.49755859     2.7487793 
%     SOR:       3.25071931    4.50065944      2.750207     
