1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- Created by GNU Texinfo 6.7, 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 6.2.0))</title>
<meta name="description" content="Multi-dimensional Interpolation (GNU Octave (version 6.2.0))">
<meta name="keywords" content="Multi-dimensional Interpolation (GNU Octave (version 6.2.0))">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<link href="index.html" rel="start" title="Top">
<link href="Concept-Index.html" rel="index" title="Concept Index">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="Interpolation.html" rel="up" title="Interpolation">
<link href="Geometry.html" rel="next" title="Geometry">
<link href="One_002ddimensional-Interpolation.html" rel="prev" title="One-dimensional Interpolation">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.indentedblock {margin-right: 0em}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.lisp {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}
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">
<span id="Multi_002ddimensional-Interpolation"></span><div class="header">
<p>
Previous: <a href="One_002ddimensional-Interpolation.html" accesskey="p" rel="prev">One-dimensional Interpolation</a>, Up: <a href="Interpolation.html" 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" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<span id="Multi_002ddimensional-Interpolation-1"></span><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</a>.
</p>
<span id="XREFinterp2"></span><dl>
<dt id="index-interp2">: <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 id="index-interp2-1">: <em><var>zi</var> =</em> <strong>interp2</strong> <em>(<var>z</var>, <var>xi</var>, <var>yi</var>)</em></dt>
<dt id="index-interp2-2">: <em><var>zi</var> =</em> <strong>interp2</strong> <em>(<var>z</var>, <var>n</var>)</em></dt>
<dt id="index-interp2-3">: <em><var>zi</var> =</em> <strong>interp2</strong> <em>(<var>z</var>)</em></dt>
<dt id="index-interp2-4">: <em><var>zi</var> =</em> <strong>interp2</strong> <em>(…, <var>method</var>)</em></dt>
<dt id="index-interp2-5">: <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>
<span id="XREFinterp3"></span><dl>
<dt id="index-interp3">: <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 id="index-interp3-1">: <em><var>vi</var> =</em> <strong>interp3</strong> <em>(<var>v</var>, <var>xi</var>, <var>yi</var>, <var>zi</var>)</em></dt>
<dt id="index-interp3-2">: <em><var>vi</var> =</em> <strong>interp3</strong> <em>(<var>v</var>, <var>n</var>)</em></dt>
<dt id="index-interp3-3">: <em><var>vi</var> =</em> <strong>interp3</strong> <em>(<var>v</var>)</em></dt>
<dt id="index-interp3-4">: <em><var>vi</var> =</em> <strong>interp3</strong> <em>(…, <var>method</var>)</em></dt>
<dt id="index-interp3-5">: <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>
<span id="XREFinterpn"></span><dl>
<dt id="index-interpn">: <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 id="index-interpn-1">: <em><var>vi</var> =</em> <strong>interpn</strong> <em>(<var>v</var>, <var>y1</var>, <var>y2</var>, …)</em></dt>
<dt id="index-interpn-2">: <em><var>vi</var> =</em> <strong>interpn</strong> <em>(<var>v</var>, <var>m</var>)</em></dt>
<dt id="index-interpn-3">: <em><var>vi</var> =</em> <strong>interpn</strong> <em>(<var>v</var>)</em></dt>
<dt id="index-interpn-4">: <em><var>vi</var> =</em> <strong>interpn</strong> <em>(…, <var>method</var>)</em></dt>
<dt id="index-interpn-5">: <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.
</p>
<p>The parameters <var>y1</var>, <var>y2</var>, …, <var>yn</var> represent the points at
which the array <var>vi</var> is interpolated. They can be vectors of the same
length and orientation in which case they are interpreted as coordinates of
scattered points. If they are vectors of differing orientation or length,
they are used to form a grid in <code>"ndgrid"</code> format. They can also be
<var>n</var>-dimensional arrays of equal size.
</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"><span id="fig_003ainterpn"></span>
<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" accesskey="p" rel="prev">One-dimensional Interpolation</a>, Up: <a href="Interpolation.html" 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" title="Index" rel="index">Index</a>]</p>
</div>
</body>
</html>
|