function func = plotfdm ( iplot );
% PlotFDM
%
% This m-file is part of the material of the lecture
%   Theory and Numerics of Partial Differential Equations
% at the Math Department of Halle University, Germany.
%
% Author :      Prof. Dr. M. Arnold, martin.arnold@mathematik.uni-halle.de
% Version of :  Jun 3, 2005
%
% Error Plot for Finite Difference Method
%
% Parameters:
%   iplot  (input)  : control flag "plot"
%                       =0 .. loglog diagram err vs. h
%                       =1 .. A_h*U-q_h
%                       =2 .. u_h-U
%
% Examples:
%   plotfdm ( 0 );     %   print -dpng ../png/plotfdm0.png
%   plotfdm ( 1 );     %   print -dpng ../png/plotfdm1.png
%   plotfdm ( 2 );     %   print -dpng ../png/plotfdm2.png

graphwidth  = 12.0;
graphheight = 10.0;
fac         =  0.95;
shift       =  0.25;
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 ] );

%
% -> get data
if iplot==0,
    Nvec = zeros ( 8, 1 );    Nvec(1) = 4;
    conerr = zeros ( length(Nvec), 4 );
    for iN=1:length(Nvec),  
        [ conerr(iN,:), XX, YY, merrs, merrv ] = fdmerr ( Nvec(iN) );  
        if iN<length(Nvec),  Nvec(iN+1) = 2*Nvec(iN);  end;
    end;
else
    Ncur = 40;
    [ conerr, XX, YY, merrs, merrv ] = fdmerr ( Ncur );
end;

%
% -> plot
clf,

if iplot==0,
    loglog ( 1./Nvec, conerr, 'LineWidth', ActLineWidth );
    xlabel ( 'h' );
    ylabel ( 'Error' );
    title ( 'f(x,y)  =  5 * \pi^2 * sin(\pi*x) * sin(2*\pi*y)' );
    h = legend (                            ...
                 '|| A_h*U - q_h ||_\infty' ...
               , '|| A_h*U - q_h ||_h'      ...
               , '|| u_h - U ||_\infty'     ...
               , '|| u_h - U ||_h'          ...
               );
else
    if iplot==1,
        mesh ( XX, YY, merrs );
        title ( sprintf('h = %6.2e ,  Konsistenz:  A_h*U - q_h',1/Ncur) );
    elseif iplot==2,
        mesh ( XX, YY, merrv );
        title ( sprintf('h = %6.2e ,  Konvergenz:  u_h - U',1/Ncur) );
    end;
    xlabel ( 'x' );
    ylabel ( 'y' );
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
if iplot==0,
    set ( h, 'Location', 'SouthEast' );
    set ( h, 'FontSize', ActFontSize-2 );
end;


