%% Interpolate Multiple Sets of Values on Same Grid
% Use griddedInterpolant to interpolate three different sets of values at the same query points.
%
% Create a grid of sample points with -5\le X\le 5 and -3\le Y\le 3.

gx = -5:5;
gy = -3:3;
[X,Y] = ndgrid(gx,gy);

%
% Evaluate three different functions at the query points, and then concatenate the values into a 3-D array. The size of V is the same as the X and Y grids in the first two dimensions, but the size of the extra dimension reflects the number of values associated with each sample point (in this case, three).

f1 = X.^2 + Y.^2;
f2 = X.^3 + Y.^3;
f3 = X.^4 + Y.^4;
V = cat(3,f1,f2,f3);

%
% Create an interpolant using the sample points and associated values.

F = griddedInterpolant(X,Y,V);

%
% Create a grid of query points with a finer mesh size compared to the sample points.

qx = -5:0.4:5;
qy = -3:0.4:3;
[XQ,YQ] = ndgrid(qx,qy);

%
% Interpolate all three sets of values at the query points.

VQ = F(XQ,YQ);

%
% Compare the original data with the interpolated results.

tiledlayout(3,2)
nexttile
surf(X,Y,f1)
title('f1')
nexttile
surf(XQ,YQ,VQ(:,:,1))
title('Interpolated f1')
nexttile
surf(X,Y,f2)
title('f2')
nexttile
surf(XQ,YQ,VQ(:,:,2))
title('Interpolated f2')
nexttile
surf(X,Y,f3)
title('f3')
nexttile
surf(XQ,YQ,VQ(:,:,3))
title('Interpolated f3')

%
% Copyright 2020 The MathWorks, Inc.