%% Comparison of Scattered Data Interpolation Methods
% Compare the results of several different interpolation algorithms offered by griddata.
%
% Create a sample data set of 50 scattered points. The number of points is artificially small to highlight the differences between the interpolation methods.

x = -3 + 6*rand(50,1);
y = -3 + 6*rand(50,1);
v = sin(x).^4 .* cos(y);

%
% Create a grid of query points.

[xq,yq] = meshgrid(-3:0.1:3);

%
% Interpolate the sample data using the 'nearest', 'linear', 'natural', and 'cubic' methods. Plot the results for comparison.

z1 = griddata(x,y,v,xq,yq,'nearest');
plot3(x,y,v,'mo')
hold on
mesh(xq,yq,z1)
title('Nearest Neighbor')
legend('Sample Points','Interpolated Surface','Location','NorthWest')

%

z2 = griddata(x,y,v,xq,yq,'linear');
figure
plot3(x,y,v,'mo')
hold on
mesh(xq,yq,z2)
title('Linear')
legend('Sample Points','Interpolated Surface','Location','NorthWest')

%

z3 = griddata(x,y,v,xq,yq,'natural');
figure
plot3(x,y,v,'mo')
hold on
mesh(xq,yq,z3)
title('Natural Neighbor')
legend('Sample Points','Interpolated Surface','Location','NorthWest')

%

z4 = griddata(x,y,v,xq,yq,'cubic');
figure
plot3(x,y,v,'mo')
hold on
mesh(xq,yq,z4)
title('Cubic')
legend('Sample Points','Interpolated Surface','Location','NorthWest')

%
% Plot the exact solution.

figure
plot3(x,y,v,'mo')
hold on
mesh(xq,yq,sin(xq).^4 .* cos(yq))
title('Exact Solution')
legend('Sample Points','Exact Surface','Location','NorthWest')

% Copyright 2012 The MathWorks, Inc.