%% Interpolate 4-D Data Set Over Grid
% Interpolate a 3-D slice of a 4-D function that is sampled at randomly scattered points.
% Sample a 4-D function v\left(x,y,z\right) at 2500 random points between -1 and 1. The vectors x, y, and z contain the nonuniform sample points.

x = 2*rand(2500,1) - 1; 
y = 2*rand(2500,1) - 1; 
z = 2*rand(2500,1) - 1;
v = x.^2 + y.^3 - z.^4;

%
% Define a regular grid with xy points in the range [-1, 1], and set z=0. Interpolating on this grid of 2-D query points (xq,yq,0) produces a 3-D interpolated slice (xq,yq,0,vq) of the 4-D data set (x,y,z,v).

d = -1:0.05:1;
[xq,yq,zq] = meshgrid(d,d,0);

%
% Interpolate the scattered data on the grid. Plot the results.

vq = griddata(x,y,z,v,xq,yq,zq);
plot3(x,y,v,'ro')
hold on
surf(xq,yq,vq)

%
% Copyright 2015 The MathWorks, Inc.