function func = plotchar ( iexmpl, c );
% PlotChar
%
% 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, arnold@mathematik.uni-halle.de
% Version of :  May 9, 2005
%
% Animated Plot of the solution by the method of characteristics
%
% Parameters:
%   iexmpl (input)  : number of example
%                       =1 .. constant wave speed
%                       =2 .. variable wave speed
%   c      (input)  : characteristic wave speed
%
% Example:
%   plotchar ( 1, 2.5 );   % print -dpng ../png/plotchar01.png
%   plotchar ( 2, 1.0 );   % print -dpng ../png/plotchar02.png

graphwidth  = 15.0;
graphheight = 10.0;
fac         =  0.95;
shift       =  0.5;
papershift  =  0.0;

ActFontSize   = 14;
ActLineWidth  =  2;
ActMarkerSize =  8;
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 grid
nx = 201;
nt = 101;

xmin = -5.0;
xmax =  5.0;
tmin =  0.0;
tmax =  1.0;

x = linspace ( xmin, xmax, nx );
t = linspace ( tmin, tmax, nt );

[ X, T ] = meshgrid ( x, t );

% -> solution of wave equation
if iexmpl==1,
    U = 0.2*exp(-(X-c*T).^2/2) + 0.8*exp(-(X-c*T).^4/2);
elseif iexmpl==2,
    Xarg = - log ( exp(-X) + c*T );
    U = 0.2*exp(-Xarg.^2/2) + 0.8*exp(-Xarg.^4/2);
end;

% -> first plot
mymesh = mesh ( T, X, U );
set ( gca, 'CameraPosition', [ 3.5 -12.0 8.5 ] );
xlabel ( 't' );
ylabel ( 'x' );
zlabel ( 'u' );

set ( gca, 'XLim', [ tmin tmax ] );
set ( gca, 'YLim', [ xmin xmax ] );

hold on;
if iexmpl==1,
    plot3 ( [ tmin tmax ], (xmin+xmax)/2 + [ 0 c*(tmax-tmin) ], [ 0 0 ], '--k' ); 
    title( sprintf('u_t + c u_x = 0 ,    c = %4.2f',c) );
elseif iexmpl==2,
    xh = exp((xmin+xmax)/2) - c*t;
    xt = NaN * ones ( size(xh) );
    xt(xh>0) = - log ( xh(xh>0) );
    plot3 ( t, xt, zeros(size(t)), '--k' ); 
    title( sprintf('u_t + c e^x u_x = 0 ,    c = %4.2f',c) );
end;
hold off;

drawnow;

%
% -> 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] );
  set ( get ( hf, 'zlabel' ), 'FontSize', [ActFontSize] );
end;

% -> animation
for iplot=3:nt,
    Uact = U;
    Uact(iplot:end,:) = NaN;
    set ( mymesh, 'ZData', Uact );
    drawnow;
end;

