<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <!-- Created by GNU Texinfo 6.5, http://www.gnu.org/software/texinfo/ --> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Multi-dimensional Interpolation (GNU Octave (version 5.1.0))</title> <meta name="description" content="Multi-dimensional Interpolation (GNU Octave (version 5.1.0))"> <meta name="keywords" content="Multi-dimensional Interpolation (GNU Octave (version 5.1.0))"> <meta name="resource-type" content="document"> <meta name="distribution" content="global"> <meta name="Generator" content="makeinfo"> <link href="index.html#Top" rel="start" title="Top"> <link href="Concept-Index.html#Concept-Index" rel="index" title="Concept Index"> <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents"> <link href="Interpolation.html#Interpolation" rel="up" title="Interpolation"> <link href="Geometry.html#Geometry" rel="next" title="Geometry"> <link href="One_002ddimensional-Interpolation.html#One_002ddimensional-Interpolation" rel="prev" title="One-dimensional Interpolation"> <style type="text/css"> <!-- a.summary-letter {text-decoration: none} blockquote.indentedblock {margin-right: 0em} blockquote.smallindentedblock {margin-right: 0em; font-size: smaller} blockquote.smallquotation {font-size: smaller} div.display {margin-left: 3.2em} div.example {margin-left: 3.2em} div.lisp {margin-left: 3.2em} div.smalldisplay {margin-left: 3.2em} div.smallexample {margin-left: 3.2em} div.smalllisp {margin-left: 3.2em} kbd {font-style: oblique} pre.display {font-family: inherit} pre.format {font-family: inherit} pre.menu-comment {font-family: serif} pre.menu-preformatted {font-family: serif} pre.smalldisplay {font-family: inherit; font-size: smaller} pre.smallexample {font-size: smaller} pre.smallformat {font-family: inherit; font-size: smaller} pre.smalllisp {font-size: smaller} span.nolinebreak {white-space: nowrap} span.roman {font-family: initial; font-weight: normal} span.sansserif {font-family: sans-serif; font-weight: normal} ul.no-bullet {list-style: none} --> </style> <link rel="stylesheet" type="text/css" href="octave.css"> </head> <body lang="en"> <a name="Multi_002ddimensional-Interpolation"></a> <div class="header"> <p> Previous: <a href="One_002ddimensional-Interpolation.html#One_002ddimensional-Interpolation" accesskey="p" rel="prev">One-dimensional Interpolation</a>, Up: <a href="Interpolation.html#Interpolation" accesskey="u" rel="up">Interpolation</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p> </div> <hr> <a name="Multi_002ddimensional-Interpolation-1"></a> <h3 class="section">29.2 Multi-dimensional Interpolation</h3> <p>There are three multi-dimensional interpolation functions in Octave, with similar capabilities. Methods using Delaunay tessellation are described in <a href="Interpolation-on-Scattered-Data.html#Interpolation-on-Scattered-Data">Interpolation on Scattered Data</a>. </p> <a name="XREFinterp2"></a><dl> <dt><a name="index-interp2"></a><em><var>zi</var> =</em> <strong>interp2</strong> <em>(<var>x</var>, <var>y</var>, <var>z</var>, <var>xi</var>, <var>yi</var>)</em></dt> <dt><a name="index-interp2-1"></a><em><var>zi</var> =</em> <strong>interp2</strong> <em>(<var>z</var>, <var>xi</var>, <var>yi</var>)</em></dt> <dt><a name="index-interp2-2"></a><em><var>zi</var> =</em> <strong>interp2</strong> <em>(<var>z</var>, <var>n</var>)</em></dt> <dt><a name="index-interp2-3"></a><em><var>zi</var> =</em> <strong>interp2</strong> <em>(<var>z</var>)</em></dt> <dt><a name="index-interp2-4"></a><em><var>zi</var> =</em> <strong>interp2</strong> <em>(…, <var>method</var>)</em></dt> <dt><a name="index-interp2-5"></a><em><var>zi</var> =</em> <strong>interp2</strong> <em>(…, <var>method</var>, <var>extrap</var>)</em></dt> <dd> <p>Two-dimensional interpolation. </p> <p>Interpolate reference data <var>x</var>, <var>y</var>, <var>z</var> to determine <var>zi</var> at the coordinates <var>xi</var>, <var>yi</var>. The reference data <var>x</var>, <var>y</var> can be matrices, as returned by <code>meshgrid</code>, in which case the sizes of <var>x</var>, <var>y</var>, and <var>z</var> must be equal. If <var>x</var>, <var>y</var> are vectors describing a grid then <code>length (<var>x</var>) == columns (<var>z</var>)</code> and <code>length (<var>y</var>) == rows (<var>z</var>)</code>. In either case the input data must be strictly monotonic. </p> <p>If called without <var>x</var>, <var>y</var>, and just a single reference data matrix <var>z</var>, the 2-D region <code><var>x</var> = 1:columns (<var>z</var>), <var>y</var> = 1:rows (<var>z</var>)</code> is assumed. This saves memory if the grid is regular and the distance between points is not important. </p> <p>If called with a single reference data matrix <var>z</var> and a refinement value <var>n</var>, then perform interpolation over a grid where each original interval has been recursively subdivided <var>n</var> times. This results in <code>2^<var>n</var>-1</code> additional points for every interval in the original grid. If <var>n</var> is omitted a value of 1 is used. As an example, the interval [0,1] with <code><var>n</var>==2</code> results in a refined interval with points at [0, 1/4, 1/2, 3/4, 1]. </p> <p>The interpolation <var>method</var> is one of: </p> <dl compact="compact"> <dt><code>"nearest"</code></dt> <dd><p>Return the nearest neighbor. </p> </dd> <dt><code>"linear"</code> (default)</dt> <dd><p>Linear interpolation from nearest neighbors. </p> </dd> <dt><code>"pchip"</code></dt> <dd><p>Piecewise cubic Hermite interpolating polynomial—shape-preserving interpolation with smooth first derivative. </p> </dd> <dt><code>"cubic"</code></dt> <dd><p>Cubic interpolation (same as <code>"pchip"</code>). </p> </dd> <dt><code>"spline"</code></dt> <dd><p>Cubic spline interpolation—smooth first and second derivatives throughout the curve. </p></dd> </dl> <p><var>extrap</var> is a scalar number. It replaces values beyond the endpoints with <var>extrap</var>. Note that if <var>extrap</var> is used, <var>method</var> must be specified as well. If <var>extrap</var> is omitted and the <var>method</var> is <code>"spline"</code>, then the extrapolated values of the <code>"spline"</code> are used. Otherwise the default <var>extrap</var> value for any other <var>method</var> is <code>"NA"</code>. </p> <p><strong>See also:</strong> <a href="One_002ddimensional-Interpolation.html#XREFinterp1">interp1</a>, <a href="#XREFinterp3">interp3</a>, <a href="#XREFinterpn">interpn</a>, <a href="Three_002dDimensional-Plots.html#XREFmeshgrid">meshgrid</a>. </p></dd></dl> <a name="XREFinterp3"></a><dl> <dt><a name="index-interp3"></a><em><var>vi</var> =</em> <strong>interp3</strong> <em>(<var>x</var>, <var>y</var>, <var>z</var>, <var>v</var>, <var>xi</var>, <var>yi</var>, <var>zi</var>)</em></dt> <dt><a name="index-interp3-1"></a><em><var>vi</var> =</em> <strong>interp3</strong> <em>(<var>v</var>, <var>xi</var>, <var>yi</var>, <var>zi</var>)</em></dt> <dt><a name="index-interp3-2"></a><em><var>vi</var> =</em> <strong>interp3</strong> <em>(<var>v</var>, <var>n</var>)</em></dt> <dt><a name="index-interp3-3"></a><em><var>vi</var> =</em> <strong>interp3</strong> <em>(<var>v</var>)</em></dt> <dt><a name="index-interp3-4"></a><em><var>vi</var> =</em> <strong>interp3</strong> <em>(…, <var>method</var>)</em></dt> <dt><a name="index-interp3-5"></a><em><var>vi</var> =</em> <strong>interp3</strong> <em>(…, <var>method</var>, <var>extrapval</var>)</em></dt> <dd> <p>Three-dimensional interpolation. </p> <p>Interpolate reference data <var>x</var>, <var>y</var>, <var>z</var>, <var>v</var> to determine <var>vi</var> at the coordinates <var>xi</var>, <var>yi</var>, <var>zi</var>. The reference data <var>x</var>, <var>y</var>, <var>z</var> can be matrices, as returned by <code>meshgrid</code>, in which case the sizes of <var>x</var>, <var>y</var>, <var>z</var>, and <var>v</var> must be equal. If <var>x</var>, <var>y</var>, <var>z</var> are vectors describing a cubic grid then <code>length (<var>x</var>) == columns (<var>v</var>)</code>, <code>length (<var>y</var>) == rows (<var>v</var>)</code>, and <code>length (<var>z</var>) == size (<var>v</var>, 3)</code>. In either case the input data must be strictly monotonic. </p> <p>If called without <var>x</var>, <var>y</var>, <var>z</var>, and just a single reference data matrix <var>v</var>, the 3-D region <code><var>x</var> = 1:columns (<var>v</var>), <var>y</var> = 1:rows (<var>v</var>), <var>z</var> = 1:size (<var>v</var>, 3)</code> is assumed. This saves memory if the grid is regular and the distance between points is not important. </p> <p>If called with a single reference data matrix <var>v</var> and a refinement value <var>n</var>, then perform interpolation over a 3-D grid where each original interval has been recursively subdivided <var>n</var> times. This results in <code>2^<var>n</var>-1</code> additional points for every interval in the original grid. If <var>n</var> is omitted a value of 1 is used. As an example, the interval [0,1] with <code><var>n</var>==2</code> results in a refined interval with points at [0, 1/4, 1/2, 3/4, 1]. </p> <p>The interpolation <var>method</var> is one of: </p> <dl compact="compact"> <dt><code>"nearest"</code></dt> <dd><p>Return the nearest neighbor. </p> </dd> <dt><code>"linear"</code> (default)</dt> <dd><p>Linear interpolation from nearest neighbors. </p> </dd> <dt><code>"cubic"</code></dt> <dd><p>Piecewise cubic Hermite interpolating polynomial—shape-preserving interpolation with smooth first derivative (not implemented yet). </p> </dd> <dt><code>"spline"</code></dt> <dd><p>Cubic spline interpolation—smooth first and second derivatives throughout the curve. </p></dd> </dl> <p><var>extrapval</var> is a scalar number. It replaces values beyond the endpoints with <var>extrapval</var>. Note that if <var>extrapval</var> is used, <var>method</var> must be specified as well. If <var>extrapval</var> is omitted and the <var>method</var> is <code>"spline"</code>, then the extrapolated values of the <code>"spline"</code> are used. Otherwise the default <var>extrapval</var> value for any other <var>method</var> is <code>"NA"</code>. </p> <p><strong>See also:</strong> <a href="One_002ddimensional-Interpolation.html#XREFinterp1">interp1</a>, <a href="#XREFinterp2">interp2</a>, <a href="#XREFinterpn">interpn</a>, <a href="Three_002dDimensional-Plots.html#XREFmeshgrid">meshgrid</a>. </p></dd></dl> <a name="XREFinterpn"></a><dl> <dt><a name="index-interpn"></a><em><var>vi</var> =</em> <strong>interpn</strong> <em>(<var>x1</var>, <var>x2</var>, …, <var>v</var>, <var>y1</var>, <var>y2</var>, …)</em></dt> <dt><a name="index-interpn-1"></a><em><var>vi</var> =</em> <strong>interpn</strong> <em>(<var>v</var>, <var>y1</var>, <var>y2</var>, …)</em></dt> <dt><a name="index-interpn-2"></a><em><var>vi</var> =</em> <strong>interpn</strong> <em>(<var>v</var>, <var>m</var>)</em></dt> <dt><a name="index-interpn-3"></a><em><var>vi</var> =</em> <strong>interpn</strong> <em>(<var>v</var>)</em></dt> <dt><a name="index-interpn-4"></a><em><var>vi</var> =</em> <strong>interpn</strong> <em>(…, <var>method</var>)</em></dt> <dt><a name="index-interpn-5"></a><em><var>vi</var> =</em> <strong>interpn</strong> <em>(…, <var>method</var>, <var>extrapval</var>)</em></dt> <dd> <p>Perform <var>n</var>-dimensional interpolation, where <var>n</var> is at least two. </p> <p>Each element of the <var>n</var>-dimensional array <var>v</var> represents a value at a location given by the parameters <var>x1</var>, <var>x2</var>, …, <var>xn</var>. The parameters <var>x1</var>, <var>x2</var>, …, <var>xn</var> are either <var>n</var>-dimensional arrays of the same size as the array <var>v</var> in the <code>"ndgrid"</code> format or vectors. The parameters <var>y1</var>, etc. respect a similar format to <var>x1</var>, etc., and they represent the points at which the array <var>vi</var> is interpolated. </p> <p>If <var>x1</var>, …, <var>xn</var> are omitted, they are assumed to be <code>x1 = 1 : size (<var>v</var>, 1)</code>, etc. If <var>m</var> is specified, then the interpolation adds a point half way between each of the interpolation points. This process is performed <var>m</var> times. If only <var>v</var> is specified, then <var>m</var> is assumed to be <code>1</code>. </p> <p>The interpolation <var>method</var> is one of: </p> <dl compact="compact"> <dt><code>"nearest"</code></dt> <dd><p>Return the nearest neighbor. </p> </dd> <dt><code>"linear"</code> (default)</dt> <dd><p>Linear interpolation from nearest neighbors. </p> </dd> <dt><code>"pchip"</code></dt> <dd><p>Piecewise cubic Hermite interpolating polynomial—shape-preserving interpolation with smooth first derivative (not implemented yet). </p> </dd> <dt><code>"cubic"</code></dt> <dd><p>Cubic interpolation (same as <code>"pchip"</code> [not implemented yet]). </p> </dd> <dt><code>"spline"</code></dt> <dd><p>Cubic spline interpolation—smooth first and second derivatives throughout the curve. </p></dd> </dl> <p>The default method is <code>"linear"</code>. </p> <p><var>extrapval</var> is a scalar number. It replaces values beyond the endpoints with <var>extrapval</var>. Note that if <var>extrapval</var> is used, <var>method</var> must be specified as well. If <var>extrapval</var> is omitted and the <var>method</var> is <code>"spline"</code>, then the extrapolated values of the <code>"spline"</code> are used. Otherwise the default <var>extrapval</var> value for any other <var>method</var> is <code>"NA"</code>. </p> <p><strong>See also:</strong> <a href="One_002ddimensional-Interpolation.html#XREFinterp1">interp1</a>, <a href="#XREFinterp2">interp2</a>, <a href="#XREFinterp3">interp3</a>, <a href="One_002ddimensional-Interpolation.html#XREFspline">spline</a>, <a href="Three_002dDimensional-Plots.html#XREFndgrid">ndgrid</a>. </p></dd></dl> <p>A significant difference between <code>interpn</code> and the other two multi-dimensional interpolation functions is the fashion in which the dimensions are treated. For <code>interp2</code> and <code>interp3</code>, the y-axis is considered to be the columns of the matrix, whereas the x-axis corresponds to the rows of the array. As Octave indexes arrays in column major order, the first dimension of any array is the columns, and so <code>interpn</code> effectively reverses the ’x’ and ’y’ dimensions. Consider the example, </p> <div class="example"> <pre class="example">x = y = z = -1:1; f = @(x,y,z) x.^2 - y - z.^2; [xx, yy, zz] = meshgrid (x, y, z); v = f (xx,yy,zz); xi = yi = zi = -1:0.1:1; [xxi, yyi, zzi] = meshgrid (xi, yi, zi); vi = interp3 (x, y, z, v, xxi, yyi, zzi, "spline"); [xxi, yyi, zzi] = ndgrid (xi, yi, zi); vi2 = interpn (x, y, z, v, xxi, yyi, zzi, "spline"); mesh (zi, yi, squeeze (vi2(1,:,:))); </pre></div> <p>where <code>vi</code> and <code>vi2</code> are identical. The reversal of the dimensions is treated in the <code>meshgrid</code> and <code>ndgrid</code> functions respectively. The result of this code can be seen in <a href="#fig_003ainterpn">Figure 29.4</a>. </p> <div class="float"><a name="fig_003ainterpn"></a> <div align="center"><img src="interpn.png" alt="interpn"> </div> <div class="float-caption"><p><strong>Figure 29.4: </strong>Demonstration of the use of <code>interpn</code></p></div></div> <hr> <div class="header"> <p> Previous: <a href="One_002ddimensional-Interpolation.html#One_002ddimensional-Interpolation" accesskey="p" rel="prev">One-dimensional Interpolation</a>, Up: <a href="Interpolation.html#Interpolation" accesskey="u" rel="up">Interpolation</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p> </div> </body> </html>