Why is interp2 producing NaN values?

61 views (last 30 days)
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)))

Accepted Answer

John D'Errico
John D'Errico on 13 Apr 2020
Edited: John D'Errico on 13 Apr 2020
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
  2 Comments
Mehmed Saad
Mehmed Saad on 13 Apr 2020
Great, i understand that too. thanks @John D'Errico
chris crowley
chris crowley on 13 Apr 2020
@John D'Errico Yes, I agree that in my OP I sloppily typed it such that I fall outside of the domain and your suggestiong to used griddedInterpolant is the way to go. However, for the benefit of future visitors to this post, I will mention that after fixing this and investigating it more closely, if the interp2 is asked to evaluate right on the boundary it also produces a NaN.

Sign in to comment.

More Answers (1)

Mehmed Saad
Mehmed Saad on 13 Apr 2020
I think it is because you are touching boundaries.
Try values below
xi = 1 + (500 - 1).*rand(1000,1);
yi = 1 + (500 - 1).*rand(1000,1);
  2 Comments
chris crowley
chris crowley on 13 Apr 2020
It seems that you are correct, however, the values that are throwing the NaN are not on the boundary, but very close to it. A value of 499.2 results in a NaN. This seems like something is not working properly to me..
Anyway, your suggestion is a good enough work around for me for now.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!