%% Compare Scattered Data Interpolation Methods
% Compare the results of several different interpolation algorithms offered by scatteredInterpolant.
%
% 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 the interpolant and a grid of query points.

F = scatteredInterpolant(x,y,v);
[xq,yq] = meshgrid(-3:0.1:3);

%
% Plot the results using the 'nearest', 'linear', and 'natural' methods. Each time the interpolation method changes, you need to requery the interpolant to get the updated results.

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

%

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

%

F.Method = 'natural';
vq3 = F(xq,yq);
figure
plot3(x,y,v,'mo')
hold on
mesh(xq,yq,vq3)
title('Natural Neighbor')
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.