MATLAB: Is interp2 producing NaN values

interp2nan values

It seems that interp2 produces NaN values on smooth, well-behaved functions. What am I doing wrong? For sure I am making a silly mistake.
Example code:
% define a constant value matrix of 500x500
x = 1:500;
y = 1:500;
[X,Y] = meshgrid(x,y);
f = ones(size(X));
% produce random locations to interpolate f at between 0 and 500
xi = 0 + (500 - 0).*rand(1000,1);
yi = 0 + (500 - 0).*rand(1000,1);
% run interp2
f_interp = interp2(X,Y,f,xi,yi);
% this value should be zero, but I am getting something like 5
sum(sum(isnan(f_interp)))

Best Answer

It is wrong tha the OP code was touching the cboundary. It was that the op code was EXCEEDING the boundaries.
The rand statements, as you did show will sometimes produce a number that was below 1, yet the meshgrid ran form 1 to 500.
When faced with a point outside of the boundary, interp2 does NOT extrapolate. it returns a NaN result, as you saw.
[X,Y] = meshgrid(1:20,1:20);
V = (X-5).^2 + Y.^2;
interp2(X,Y,V,0,0)
ans =
NaN
However, you can use griddedInterpolant, which prefers its grid produced by ndgrid, instead of meshgrid. It is just as simple to transpose the data however.
F = griddedInterpolant(X',Y',V')
F =
griddedInterpolant with properties:
GridVectors: {[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20] [1×20 double]}
Values: [20×20 double]
Method: 'linear'
ExtrapolationMethod: 'linear'
As you can see now, it does work, no longer producing NaNs when extrapolating.
F(10,12)
ans =
169
F(0,0)
ans =
21
I could have turned off extrapolation with griddedInterpolant, IF I wanted that behaviour, but you are now given the choice to control this behavior.
F.ExtrapolationMethod = 'none';
F(117,-134435)
ans =
NaN
Beware the dangers of extrapolation however, as an extrapolant can easily predict meaningless and strange results. If you expect to be ding extrapolation, my recommendation is to never try to extrapolate too far, and if you will do so anyway, use a simple interpolation scheme, such as linear.
F2spl = griddedInterpolant(X',Y',V','spline');
F2spl(-3,-3)
ans =
73
F2lin = griddedInterpolant(X',Y',V','linear');
F2lin(-3,-3)
ans =
33
As you can see, the spline extrapolant will be a little more wild when asked to extrapolate. However, if I go far outside, the linear method is more well behaved.
F2spl(-300,-300)
ans =
183025
F2lin(-300,-300)
ans =
1221
We can even change the behavior in extrpolation however, while leaving things the same when a point is interior to the region.
F2spl.ExtrapolationMethod = 'linear'
F2spl =
griddedInterpolant with properties:
GridVectors: {[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20] [1×20 double]}
Values: [20×20 double]
Method: 'spline'
ExtrapolationMethod: 'linear'
F2spl(-300,-300)
ans =
1221