tags:

views:

40

answers:

2

I found that if I use griddata Method with Cubic Interpolation method, for certain values of x, y, it will return NaN. One post says that this is because the x and y data are very near to convex hull.

Any idea how to fix this?

Edit: Note that I can't make sure that my inputs are monotonously increasing ( thus, gridfit doesn't work). The reason is because I would have to mesh my area ( which could be an irregular polygon in 2D), get all the points before generating the coressponding Z values for each points. My code is as follows:

function ZI=Interpolate3D(scatteredData, boundary)
%scatteredData is the scattered points, boundary is the area that I want to generate 3D surface.



% Given the boundaries, generate mesh first 
[element,points]= GenMesh(boundary);
ZI = griddata(scatteredData(:,1),scatteredData(:,2),scatteredData(:,3),points(:,1),points(:,2), 'cubic',{'QJ'});
A: 

Are you sure you want cubic interpolation? For some input data the calculated z-nodes could have extreme large values!

I always use -v4 option like the post in your link mentions. You can also play with options used in Qhull via delaunayn, some (but not all) are {'Qt','Qbb','Qc'} http://www.qhull.org/html/qhull.htm

Mikhail
The problem with the -v4 option is it is terribly slow. It also consumes large amounts of memory if you have many points. Also note that these qhull options are going away in future releases.
woodchips
I'm not sure how the Qhull option can solve my problem, care to elaborate?
Ngu Soon Hui
The qhull options will not help here. They can only help to produce a triangulation when it might fail, perhaps due to replicate or near replicate points, or for collinear points. The problem that produces nans is well beyond the point where the qhull options are employed. It is in the interpolation phase, so qhull is inconsequential.
woodchips
@woodchips, I agree with you on this point.
Ngu Soon Hui
+2  A: 

If your points are outside of the convex hull, you CANNOT get a result other than NaN from griddata using the cubic option. If the point is right on the line, then a NaN may result, depending upon what happens in the least significant bits of the computation.

The issue is that the cubic method uses a triangulation. If your point is outside of the convex hull, then the triangulation fails on that point.

Of course, you CAN use the -v4 method, but there are tremendously good reasons why it has largely been superceded. It uses a distance based interpolation method, where for n data points, a full nxn matrix must be generated. Then a system of equations is solved using that matrix. This will be quite slow for even moderately large problems.

The virtue of the -v4 method is it will extrapolate smoothly without producing nans. This is why it was left in there.

For larger problems where you need a smooth result, and you still wish to extrapolate outside of the convex hull, you can use my gridfit tool. It does do smoothing though, not pure interpolation.

All such methods have tradeoffs that you must resolve for your particular problem.

woodchips
gridfit is a great tool, but my x, y input can't be monotonously increasing, see my updated answer for a code sample.
Ngu Soon Hui
gridfit is not a solution for all problems. If you have a specific domain of interest that is not a lattice, then gridfit is not for you.
woodchips
@woodchip, not sure what do you mean by 'lattice', but I *do* have a set of elements and coordinates as a result of meshing.
Ngu Soon Hui
Gridfit estimates a surface, defined on the support of a rectangular lattice of points.
woodchips
@woodchips, must it be rectangular? What about triangular?
Ngu Soon Hui
@woodchips, also, what about *rotated* rectangular grid?
Ngu Soon Hui
REctangular, in the sense of what meshgrid would return.
woodchips
No triangulated domains. No rotated grids. If you want a rotated grid, then rotate the data instead.
woodchips

related questions