griddata

Interpolate scattered data

 Note:   Qhull-specific options are no longer supported. Remove the `OPTIONS` argument from all instances in your code that pass it to `griddata`.In a future release, the following syntaxes will be removed:```[Xq,Yq,Vq] = griddata(x,y,v,xq,yq) [Xq,Yq,Vq] = griddata(x,y,v,xq,yq, `method`)```In addition, `griddata` will not accept any input vectors of mixed orientation in a future release. To specify a grid of query points, construct a full grid with `ndgrid` or `meshgrid` before calling `griddata`.

Syntax

`vq = griddata(x,y,v,xq,yq)vq = griddata(x,y,z,v,xq,yq,zq)vq = griddata(___,method)`

Description

`vq = griddata(x,y,v,xq,yq)` fits a surface of the form v = f(x,y) to the scattered data in the vectors `(x,y,v)`. The `griddata` function interpolates the surface at the query points specified by `(xq,yq)` and returns the interpolated values, `vq`. The surface always passes through the data points defined by `x` and `y`.

`vq = griddata(x,y,z,v,xq,yq,zq)` fits a hypersurface of the form v = f(x,y,z).

`vq = griddata(___,method)` uses a specified interpolation method to compute `vq` using any of the input arguments in the previous syntaxes.

Input Arguments

`x`

Vector specifying the x- coordinates of the sample points.

`y`

Vector specifying the y- coordinates of the sample points.

`z`

Vector specifying the z- coordinates of the sample points.

`v`

Vector of sample values that correspond to the sample coordinates `x`, `y` (and `z` for 3-D interpolation).

`xq`

Vector or array that specifies x- coordinates of the query points to be evaluated. `xq` must be the same size as `yq` (and `zq` for 3-D interpolation).

`yq`

Vector or array that specifies y- coordinates of the query points to be evaluated. `yq` must be the same size as `xq` (and `zq` for 3-D interpolation).

`zq`

Vector or array that specifies z- coordinates of the query points to be evaluated. `zq` must be the same size as `xq` and `yq`.

`method`

Keyword that specifies the interpolation method. Use one of the following:

method stringDescriptionContinuity
`'nearest'`Triangulation-based nearest neighbor interpolation supporting 2-D and 3-D interpolation.Discontinuous
`'linear'`Triangulation-based linear interpolation (default) supporting 2-D and 3-D interpolationC0
`'natural'`Triangulation-based natural neighbor interpolation supporting 2-D and 3-D interpolation. This method is an efficient tradeoff between linear and cubic.C1 except at sample points
`'cubic'`Triangulation-based cubic interpolation supporting 2-D interpolation onlyC2
`'v4'`

Biharmonic spline interpolation (MATLAB® 4 `griddata` method) supporting 2-D interpolation only. Unlike the other methods, this interpolation is not based on a triangulation.

C2

Output Arguments

 `vq` The interpolated values at the query points. For 2-D interpolation, where `xq` and `yq` specify an `m`-by-`n` grid of query points, `vq` is an `m`-by-`n` array.For 3-D interpolation, where `xq`, `yq`, and `zq` specify an `m`-by-`n`-by-`p` grid of query points, `vq` is an `m`-by-`n`-by-`p` array.If `xq`, `yq`, (and `zq` for 3-D interpolation) are vectors that specify scattered points, `vq` is a vector of the same length.

Examples

collapse all

Interpolate Scattered Data Over a Uniform Grid

Sample a function at 200 random points between `-2.5` and `2.5`.

```xy = -2.5 + 5*gallery('uniformdata',[200 2],0); x = xy(:,1); y = xy(:,2); v = x.*exp(-x.^2-y.^2); ```

`x`, `y`, and `v` are vectors containing scattered (nonuniform) sample points and data.

Define a regular grid and interpolate the scattered data over the grid.

```[xq,yq] = meshgrid(-2:.2:2, -2:.2:2); vq = griddata(x,y,v,xq,yq); ```

Plot the gridded data as a mesh and the scattered data as dots.

```figure mesh(xq,yq,vq); hold on plot3(x,y,v,'o'); h = gca; h.XLim = [-2.7 2.7]; h.YLim = [-2.7 2.7]; ```

Interpolate 3-D Data Set Over a Grid in the x-y Plane

Sample a function at 5000 random points between `-1` and `1`. Initialize the random number generator to make the output of `randn` repeatable.

```rng default x = 2*rand(5000,1) - 1; y = 2*rand(5000,1) - 1; z = 2*rand(5000,1) - 1; v = x.^2 + y.^2 + z.^2; ```

`x`, `y`, and `z` are now vectors containing nonuniformly sampled data.

Define a regular grid with points in the range [-0.8, 0.8].

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

Interpolate the scattered data over a rectangular region at `z=0`. Then, plot the results.

```vq = griddata(x,y,z,v,xq,yq,zq); surf(xq,yq,vq); h = gca; h.XTick = [-1 -0.5 0 0.5 1]; h.YTick = [-1 -0.5 0 0.5 1]; ```