www.gusucode.com > matlab 案例源码 matlab代码程序 > matlab/InterpolatingScatteredDataUsingTheScatteredInterpolantClExample.m

    %% Interpolating Scattered Data Using the scatteredInterpolant Class
% This example shows how to use |scatteredInterpolant| to interpolate a
% scattered sampling of the |peaks| function.

% Copyright 2015 The MathWorks, Inc.


%% Create the scattered data set.
X = -3 + 6.*gallery('uniformdata',[250 2],0);
V = peaks(X(:,1),X(:,2));

%% Create the interpolant.
F = scatteredInterpolant(X,V)

%%
% The |Points| property represents the coordinates of the data points, and
% the |Values| property represents the associated values. The |Method|
% property represents the interpolation method that performs the
% interpolation. The |ExtrapolationMethod| property represents the
% extrapolation method used when query points fall outside the convex hull.

%%
% You can access the properties of |F| in the same way you access the
% fields of a |struct|. For example, use |F.Points| to examine the coordinates of the data points.

%% Evaluate the interpolant. 
% |scatteredInterpolant| provides subscripted evaluation of the
% interpolant.  It is evaluated the same way as a function.  You can
% evaluate the interpolant as follows. In this case, the value at the query
% location is given by |Vq|. You can evaluate at a single query point:
Vq = F([1.5 1.25])

%%
% You can also pass individual coordinates:
Vq = F(1.5, 1.25)

%%
% You can evaluate at a vector of point locations:
Xq = [0.5 0.25; 0.75 0.35; 1.25 0.85];
Vq = F(Xq)

%%
% You can evaluate |F| at grid point locations and plot the result.
[Xq,Yq] = meshgrid(-2.5:0.125:2.5);
Vq = F(Xq,Yq);
surf(Xq,Yq,Vq);
xlabel('X','fontweight','b'), ylabel('Y','fontweight','b');
zlabel('Value - V','fontweight','b');
title('Linear Interpolation Method','fontweight','b');

%% Change the interpolation method.
% You can change the interpolation method on the fly. Set the method to
% |'nearest'|.
F.Method = 'nearest';

%%
% Reevaluate and plot the interpolant as before.
Vq = F(Xq,Yq);
figure
surf(Xq,Yq,Vq);
xlabel('X','fontweight','b'),ylabel('Y','fontweight','b') 
zlabel('Value - V','fontweight','b')
title('Nearest neighbor Interpolation Method','fontweight','b');

%%
% Change the interpolation method to natural neighbor, reevaluate, and plot the results.
F.Method = 'natural';
Vq = F(Xq,Yq);
figure
surf(Xq,Yq,Vq);
xlabel('X','fontweight','b'),ylabel('Y','fontweight','b') 
zlabel('Value - V','fontweight','b')
title('Natural neighbor Interpolation Method','fontweight','b');

%% Replace the values at the sample data locations. 
% You can change the values |V| at the sample data locations, |X|, on the
% fly. This is useful in practice as some interpolation problems may have
% multiple sets of values at the same locations. For example, suppose you
% want to interpolate a 3-D velocity field that is defined by locations
% (|x|, |y|, |z|) and corresponding componentized velocity vectors (|Vx|,
% |Vy|, |Vz|). You can interpolate each of the velocity components by
% assigning them to the values property (|V|) in turn.
% This has important performance benefits, because it allows you to reuse
% the same interpolant without incurring the overhead of computing a new
% one each time.

%%
% The following steps show how to change the values in our example. You
% will compute the values using the expression, $v = xe^{-x^{2}-y^{2}}$.
V = X(:,1).*exp(-X(:,1).^2-X(:,2).^2);
F.Values = V;

%%
% Evaluate the interpolant and plot the result.
Vq = F(Xq,Yq);
figure
surf(Xq,Yq,Vq);
xlabel('X','fontweight','b'), ylabel('Y','fontweight','b') 
zlabel('Value - V','fontweight','b')
title('Natural neighbor interpolation of v = x.*exp(-x.^2-y.^2)')

%% Add additional point locations and values to the existing interpolant.
% This performs an efficient update as opposed to a complete recomputation
% using the augmented data set.

%%
% When adding sample data, it is important to add both the point locations and the corresponding values.

%%
% Continuing the example, create new sample points as follows:
X = -1.5 + 3.*rand(100,2);
V = X(:,1).*exp(-X(:,1).^2-X(:,2).^2);

%%
% Add the new points and corresponding values to the triangulation.
F.Points(end+(1:100),:) = X;
F.Values(end+(1:100)) = V;

%%
% Evaluate the refined interpolant and plot the result.
Vq = F(Xq,Yq);
figure
surf(Xq,Yq,Vq);
xlabel('X','fontweight','b'), ylabel('Y','fontweight','b');
zlabel('Value - V','fontweight','b');

%% Remove data from the interpolant.
% You can incrementally remove sample data points from the interpolant. You also can remove data points and corresponding values from the interpolant. This is useful for removing spurious outliers.

%%
% When removing sample data, it is important to remove both the point location and the corresponding value.

%%
% Remove the 25th point.
F.Points(25,:) = [];
F.Values(25) = [];

%%
% Remove points 5 to 15.
F.Points(5:15,:) = [];
F.Values(5:15) = [];

%%
% Retain 150 points and remove the rest.
F.Points(150:end,:) = [];
F.Values(150:end) = [];

%%
% This creates a coarser surface when you evaluate and plot:
Vq = F(Xq,Yq);
figure
surf(Xq,Yq,Vq);
xlabel('X','fontweight','b'), ylabel('Y','fontweight','b');
zlabel('Value - V','fontweight','b');
title('Interpolation of v = x.*exp(-x.^2-y.^2) with sample points removed')