4

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'});

3 Answers 3

5

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.

8
  • gridfit is a great tool, but my x, y input can't be monotonously increasing, see my updated answer for a code sample.
    – Graviton
    Sep 6, 2010 at 14:02
  • 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.
    – user85109
    Sep 6, 2010 at 14:38
  • @woodchip, not sure what do you mean by 'lattice', but I do have a set of elements and coordinates as a result of meshing.
    – Graviton
    Sep 6, 2010 at 15:16
  • Gridfit estimates a surface, defined on the support of a rectangular lattice of points.
    – user85109
    Sep 6, 2010 at 19:07
  • @woodchips, must it be rectangular? What about triangular?
    – Graviton
    Sep 7, 2010 at 3:52
1

Since the release of Matlab R2013a you can use scatteredInterpolant instead of griddata. This has several advantages:

  • Matlab can perform interpolation as well as extrapolation on a scatteredInterpolant object. You can specify a point outside the convex hull of your scattered data and will still not get a NaN.

  • Once created, the scatteredInterpolant object can be evaluated multiple times, thus saving computational time compared to calling griddata several times.

On the down side: While you can specify the interpolation and extrapolation methods, cubic is not available but only linear, nearest and natural.

Using scatteredInterpolant your code could look like

F = scatteredInterpolant(scatteredData(:,1),scatteredData(:,2),scatteredData(:,3));
ZI=F(points(:,1),points(:,2));
0

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

3
  • 1
    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.
    – user85109
    Sep 6, 2010 at 12:38
  • I'm not sure how the Qhull option can solve my problem, care to elaborate?
    – Graviton
    Sep 6, 2010 at 13:01
  • 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.
    – user85109
    Sep 6, 2010 at 13:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.