function func = popearth ( iscal );
% PopEarth
%
% 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 2006/07
%
% Author :      Prof. Dr. M. Arnold, martin.arnold@mathematik.uni-halle.de
% Version of :  Nov 6, 2006
%
% Population dynamics on Earth
%
% Parameters:
%   iscal  (input)  : control flag scaling
%                       =0 .. default
%                       =1 .. zoom
%
% Examples:
%   popearth ( 0 );   %   print -dpng ../png/popearth0.png
%   popearth ( 1 );   %   print -dpng ../png/popearth1.png

graphwidth  = 24.0;
graphheight = 12.0;
fac         =  1.0;
shift       =  0.6;
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, source http:7/www.ecopop.ch/A5BEVOELKWELT/weltbev.html
data = [ 1500   0.5e09 
         1804   1.0e09
         1927   2.0e09
         1960   3.0e09
         1974   4.0e09
         1987   5.0e09
         1999   6.0e09
         2004   6.5e09 ];

% -> time scale
yymin = 1900;         
yymax = 2100;         
yy    = yymin:yymax;

% -> exponential fit
y0    = data(5,1);
p0    = data(5,2);
alpha = 0.016;
pexp  = p0 * exp ( alpha*(yy-y0) );

% -> logistic fit
y0     = data(6,1);
p0     = data(6,2);
lambda = 0.008e-10;
K      = 24.0e09;
plog   = K ./ ( 1 + ( K/p0 - 1 ) * exp ( -lambda*K*(yy-y0) ) );

% -> fit discussed by Heuser (1995)
y0     = data(6,1);
p0     = data(6,2);
lamheu = 0.015/5.0e09;
Kheu   = 10.0e09;
pheu   = Kheu ./ ( 1 + ( Kheu/p0 - 1 ) * exp ( -lamheu*Kheu*(yy-y0) ) );

% -> plot
if iscal==0,
    pmin   = 0;
    pmax   = 20.0e09;
    legpos = 'NorthWest';
elseif iscal==1,
    pmin   = 0;
    pmax   = 8.0e09;
    legpos = 'SouthEast';
end;
plot ( data(:,1), data(:,2), '*b', yy, pexp, 'r', yy, plog, 'g', yy, pheu, ':k', data(:,1), data(:,2), '*b' ); 
axis ( [ yymin yymax pmin pmax ] );
xlabel ( 'Jahr' );
ylabel ( 'Weltbevoelkerung' );
title ( 'Wachstumsmodelle am Beispiel der Zunahme der Weltbevoelkerung' );

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

legend ( 'Messdaten'                            ...
       , [ 'Exponentielles Wachstum  \alpha = ', sprintf('%4.3f',alpha)  ]  ...
       , [ 'Logistisches Wachstum  K = ',        sprintf('%8.1e',K) ]       ...
       , [ 'Logistisches Wachstum  K = ',        sprintf('%8.1e',Kheu) ]    ...
       , 'Location', legpos );

