interpolate 2d data in matlab

To interpolate 2D data in Matlab, there are several options. One of the most commonly used functions is griddata. Given a set of scattered data points and their values, griddata interpolates the values at requested query points using various interpolation methods.

Here's an example of how to use griddata:

main.m
% set up some example data
x = rand(100,1)*10;
y = rand(100,1)*10;
v = sin(x).*cos(y);

% define query points on a regular grid
[X,Y] = meshgrid(0:0.25:10, 0:0.25:10);

% interpolate using griddata
Vq = griddata(x,y,v,X,Y);

% plot the original data and the interpolated data
figure;
subplot(1,2,1);
scatter(x,y,10,v,'filled');
xlabel('x'); ylabel('y'); title('Original Data');
subplot(1,2,2);
pcolor(X,Y,Vq); shading interp;
xlabel('x'); ylabel('y'); title('Interpolated Data');
480 chars
20 lines

Another useful function for 2D interpolation is scatteredInterpolant, which creates an interpolant object from scattered data that can be used to interpolate at query points using various interpolation methods.

Here's an example of how to use scatteredInterpolant:

main.m
% set up some example data
x = rand(100,1)*10;
y = rand(100,1)*10;
v = sin(x).*cos(y);

% create the interpolant object using scatteredInterpolant
F = scatteredInterpolant(x, y, v);

% define query points on a regular grid
[X,Y] = meshgrid(0:0.25:10, 0:0.25:10);

% interpolate using the interpolant object
Vq = F(X,Y);

% plot the original data and the interpolated data
figure;
subplot(1,2,1);
scatter(x,y,10,v,'filled');
xlabel('x'); ylabel('y'); title('Original Data');
subplot(1,2,2);
pcolor(X,Y,Vq); shading interp;
xlabel('x'); ylabel('y'); title('Interpolated Data');
576 chars
23 lines

gistlibby LogSnag