% Script file to examine the convergence properties of the polynomial
% tests in polynomialGridTest, as we refine the grid size and angle
% between elements in the stencil.
%
% Since the error in the median approximation is proportional to 
%
%    error in median approximation \prop (d_\theta + d_x^2)
%
% and 
%
%    d_\theta \prop \Delta x / d_x = 1 / (stencil radii)
%
% the choice of d_x = \Delta x^(1/3) leads to error \Delta x^(2/3), which
% should converge.  This routine is designed to follow this pattern and
% determine whether convergence is occurring empirically.
%
% Data is summarized textually in a format suitable for inclusion in a LaTeX
% table.

% Copyright 2007 Ian M. Mitchell (mitchell@cs.ubc.ca).
% This software is used, copied and distributed under the licensing 
%   agreement contained in the file LICENSE in the top directory of 
%   the distribution.
%
% Ian Mitchell, 6/17/07

  grid_ratios = [ 1, 2, 5 ];
  base_grid = [ 100, 50, 25 ];
  stencil_sizes = [ 1, 2, 3, 4, 5 ];
  stencil_shapes = { 'circle', 'circle', 'square' };
  % One entry in this vector for each stencil_shape.
  interpolation = [ 0, 1, 1 ];
  
  which_polynomial = 1;
  show_plots = 0;

  fprintf('\n');

  for i_ratio = 1 : length(grid_ratios)
    for i_stencil_size = 1 : length(stencil_sizes)

      grid_size = 1 + round(base_grid(i_ratio) ...
                            * stencil_sizes(i_stencil_size)^(3/2) ...
                            * [ grid_ratios(i_ratio); 1 ]);

      % To reduce clutter in the table, only show ratio when necessary.
      if(i_stencil_size == 1)
        fprintf('\n$1:%d$ & ', grid_ratios(i_ratio));
      else
        fprintf('\n      & ');
      end

      fprintf('%d & $%d \\cross %d$', stencil_sizes(i_stencil_size), ...
              grid_size(1), grid_size(2));
        
      for i_stencil_shape = 1 : length(stencil_shapes);
          
        errs = polynomialGridTest(grid_size, which_polynomial, ...
                                  stencil_sizes(i_stencil_size), ...
                                  stencil_shapes{i_stencil_shape}, ...
                                  interpolation(i_stencil_shape), show_plots);

        fprintf(' & %5.3f & %5.3f', errs.one, errs.inf);
      end
        
      % End a line of the table.
      fprintf(' \\\\')
    end
  end

  fprintf('\n');
  
  % Table columns are ratio dx, stencil_size, grid size, and then L1 &
  % Linf error for each stencil_shape/interpolation combination.
  
