I have a bunch of points in 2D for which I know the value, and I'd like to fit a cubic spline through them to interpolate some other data around using MATLAB.
My code looks like:
fitobject = fit(x,y,'cubicinterp');
yy=feval(fitobject,xx)
with the following inputs:
Coordinates
x = [...
313 3;
313 5;
313 7;
315 3;
315 5;
317 3;
319 5];
Values
y = [...
28.0779;
28.0186;
11.6220;
16.7640;
23.7139;
-14.7882;
-20.4626];
Interpolation points
xx = [...
313 3;
313 4;
313 5;
313 6;
313 7;
313 8;
313 9;
314 3;
314 5;
314 7;
315 3;
315 4;
315 5;
315 6;
315 7;
316 3;
316 5;
317 3;
317 4;
317 5;
318 3;
319 5;
319 6;
319 7;
320 5];
In my output vector yy
, I get several NaN
values. To me, the input data look clean (they are all finite values and there's no NaN
). I don't get what would cause feval
to return NaN
when fitting data. Why couldn't it give the best possible fit, even if it is bad? Is there an error in my approach?
I browsed a bit and it seems that the same question had been asked a bunch of times on mathworks forums, but no one gave a clear answer.
Thanks in advance for your help.
It's because an interpolation cannot be used as an extrapolation:
%xx(:,1) xx(:,2) yy
313.0000 3.0000 28.0779
313.0000 4.0000 29.5074
313.0000 5.0000 28.0186
313.0000 6.0000 22.3233
313.0000 7.0000 11.6220
313.0000 8.0000 NaN % xx exceeds bounds of original x interval
313.0000 9.0000 NaN % xx exceeds bounds of original x interval
314.0000 3.0000 24.1239
314.0000 5.0000 27.5130
314.0000 7.0000 NaN % xx exceeds bounds of original x interval
315.0000 3.0000 16.7640
315.0000 4.0000 21.7028
315.0000 5.0000 23.7139
315.0000 6.0000 11.2710
315.0000 7.0000 NaN % xx exceeds bounds of original x interval
316.0000 3.0000 1.4641
316.0000 5.0000 13.9662
317.0000 3.0000 -14.7882
317.0000 4.0000 -5.4876
317.0000 5.0000 2.7781
318.0000 3.0000 NaN % xx exceeds bounds of original x interval
319.0000 5.0000 -20.4626
319.0000 6.0000 NaN % xx exceeds bounds of original x interval
319.0000 7.0000 NaN % xx exceeds bounds of original x interval
320.0000 5.0000 NaN % xx exceeds bounds of original x interval
In other words, you're trying to get data beyond the boundaries of your original surface data (extrapolation), which is usually already quite dangerous, and fit
does not even allow you to do it.
It looks like the points which come up as NaN lie outside of the interpolation. You can plot it to take a look.
The code I used to play with this is as follows: (Note that I set the NaNs to -25 just so that they could be plotted)
x = [313 3;
313 5;
313 7;
315 3;
315 5;
317 3;
319 5];
y = [
28.0779
28.0186
11.6220
16.7640
23.7139
-14.7882
-20.4626];
fitobject = fit(x,y,'cubicinterp');
xx = [
313 3
313 4
313 5
313 6
313 7
313 8
313 9
314 3
314 5
314 7
315 3
315 4
315 5
315 6
315 7
316 3
316 5
317 3
317 4
317 5
318 3
319 5
319 6
319 7
320 5];
yy = fitobject(xx);
badindices = isnan(yy);
yy(badindices) = -25;
plot(fitobject, xx, yy, 'Exclude', badindices)
By the way, notice that I'm not using feval, but a direct call to fitobject
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With