%% Interpolate Scattered Data Over Uniform Grid
% Interpolate randomly scattered data on a uniform grid of query points.
% Sample a function at 200 random points between -2.5 and 2.5.

rng('default')
xy = -2.5 + 5*rand([200 2]);
x = xy(:,1);
y = xy(:,2);
v = x.*exp(-x.^2-y.^2);

% x, y, and v are vectors containing scattered (nonuniform) sample points and data.
%
% Define a regular grid and interpolate the scattered data over the grid.

[xq,yq] = meshgrid(-2:.2:2, -2:.2:2);
vq = griddata(x,y,v,xq,yq);

%
% Plot the gridded data as a mesh and the scattered data as dots.

mesh(xq,yq,vq)
hold on
plot3(x,y,v,'o')
xlim([-2.7 2.7])
ylim([-2.7 2.7])

% Copyright 2015 The MathWorks, Inc.