Home > atmlab > gridcreation > gridselect1D.m

gridselect1D

PURPOSE ^

GRIDSELECT1D Simple selection of a 1D grid

SYNOPSIS ^

function [xc,Ac] = gridselect1D( xf, Af, tol, repr, abs_or_rel )

DESCRIPTION ^

 GRIDSELECT1D   Simple selection of a 1D grid

    The function needs a set of realisations of the quantity for which the
    grid shall be selected. These realisations are given as columns of the
    matrix *Af*, are provided on a sufficient fine grid that representation
    errors can be neglected and shall span the possible cases that shall be
    represented. 

    The dimension of Af shall be [ xf, number of realisation ]

    The grid is selected by first including the end points of *xf*. The 
    coarse representation is transformed to the fine one, and a new grid 
    point is included where the maximum deviation is found (where all
    realisations are considered in parallel). This procedure is repeated
    until the coarse and representations deviates less then *tol* (at all
    points of *xf*).

    See also GRIDSELECT2D and GRIDSELECT3D.

 FORMAT   xc = gridselect1D( xf, Af, tol, repr, abs_or_rel )

 OUT   xc          Obtained coarse grid.
       Ac          Af at grid points xc.
 IN    xf          Reference fine grid.
       Af          Reference data (a matrix) on fine grid.
       tol         Acceptable tolarance.
       repr        String describing representation used. Possible choices
                   are equal to allowed 'methods' for *interp1*.
 OPT   abs_or_rel  String telling if tolarance is in absolute ('abs') or
                   relative ('rel') units. Default is 'abs'.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

DOWNLOAD ^

gridselect1D.m

SOURCE CODE ^

0001 % GRIDSELECT1D   Simple selection of a 1D grid
0002 %
0003 %    The function needs a set of realisations of the quantity for which the
0004 %    grid shall be selected. These realisations are given as columns of the
0005 %    matrix *Af*, are provided on a sufficient fine grid that representation
0006 %    errors can be neglected and shall span the possible cases that shall be
0007 %    represented.
0008 %
0009 %    The dimension of Af shall be [ xf, number of realisation ]
0010 %
0011 %    The grid is selected by first including the end points of *xf*. The
0012 %    coarse representation is transformed to the fine one, and a new grid
0013 %    point is included where the maximum deviation is found (where all
0014 %    realisations are considered in parallel). This procedure is repeated
0015 %    until the coarse and representations deviates less then *tol* (at all
0016 %    points of *xf*).
0017 %
0018 %    See also GRIDSELECT2D and GRIDSELECT3D.
0019 %
0020 % FORMAT   xc = gridselect1D( xf, Af, tol, repr, abs_or_rel )
0021 %
0022 % OUT   xc          Obtained coarse grid.
0023 %       Ac          Af at grid points xc.
0024 % IN    xf          Reference fine grid.
0025 %       Af          Reference data (a matrix) on fine grid.
0026 %       tol         Acceptable tolarance.
0027 %       repr        String describing representation used. Possible choices
0028 %                   are equal to allowed 'methods' for *interp1*.
0029 % OPT   abs_or_rel  String telling if tolarance is in absolute ('abs') or
0030 %                   relative ('rel') units. Default is 'abs'.
0031 
0032 % 2004-09-18   Created by Patrick Eriksson.
0033 
0034 
0035 function [xc,Ac] = gridselect1D( xf, Af, tol, repr, abs_or_rel )
0036 
0037 if nargin < 5
0038   abs_or_rel = 'abs';
0039 end
0040 
0041 n = length( xf );
0042 
0043 
0044 %=== Check input
0045 %
0046 if ~isvector( xf )
0047   error( 'Input argument *xf* must be a vector.' );
0048 end
0049 %
0050 if ndims( Af ) ~= 2  |  ~isnumeric( Af )
0051   error( 'Input argument *Af* must be a matrix.' );
0052 end
0053 %
0054 if size( Af, 1 ) ~= n
0055   error( 'Sizes of *xf* and *Af* do not match.' );
0056 end
0057 %
0058 if strcmp( abs_or_rel, 'rel' )  &  any( Af == 0 )
0059    error( 'With *abs_or_rel* = ''rel'', *Af can not contain zeros.' );
0060 end
0061 
0062 xf = vec2row( xf );
0063 
0064 ind = 1;
0065 ip  = n;
0066 e   = tol*2;
0067 
0068 
0069 while e > tol
0070 
0071   ind = sort( [ ind ip ] );
0072 
0073   Ac = interp1( xf(ind), Af(ind,:), xf, repr );
0074 
0075   if strcmp( abs_or_rel, 'abs' )
0076     [e,ip] = max( max( abs( Ac - Af ), [], 2 ) );
0077   elseif strcmp( abs_or_rel, 'rel' )
0078     [e,ip] = max( max( abs( (Ac - Af)./Af ), [], 2 ) );
0079   else
0080     error( ...
0081       sprintf('Unknown error type (%s). Can only be ''abs'' or ''rel''.', ...
0082                                                                 abs_or_rel ) );
0083   end
0084 end
0085 
0086 
0087 xc = xf(ind);
0088 
0089 if nargout > 1
0090   Ac = Af(xind,:);
0091 end
0092 
0093 
0094 return
0095 
0096 %= Some code for plotting
0097 %
0098 
0099 E = Ac - Af;
0100 
0101 plot( xf, mean( E, 2 ), 'r-', xf, max( E, [], 2 ), 'b--', ...
0102       xf, min( E, [], 2 ), 'g--' );
0103 ylabel( 'Absolute deviation' )
0104 legend( 'Mean deviation', 'Highest deviation', 'Lowest deviation' );

Generated on Mon 15-Sep-2014 13:31:28 by m2html © 2005