%% griddedInterpolant in Three Dimensions
% This example shows how to create a 3-D interpolant and evaluate over a slice plane so you can plot the values on that plane.
% Create a full grid and sample values.

[X,Y,Z] = ndgrid((-5:2:5));
V = X.^3 + Y.^2 + Z.^2;

%
% Construct the griddedInterpolant.

F = griddedInterpolant(X,Y,Z,V,'cubic');

%
% Since we already created the full grid to generate our sample values, we had nothing to lose in passing it to griddedInterpolant. In practice however, it's common to load the sample data into MATLAB from disk. The compact grid can very beneficial in such cases because it allows you to specify the entire grid in a form that is much more economical in terms of memory. If we had loaded V into MATLAB instead of calculating it from a full grid, we could have created a compact grid to conserve memory in our workspace. For example,

gv = {(-5:2:5),(-5:2:5),(-5:2:5)};
F = griddedInterpolant(gv,V,'cubic');

%
% Now interpolate over a plane at Z = 2 with 0.25 spacing.

[Xq,Yq,Zq] = ndgrid((-5:.25:5),(-5:.25:5),2:2);
Vq = F(Xq,Yq,Zq);

%
% Plot the result.

surf(Xq,Yq,Vq);
title('Refined with Linear Interpolation');

% Copyright 2012 The MathWorks, Inc.