Matlab interp2 extrapolation

Balraj Boyal picture Balraj Boyal · Jan 28, 2016 · Viewed 7.6k times · Source

I am doing a 2-D interpolation using interp2. For some data values, the interp2 command returns NaN because one of the dimensions are outside of the range defined by the vector of known values.

Its possible to extrapolate with the interp1 command. However, Is there a way to do this for interp2?

Thanks

Here is the code in which I am using the interp2 command:

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

Answer

Mad Physicist picture Mad Physicist · Jan 28, 2016

Yes, there are two ways to get interp2 to return a meaningful value out of bounds according to the docs.

  1. Use the 'spline' interpolation method. Unlike option #2, this will actually extrapolate the data based on the boundary conditions of the spline.
  2. Specify a final extrapval parameter. This constant will be returned instead of NaN for all other interpolation methods.

Unfortunately, there does not appear to be a way to specify something like "nearest neighbor on the grid" or something like that. If the out-of bounds elements are close to the edges, perhaps you could just expand the input array. For example like this:

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