function func = demoquiv ( iplot );
% DemoQuiv
%
% This m-file is part of the material of the lecture
%   Theory and Numerics of Ordinary Differential Equations
% at the Math Department of Halle University, Germany.
% Winter term 2004/05
%
% Author :      Prof. Dr. M. Arnold, arnold@mathematik.uni-halle.de
% Version of :  Oct 14, 2004
%
% Demonstration quiver plot
%
% Parameters:
%   iplot  (input)  : control flag "plot"
%                       =0 .. without solution curve
%                       =1 .. with one solution curve
%
% Examples:
%   demoquiv ( 0 );   %   print -dpng ../png/demoquiv0.png
%   demoquiv ( 1 );   %   print -dpng ../png/demoquiv1.png
%   demoquiv ( 2 );   %   print -dpng ../png/demoquiv2.png

graphwidth  = 16.0;
graphheight = 12.0;
fac         =  0.98;
shift       =  0.4;
papershift  =  0.0;

ActFontSize   = 14;
ActLineWidth  =  2;
ActMarkerSize = 12;
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 ] );

% -> data
xmin = -2.0;
xmax =  2.0;
ymin = -2.0;
ymax =  2.0;

nx = 21;   x = linspace ( xmin, xmax, nx );
ny = 21;   y = linspace ( ymin, ymax, ny );
[ XX, YY ] = meshgrid ( x, y );

fmax = 4.0;                          % -> ignore very long quivers
FF = XX.^2 + YY.^2;
FF(abs(FF)>fmax) = NaN;
quiver ( x, y, ones(size(FF)), FF, 1.4 );
grid;
set ( gca, 'DataAspectRatio',    [ 1 1 1 ] );
set ( gca, 'PlotBoxAspectRatio', [ 1 1 1 ] );
axis ( [ xmin xmax ymin ymax ] );

xx  = linspace ( xmin, xmax, 201 );
yy0 = - xx.*(-besselj(-3/4,xx.^2/2)+bessely(-3/4,xx.^2/2)) ./ ...
            (-besselj(1/4,xx.^2/2)+bessely(1/4,xx.^2/2));
yyo = - xx.*(2 *besselj(-3/4,xx.^2/2)*gamma(3/4)^2 -          ...
             2 *bessely(-3/4,xx.^2/2)*gamma(3/4)^2 -          ...
             pi*besselj(-3/4,xx.^2/2) )                    ./ ...
            (2 *besselj(1/4,xx.^2/2)*gamma(3/4)^2 -           ...
             2 *bessely(1/4,xx.^2/2)*gamma(3/4)^2 -           ...
             pi*besselj(1/4,xx.^2/2) );
yyo(abs(xx)>1.2) = NaN;
yyu = - xx.*(2 *besselj(-3/4,xx.^2/2)*gamma(3/4)^2 -          ...
             2 *bessely(-3/4,xx.^2/2)*gamma(3/4)^2 +          ...
             pi*besselj(-3/4,xx.^2/2) )                    ./ ...
            (2 *besselj(1/4,xx.^2/2)*gamma(3/4)^2 -           ...
             2 *bessely(1/4,xx.^2/2)*gamma(3/4)^2 +           ...
             pi*besselj(1/4,xx.^2/2) );

if ( (iplot==1) | (iplot==2) ),
    hold on;
    plot ( xx, yy0, '-r', 'LineWidth', ActLineWidth );
    hold off;
end;
if (iplot==2),
    hold on;
    plot ( xx, yyo, '-r', 'LineWidth', 1 );
    plot ( xx, yyu, '-r', 'LineWidth', 1 );
    hold off;
end;

xlabel ( 'x' );
ylabel ( 'y' );
title ( 'Richtungsfeld fuer  y''(x) = x^2+y^2' );

%
% -> 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 etc.
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;

