help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Can't get griddata3 to work


From: c.
Subject: Re: Can't get griddata3 to work
Date: Tue, 15 Mar 2011 10:05:16 +0100

On 15 Mar 2011, at 02:32, mph2 wrote:

> I can't get griddata3 to work with nicely ordered input data.
> 
> I have a set of data, which are temperatures on a x,y,z volume grid.
> Unfortunately, some x,y,z triplets are missing in the set, and I was trying
> to use griddata3 to get these values by interpolation. My problem is that
> apparently the ordering of the original set is relevant to whether griddata3
> works. (I can't use interp3 because I don't have temperature data for all
> x,y,z triplets.)
> 
> I've been trying to use test sample data to see what's going on. If I run a
> slightly modified version of the examples:
> 
> x = 2 * rand(1000, 1) - 1;
> y = 2 * rand(1000, 1) - 1;
> z = 2 * rand(1000, 1) - 1;
> % x = sort(x);
> % y = sort(y);
> % z = sort(z);
> v = x + y + z;
> 
> [xi, yi, zi] = meshgrid(-0.8:0.2:0.8);
> vi = griddata3(x, y, z, v, xi, yi, zi, 'linear');
> vv = vi - xi - yi - zi;
> sum(isfinite(vi(:)))
> sum(isnan(vi(:)))
> 
> then all is well, and only one or two values in vi end up being NaN. I get
> something like 727 values, 2 NaN's or so, depending on the run. But if I
> sort the vectors by uncommenting the three lines above, the routing no
> longer works. I get one real value, and 728 NaN's or so. The output vi is
> useless.
> Also, even without sorting, if the x,y,z vectors are only 100 values long,
> even without sorting, I get 50% NaN's in the output array vi. Shouldn't 100
> values be enough to interpolate?
> 
> This all seems very weird. What's going on? Is this fixable?

Hi,

griddata3 with method 'linear' internally uses a delaunay triangolation of your 
points and 
then linear interpolation within the simplices of the generated mesh.

this method only works within the convex hull oh your x,y,z points, if you ask 
to extrapolate 
beyond the convex hull you will get a NaN

to visualize what's actually going on, a 2D example may help. try the following

x = 2 * rand(100, 1) - 1;
y = 2 * rand(100, 1) - 1;

T = delaunay (x, y);
v = x + y;

xs = sort(x);
ys = sort(y);

Ts = delaunay (xs, ys);
vs = xs + ys;


[xi, yi] = meshgrid(-0.8:0.2:0.8);


triplot (T, x, y, 'r')
hold on
triplot (Ts, xs, ys, 'b')
plot (xi, yi, 'x')


you will see many of the xi,yi couples will fall outside the blue mesh while 
relatively few are outside the red mesh.
If you wish to extrapolate outside the convex hull of your sampling points you 
should use the method 'nearest' rather than
'linear' at least for those points that are outside the convex hull.

HTH,
c.






reply via email to

[Prev in Thread] Current Thread [Next in Thread]