Search code examples
matlabinterpolationextrapolation

How to interpolate and extrapolate non-monotonic vector data set in Matlab


I have a geographically distributed data set with X-coordinate, Y-coordinate and corresponding target value of interest D. That is, my data set consists from three vectors: X, Y, D.

Now what I would like to do, is interpolate and extrapolate the target variable D over a coordinate grid of interest. The griddata-function in Matlab seems to be able to help me in this problem, but it only does interpolation over the convex hull determined by my data set.

What I would like to do, is to also extrapolate the data D to any rectangular coordinate grid of interest like so:

enter image description here

I have tried using functions like interp2 and griddedInterpolant, but these functions seem to require that I provide the known data as monotonic matrices (using e.g. meshgrid). That is, if I have understood correctly, I must provide X,Y,D as 2D-grids. But they are not grids, they are non-monotonic vectors.

So how can I proceed?


Solution

  • I found out one way using scatteredInterpolant:

    xy = -2.5 + 5*gallery('uniformdata',[200 2],0);
    x = xy(:,1);
    y = xy(:,2);
    v = x.*exp(-x.^2-y.^2);
    F1 = scatteredInterpolant(x,y,v, 'natural');
    [xq,yq] = ndgrid(-5:.1:5) % Make the grid
    vq1 = F1(xq,yq); % Evaluate function values at grid of interest
    surf(xq,yq,vq1)
    hold on
    plot3(x,y,v, 'ro', 'MarkerFaceColor', 'red')
    xlabel('X')
    ylabel('Y')
    zlabel('V')
    title('Interpolation and exrapolation based on scattered data')
    

    enter image description here

    The problem is, you can do extrapolation with only three methods: 'linear', 'nearest', 'natural'.