Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Difference in griddata/TriScatteredInterp between MATLAB versions

Subject: Difference in griddata/TriScatteredInterp between MATLAB versions

From: Brock

Date: 2 Apr, 2012 18:00:13

Message: 1 of 2

I am getting significant differences when using griddata/TriScatteredInterp between different versions of MATLAB. This interpolation is a necessary step in a much larger routine to compute the spectral approximation to a velocity field, and using the griddata/TriScatteredInterp functions in the latest MATLAB release (2012a) produces overall results that do not converge appropriately as the sampling resolution is increased. In contrast, the 2010a results do converge properly. Thus, the newest version of MATLAB appears unusable for me for this problem. The griddata/TriScatteredInterp function is the only MATLAB built-in function I use in my larger routine.

I've included a simple routine that samples a velocity field at a discrete number of points, and then tries to interpolate at a new grid staggered from the first. There is no difference (to machine precision) between MATLAB versions for the sampled velocities, however their interpolants differ greatly (the max difference between the interpolants is between 2-4% of the maximum x- and y-velocities). This difference is identical using both the griddata and TriScatteredInterp formulations.

It is a shame that the newest release appears to perform so poorly, whereas the older version (2010a) seems to give much more accurate results. Does anyone know why this would be occurring or a workaround to fix it? Is there any way to roll back the current griddata/TriScatteredInterp routines to their 2010a predecessors? I just updated to R2012a, and am ready to delete it from my computer in favor of R2010a, unless this discrepancy can be resolved.

Any help that can be provided (explanation/workaround/etc.) would be greatly appreciated.

Thanks in advance,
--Brock

Code:
A test file is included below to illustrate this behavior. I ran it first on R2010a and then on R2012a, saving respective MAT files on each run. The MAT files were then loaded and the results compared. The code to compare the results is currently commented out below. I have the individual MAT files from the runs under different MATLAB versions and can e-mail them by request.

% Script to illustrate differences between griddata/TriScatteredInterp
% between different versions of MATLAB. Calling griddata or
% TriScatteredInterp (commented out below) makes no difference; the
% results are equivalent to machine precision.

function testfile

%**************************** Gather/Save Data ****************************
% Define grid parameters.
Nx = 10;
Ny = Nx;
NxI = 11;
NyI = NxI;
X0 = 0;
Y0 = 0;
L = 1;
H = 1;

% Grid for velocity sample.
X = repmat( linspace( X0, X0+L, Nx )', Ny, 1 ); % x-gridpoints (col vect).
Y = [];
Yvals = linspace( Y0, Y0+H, Ny );
for i = 1 : Ny
    Y = [Y; Yvals(i)*ones( Nx, 1 ) ]; % y-gridpoints (col vect).
end
[ Xg Yg ] = meshgrid( X, Y );

% Grid for interpolation.
XI = repmat( linspace( X0, X0+L, NxI )', NyI, 1 ); % x at which to interp.
YI = [];
YvalsI = linspace( Y0, Y0+H, NyI );
for i = 1 : NyI
    YI = [YI; YvalsI(i)*ones( NxI, 1 ) ]; % y-gridpoints (col vect).
end

% Sample velocity field on sampling grid.
for j = 1 : length(X)
    t = 0;
    V = veloc( t, [ X(j); Y(j) ] );
    Ux(j,1) = V(1);
    Uy(j,1) = V(2);
end

% Interpolate x- and y-velocity on interpolation grid.
UxI = griddata( X, Y, Ux, XI, YI );
UyI = griddata( X, Y, Uy, XI, YI );

% Fx = TriScatteredInterp( X, Y, Ux );
% UxI = Fx( XI, YI );
% Fy = TriScatteredInterp( X, Y, Uy );
% UyI = Fy( XI, YI );

% save interp2010a.mat
save interp2012a.mat
%**************************************************************************

% %******************* Compare results between versions *********************
% clear
% load interp2010a.mat
% UxI10 = UxI;
% UyI10 = UyI;
% Ux10 = Ux;
% Uy10 = Uy;
% load interp2012a.mat
% UxI12 = UxI;
% UyI12 = UyI;
% Ux12 = Ux;
% Uy12 = Uy;
% xImax = max(UxI10 - UxI12);
% yImax = max(UyI10 - UyI12);
% xmax = max(Ux10 - Ux12);
% ymax = max(Uy10 - Uy12);
% disp(sprintf('Max difference in sampled x veloc: %.16f', xmax));
% disp(sprintf('Max difference in sampled y veloc: %.16f', ymax));
% disp(sprintf('Max difference in interpolated x: %.16f', xImax));
% disp(sprintf('Max difference in interpolated y: %.16f', yImax));
% %**************************************************************************


%**************************************************************************
%************************** Auxiliary Functions ***************************
%**************************************************************************


function V = veloc( t, pos )

global tTrans

x = pos(1);
y = pos(2);
T = 0;

% Compute velocities.
u = -pi * sin(2*pi*x) * cos(pi*y);
v = 2*pi * cos(2*pi*x) * sin(pi*y);
V = [ u ; v ];

  

Subject: Difference in griddata/TriScatteredInterp between MATLAB versions

From: Damian Sheehy

Date: 7 Apr, 2012 02:11:16

Message: 2 of 2

Hi,

Your sample data forms a regular grid so you should get better results if you use grid-based interpolation; interp2 or griddedInterpolant.
If you perform the following operation on your (X, Y) data
  dt = DelaunayTri(X,Y);
  triplot(dt)
You will see the topology of the triangulation is more uniform in R2012a than it was in R2010a. TriScatteredInterp uses this triangulation to perform interpolation. So R2012a will give you a more consistent interpolation across the cells of the grid.

When you use triangulation-based algorithms to interpolate over a grid, the interpolated values are sensitive to diagonal swaps.
 
Damian

"Brock " <bmosov01@hotmail.com> wrote in message <jlcpfd$1bl$1@newscl01ah.mathworks.com>...
> I am getting significant differences when using griddata/TriScatteredInterp between different versions of MATLAB. This interpolation is a necessary step in a much larger routine to compute the spectral approximation to a velocity field, and using the griddata/TriScatteredInterp functions in the latest MATLAB release (2012a) produces overall results that do not converge appropriately as the sampling resolution is increased. In contrast, the 2010a results do converge properly. Thus, the newest version of MATLAB appears unusable for me for this problem. The griddata/TriScatteredInterp function is the only MATLAB built-in function I use in my larger routine.
>
> I've included a simple routine that samples a velocity field at a discrete number of points, and then tries to interpolate at a new grid staggered from the first. There is no difference (to machine precision) between MATLAB versions for the sampled velocities, however their interpolants differ greatly (the max difference between the interpolants is between 2-4% of the maximum x- and y-velocities). This difference is identical using both the griddata and TriScatteredInterp formulations.
>
> It is a shame that the newest release appears to perform so poorly, whereas the older version (2010a) seems to give much more accurate results. Does anyone know why this would be occurring or a workaround to fix it? Is there any way to roll back the current griddata/TriScatteredInterp routines to their 2010a predecessors? I just updated to R2012a, and am ready to delete it from my computer in favor of R2010a, unless this discrepancy can be resolved.
>
> Any help that can be provided (explanation/workaround/etc.) would be greatly appreciated.
>
> Thanks in advance,
> --Brock
>
> Code:
> A test file is included below to illustrate this behavior. I ran it first on R2010a and then on R2012a, saving respective MAT files on each run. The MAT files were then loaded and the results compared. The code to compare the results is currently commented out below. I have the individual MAT files from the runs under different MATLAB versions and can e-mail them by request.
>
> % Script to illustrate differences between griddata/TriScatteredInterp
> % between different versions of MATLAB. Calling griddata or
> % TriScatteredInterp (commented out below) makes no difference; the
> % results are equivalent to machine precision.
>
> function testfile
>
> %**************************** Gather/Save Data ****************************
> % Define grid parameters.
> Nx = 10;
> Ny = Nx;
> NxI = 11;
> NyI = NxI;
> X0 = 0;
> Y0 = 0;
> L = 1;
> H = 1;
>
> % Grid for velocity sample.
> X = repmat( linspace( X0, X0+L, Nx )', Ny, 1 ); % x-gridpoints (col vect).
> Y = [];
> Yvals = linspace( Y0, Y0+H, Ny );
> for i = 1 : Ny
> Y = [Y; Yvals(i)*ones( Nx, 1 ) ]; % y-gridpoints (col vect).
> end
> [ Xg Yg ] = meshgrid( X, Y );
>
> % Grid for interpolation.
> XI = repmat( linspace( X0, X0+L, NxI )', NyI, 1 ); % x at which to interp.
> YI = [];
> YvalsI = linspace( Y0, Y0+H, NyI );
> for i = 1 : NyI
> YI = [YI; YvalsI(i)*ones( NxI, 1 ) ]; % y-gridpoints (col vect).
> end
>
> % Sample velocity field on sampling grid.
> for j = 1 : length(X)
> t = 0;
> V = veloc( t, [ X(j); Y(j) ] );
> Ux(j,1) = V(1);
> Uy(j,1) = V(2);
> end
>
> % Interpolate x- and y-velocity on interpolation grid.
> UxI = griddata( X, Y, Ux, XI, YI );
> UyI = griddata( X, Y, Uy, XI, YI );
>
> % Fx = TriScatteredInterp( X, Y, Ux );
> % UxI = Fx( XI, YI );
> % Fy = TriScatteredInterp( X, Y, Uy );
> % UyI = Fy( XI, YI );
>
> % save interp2010a.mat
> save interp2012a.mat
> %**************************************************************************
>
> % %******************* Compare results between versions *********************
> % clear
> % load interp2010a.mat
> % UxI10 = UxI;
> % UyI10 = UyI;
> % Ux10 = Ux;
> % Uy10 = Uy;
> % load interp2012a.mat
> % UxI12 = UxI;
> % UyI12 = UyI;
> % Ux12 = Ux;
> % Uy12 = Uy;
> % xImax = max(UxI10 - UxI12);
> % yImax = max(UyI10 - UyI12);
> % xmax = max(Ux10 - Ux12);
> % ymax = max(Uy10 - Uy12);
> % disp(sprintf('Max difference in sampled x veloc: %.16f', xmax));
> % disp(sprintf('Max difference in sampled y veloc: %.16f', ymax));
> % disp(sprintf('Max difference in interpolated x: %.16f', xImax));
> % disp(sprintf('Max difference in interpolated y: %.16f', yImax));
> % %**************************************************************************
>
>
> %**************************************************************************
> %************************** Auxiliary Functions ***************************
> %**************************************************************************
>
>
> function V = veloc( t, pos )
>
> global tTrans
>
> x = pos(1);
> y = pos(2);
> T = 0;
>
> % Compute velocities.
> u = -pi * sin(2*pi*x) * cos(pi*y);
> v = 2*pi * cos(2*pi*x) * sin(pi*y);
> V = [ u ; v ];
>
>

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us