function func = plotiter ( imeth );
% PlotIter
%
% This m-file is part of the material of the lectures on
%   Numerical Mathematics
% at the Math Department of Halle University, Germany.
%
% Author :      Martin Arnold, martin.arnold@mathematik.uni-halle.de
% Version of :  Dec 9, 2005
%
% Plot for a comparison of Iterative methods for the solution of linear systems
%
% Parameters:
%   imeth  (input)  : control flag "iterative methods"
%                       =1 .. Jacobi, Gauss-Seidel, SOR
%                       =2 .. SOR, CG, preCG
%
% Examples:
%   plotiter ( 1 );     %   print -dpng ../png/plotiter1.png
%   plotiter ( 2 );     %   print -dpng ../png/plotiter2.png
%
% Reference: Sections 8.4 and 8.7 of
%   Stoer, J.; Bulirsch, R.: Numerische Mathematik 2, Dritte Auflage.
%   Springer-Verlag Berlin Heidelberg New York, 1990.

graphwidth  = 24.0;
graphheight = 14.0;
fac         =  1.0;
shift       =  0.0;
papershift  =  0.0;

ActFontSize   = 14;
ActLineWidth  =  2;
ActMarkerSize =  6;

RatioFlag   = 0;

border      = 1.0;

%
% -> Bestimme Groesse des Bildschirms und Skalierungsfaktor fuer die Ausgabe
root = 0;
set ( root, 'Units', 'centimeters' );
screensize = get ( root, 'ScreenSize' );
left   = screensize(1);
bottom = screensize(2);
width  = screensize(3);
height = screensize(4);
set ( root, 'Units', 'pixels' );
if RatioFlag==0,
  screenscale = 1.0;
elseif RatioFlag==1,
  screenscale = (width-2*border) / graphwidth;
end;

%
% -> Definiere den Bildschirmausschnitt
if length ( get ( root, 'Children' ) ) == 0,
  h = figure;
else
  h = gcf;
  clf;
end;
set ( h, 'Units',    'centimeters' );
set ( h, 'Position', ...
  [ left+width-screenscale*graphwidth-border height-2*border-screenscale*graphheight ...
    screenscale*graphwidth                   screenscale*graphheight ] );
set ( h, 'Units',    'pixels' );

%
% -> Skalierung der Ausgabe in PostScript
%
set ( gcf, 'PaperUnits',    'centimeters' );
set ( gcf, 'PaperPosition', [ papershift papershift graphwidth graphheight ] );

%
% -> plot
clf,

if imeth==1,
    nit = 500;  NN = [ 10 20  40  80 ];
elseif imeth==2,
    nit = 500;  NN = [ 40 80 160 320 ];
end;
    
for in=1:4,
    if imeth==1,
        [ errjac, errgs, errsor ] = liniter ( NN(in), nit, 0 );
        err_red    = errjac;
        err_blue   = errgs;
        err_green  = errsor;
        lstr_red   = 'Jacobi';
        lstr_blue  = 'Gauss-Seidel';
        lstr_green = 'SOR';
    elseif imeth==2,
        [ errsor, errcg, errcgp ] = cgiter ( NN(in), nit, 0 );
        err_red    = errsor;
        err_blue   = errcg;
        err_green  = errcgp;
        lstr_red   = 'SOR';
        lstr_blue  = 'CG';
        lstr_green = 'CG (preconditioned)';
    end;
    subplot ( 2, 2, in ); 
    semilogy ( (1+(0:nit))', err_red,   'r',  (1+(0:nit))', err_blue, 'b'  ...
             , (1+(0:nit))', err_green, 'g' );
    axis ( [ 0, 600, 1e-20, 1.0 ] );
    xlabel ( 'k' );  ylabel ( '||x^{(k)}-x||' );
    title ( sprintf ( 'Model problem   N = %i', NN(in) ) );
    hlegend(in) = legend ( lstr_red, lstr_blue, lstr_green );
end;

%
% -> Korrektur der Groesse und Lage der Koordinatensysteme
allax = get ( gcf, 'Children' )';
for ax = allax,
  rect    = get ( ax, 'Position' );
  rect(2) = rect(2) + shift * (1-fac) * rect(4);
  rect(4) = fac * rect(4);
  set ( ax, 'Position', rect );
end;

%
% -> Aenderung der Schriftgroesse
for hf = allax,
  set ( hf, 'FontSize', [ActFontSize] );
  set ( get ( hf, 'title' ), 'FontSize', [ActFontSize] );
  set ( get ( hf, 'xlabel' ), 'FontSize', [ActFontSize] );
  set ( get ( hf, 'ylabel' ), 'FontSize', [ActFontSize] );
  allch = get ( hf, 'Children' )';
  for hch = allch,
    if strcmp(get(hch,'Type'),'line'),
      set ( hch, 'LineWidth',  ActLineWidth );
      set ( hch, 'MarkerSize', ActMarkerSize );
    end;
  end;
end;

% -> enforce correct position of legend
for in=1:4,
    set ( hlegend, 'Location', 'SouthEast' );
end;
