MATLAB: Linear Extrapolation with interp2

interp2MATLAB

Matlab's griddedInterpolant allows for linear interpolation with linear extrapolation in 2D space, i.e.,
griddedInterpolant(X,Y,V,'linear','linear');
However, it seems to be the case that interp2, which I believe uses griddedInterpolant under-the-hood, doesn't allow for this combination. If "linear" is selected as the interpolation method, then the extrapolation must be a scalar. Is there a way to use linear-linear inter-extrapolation with interp2?

Best Answer

The only remedy I can think of is to pre-pad the edges of the data V with linearly extrapolated values at distant X, Y. That way, you aren't doing any extrapolation anymore. All of your query points xq,yq will fall within the boundaries of the padded data, assuming you make the padded, X,Y distant enough to enclose them.
[V,x]=linearpad(V,x);
[V,x]=linearpad(fliplr(V),flip(x)); V=fliplr(V);
V=V.';
[V,y]=linearpad(V,y);
[V,y]=linearpad(fliplr(V),flip(y)); V=fliplr(V);
V=V.';
Vq=interp2(x,y,V,xq,yq,'linear');
function [D,z]=linearpad(D0,z0)
factor=1e6;
dz=z0(end)-z0(end-1);
dD=D(:,end)-D(:,end-1);
z=z0;
z(end+1)=z(end)+factor*dz;
D=D0;
D(:,end+1)=D(:,end) + factor*dD;
end