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(sc开发者_JAVA技巧atteredData(:,1),scatteredData(:,2),scatteredData(:,3),points(:,1),points(:,2), 'cubic',{'QJ'});
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.
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 callinggriddata
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));
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
精彩评论