为什么feval会在MATLAB中返回NaN

Vir*_*nie 5 matlab interpolation spline curve-fitting

我在2D中有一堆点,我知道它的值,并且我想通过它们拟合三次样条来使用MATLAB插入其他一些数据.

我的代码看起来像:

fitobject = fit(x,y,'cubicinterp');
yy=feval(fitobject,xx)
Run Code Online (Sandbox Code Playgroud)

具有以下输入:

坐标

x = [...
   313     3;
   313     5;
   313     7;
   315     3;
   315     5;
   317     3;
   319     5];
Run Code Online (Sandbox Code Playgroud)

y = [...
   28.0779;
   28.0186;
   11.6220;
   16.7640;
   23.7139;
  -14.7882;
  -20.4626];
Run Code Online (Sandbox Code Playgroud)

插值点

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];
Run Code Online (Sandbox Code Playgroud)

在我的输出向量中yy,我得到了几个NaN值.对我来说,输入数据看起来很干净(它们都是有限值而且没有NaN).我不知道在拟合数据时会feval返回NaN什么.为什么它不能给出最好的适合度,即使它很糟糕?我的方法有错误吗?

我浏览了一下,似乎在mathworks论坛上已经多次问过同样的问题,但没有人给出明确的答案.

在此先感谢您的帮助.

Rod*_*uis 7

这是因为插值不能用作外推法:

  %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
Run Code Online (Sandbox Code Playgroud)

换句话说,您试图将数据超出原始表面数据的边界(外推),这通常已经非常危险,fit甚至不允许您这样做.


Hug*_*not 6

看起来像NaN的点位于插值之外.你可以绘制它来看一看.

三次插值

我以前使用的代码如下:(请注意,我将NaN设置为-25,以便可以绘制它们)

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)
Run Code Online (Sandbox Code Playgroud)

顺便说一句,请注意我没有使用feval,而是直接调用 fitobject

  • +1:总是+1为一个好的答案伴随着情节:) (2认同)