Matlab interp2外推

Matlab interp2 extrapolation

我正在使用 interp2 进行二维插值。对于某些数据值, interp2 命令 returns NaN 因为其中一个维度在外面 由已知值向量定义的范围。

可以使用 interp1 命令进行推断。然而,是 interp2?

有办法做到这一点

谢谢

这是我使用 interp2 命令的代码:

function [Cla] = AirfoilLiftCurveSlope(obj,AFdata,Rc,M)

% Input:
% AFdata: Airfoil coordinates.
% Rc: Local Reynolds number.
% M: Mach number for Prandtle Glauert compressibility correction.

% Output: 
% Cla: 2 dimensional lift curve slopea applicable to linear region of lift polar.

load('ESDU84026a.mat');

xi = size(AFdata);

if mod(xi(1,1),2) == 0
    %number is even
    AFupper = flipud(AFdata(1:(xi(1,1)/2),:));
    AFlower = AFdata(((xi(1,1)/2)+1):end,:);
else
    %number is odd
    AFupper = flipud(AFdata(1:floor((xi(1,1)/2)),:));
    AFlower = AFdata((floor(xi(1,1)/2)+1):end,:);
end


t_c = Airfoil.calculateThickness(AFdata(:,2));

Y90 = ((interp1(AFupper(:,1),AFupper(:,2),0.9,'linear')) - (interp1(AFlower(:,1),AFlower(:,2),0.9,'linear')))*100;

Y99 = ((interp1(AFupper(:,1),AFupper(:,2),0.99,'linear')) - (interp1(AFlower(:,1),AFlower(:,2),0.99,'linear')))*100;

Phi_TE = (2 * atan( ( (Y90/2) - (Y99/2) )/9))*180/pi;                       % Degrees
Tan_Phi_Te = ( (Y90/2) - (Y99/2) )/9;

Cla_corr = interp2(Tan_Phi,Rc_cla,cla_ratio,Tan_Phi_Te,Rc,'linear');

beta =sqrt((1-M^2));                                                        % Prandtle Glauert correction
Cla_theory = 2*pi + 4.7*t_c*(1+0.00375 * Phi_TE);                           % per rad 
Cla = (1.05/beta) * Cla_corr * Cla_theory;                                  % per rad

if isnan(Cla) == 1 %|| Cla > 2*pi
    Cla = 2*pi;
end

end

是的,根据the docs.

有两种方法可以让interp2到return一个有意义的值越界
  1. 使用'spline'插值法。与选项 #2 不同,这实际上会根据样条曲线的边界条件推断数据。
  2. 指定最后一个 extrapval 参数。对于所有其他插值方法,此常量将被 returned 而不是 NaN

不幸的是,似乎没有办法指定 "nearest neighbor on the grid" 之类的内容。如果越界元素靠近边缘,也许您可​​以扩展输入数组。例如像这样:

x = [x(1, 1), x(1, :), x(1, end); ...
     x(:, 1), x, x(:, end); ...
     x(end, 1), x(end, :), x(end, end)]

嘿,请找到我的 interp2 代码,它只需要最大边界值;

function vq = Linear2dInterpWithClipExtrap(x,y,v,xq,yq);

    vq = interp2(x,y,v,xq,yq);

    [XMax, idxVMax] = max(x);
    [XMin, idxVMin] = min(x);

    idxMax = xq > XMax;
    idxMin = xq < XMin;
   if ~isempty(yq(idxMax));
    vq(idxMax) = LinearInterpWithClipExtrap(y,v(:,idxVMax),yq(idxMax));
   end
   if ~ isempty(yq(idxMin))
    vq(idxMin) = LinearInterpWithClipExtrap(y,v(:,idxVMin),yq(idxMin));
   end

   [YMax, idyVMax] = max(y);
    [YMin, idyVMin] = min(y);

    idyMax = yq > YMax;
    idyMin = yq < YMin;
   if ~isempty(xq(idyMax));
    vq(idyMax) = LinearInterpWithClipExtrap(x,v(idyVMax,:),xq(idyMax));
   end
   if ~ isempty(xq(idyMin));
    vq(idyMin) = LinearInterpWithClipExtrap(x,v(idyVMin,:),xq(idyMin));
   end



function vq = LinearInterpWithClipExtrap(x,v,xq);

    vq = interp1(x,v,xq);

    [XMax, idxVMax] = max(x);
    [XMin, idxVMin] = min(x);

    idxMax = xq > XMax;
    idxMin = xq < XMin;

    vq(idxMax) = v(idxVMax);
    vq(idxMin) = v(idxVMin

);