I am having a problem with TriScatteredInterp in MatLab.
I am using a set of coordinate points with the corresponding temperature at that location. They are all in degrees in the form (long, lat, temp). I want to make an interpolant on these points so I can find out the values at other points and build a grid.
This is what I have done so far:
long = data(:,1)
lat = data(:,2)
values = data(:,3)
lat = lat.*(pi/180)
long = long.*(pi/180)
X = cos(lat).*cos(long)
Y = cos(lat).*sin(long)
Z = sin(lat)
F = TriScatteredInterp(X,Y,Z,values)
[long1 lat1] = meshgrid(-pi:pi/360:pi, -pi/2:pi/360:pi/2);
X1 = cos(lat1).*cos(long1)
Y1 = cos(lat1).*sin(long1)
Z1 = sin(lat1);
F.Method = 'natural'
InterpVals = F(X1,Y1,Z1);
mesh(long1, lat1, InterpVals)
As you can see for every (long, lat) point, I have computed the corresponding point on the sphere and have used the 3d version of TriScatteredInterp.
The problem is that the interpolation only works for the ‘nearest’ method, as for the linear or natural is producing just NaN’s. As I have read this happens when the points that I want to interpolate are outside of the convex hull of the triangulation, but as the points needed are exactly on the sphere, and the input points are covering the entire range (Long : -180 to 180, Lat : -90 to 90), I just don’t see how all the points could be outside the convex hull. Any help will be appreciated , ty.
You should interpolate values on the bi-dimensional original data (long, lat), not on the tri-dimensional one (X, Y, Z).
Note that I included some dummy data generator, for the readers that do not have access to your data()!
There is still an issue on the edges of the map, as the interpolator doesn’t know that the data “wraps” around. The content of InterpVals on those edges will be.. NaN!
Edit: suggestions for the wrapping:
1) rewrite TriScatteredInterp so that it uses modulos;
2) mirror the data around the “edges” of the map, interpolate, then crop it back to original size;
3) check out the Matlab Mapping Toolbox, which analyze and visualize geographic information.