File: Multi_002ddimensional-Interpolation.html

package info (click to toggle)
octave 3.8.2-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 84,396 kB
  • ctags: 45,547
  • sloc: cpp: 293,356; ansic: 42,041; fortran: 23,669; sh: 13,629; objc: 7,890; yacc: 7,093; lex: 3,442; java: 2,125; makefile: 1,589; perl: 1,009; awk: 974; xml: 34
file content (302 lines) | stat: -rw-r--r-- 15,470 bytes parent folder | download | duplicates (3)
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
300
301
302
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- Created by GNU Texinfo 5.2, http://www.gnu.org/software/texinfo/ -->
<head>
<title>GNU Octave: Multi-dimensional Interpolation</title>

<meta name="description" content="GNU Octave: Multi-dimensional Interpolation">
<meta name="keywords" content="GNU Octave: Multi-dimensional Interpolation">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<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.smallquotation {font-size: smaller}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.indentedblock {margin-left: 3.2em}
div.lisp {margin-left: 3.2em}
div.smalldisplay {margin-left: 3.2em}
div.smallexample {margin-left: 3.2em}
div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
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.nocodebreak {white-space:nowrap}
span.nolinebreak {white-space:nowrap}
span.roman {font-family:serif; font-weight:normal}
span.sansserif {font-family:sans-serif; font-weight:normal}
ul.no-bullet {list-style: none}
-->
</style>


</head>

<body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
<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> &nbsp; [<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>Function File: <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>Function File: <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>Function File: <em><var>zi</var> =</em> <strong>interp2</strong> <em>(<var>Z</var>, <var>n</var>)</em></dt>
<dt><a name="index-interp2-3"></a>Function File: <em><var>zi</var> =</em> <strong>interp2</strong> <em>(&hellip;, <var>method</var>)</em></dt>
<dt><a name="index-interp2-4"></a>Function File: <em><var>zi</var> =</em> <strong>interp2</strong> <em>(&hellip;, <var>method</var>, <var>extrapval</var>)</em></dt>
<dd>
<p>Two-dimensional interpolation.  <var>x</var>, <var>y</var> and <var>z</var> describe a
surface function.  If <var>x</var> and <var>y</var> are vectors their length
must correspondent to the size of <var>z</var>.  <var>x</var> and <var>y</var> must be
monotonic.  If they are matrices they must have the <code>meshgrid</code>
format.
</p>
<dl compact="compact">
<dt><code>interp2 (<var>x</var>, <var>y</var>, <var>Z</var>, <var>xi</var>, <var>yi</var>, &hellip;)</code></dt>
<dd><p>Returns a matrix corresponding to the points described by the
matrices <var>xi</var>, <var>yi</var>.
</p>
<p>If the last argument is a string, the interpolation method can
be specified.  The method can be <code>&quot;linear&quot;</code>, <code>&quot;nearest&quot;</code> or
<code>&quot;cubic&quot;</code>.  If it is omitted <code>&quot;linear&quot;</code> interpolation is
assumed.
</p>
</dd>
<dt><code>interp2 (<var>z</var>, <var>xi</var>, <var>yi</var>)</code></dt>
<dd><p>Assumes <code><var>x</var> = 1:rows (<var>z</var>)</code> and <code><var>y</var> =
1:columns (<var>z</var>)</code>
</p>
</dd>
<dt><code>interp2 (<var>z</var>, <var>n</var>)</code></dt>
<dd><p>Interleaves the matrix <var>z</var> n-times.  If <var>n</var> is omitted a value
of <code><var>n</var> = 1</code> is assumed.
</p></dd>
</dl>

<p>The variable <var>method</var> defines the method to use for the
interpolation.  It can take one of the following values
</p>
<dl compact="compact">
<dt><code>&quot;nearest&quot;</code></dt>
<dd><p>Return the nearest neighbor.
</p>
</dd>
<dt><code>&quot;linear&quot;</code></dt>
<dd><p>Linear interpolation from nearest neighbors.
</p>
</dd>
<dt><code>&quot;pchip&quot;</code></dt>
<dd><p>Piecewise cubic Hermite interpolating polynomial.
</p>
</dd>
<dt><code>&quot;cubic&quot;</code></dt>
<dd><p>Cubic interpolation from four nearest neighbors.
</p>
</dd>
<dt><code>&quot;spline&quot;</code></dt>
<dd><p>Cubic spline interpolation&mdash;smooth first and second derivatives
throughout the curve.
</p></dd>
</dl>

<p>If a scalar value <var>extrapval</var> is defined as the final value, then
values outside the mesh as set to this value.  Note that in this case
<var>method</var> must be defined as well.  If <var>extrapval</var> is not
defined then NA is assumed.
</p>

<p><strong>See also:</strong> <a href="One_002ddimensional-Interpolation.html#XREFinterp1">interp1</a>.
</p></dd></dl>


<a name="XREFinterp3"></a><dl>
<dt><a name="index-interp3"></a>Function File: <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>Function File: <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>Function File: <em><var>vi</var> =</em> <strong>interp3</strong> <em>(<var>v</var>, <var>m</var>)</em></dt>
<dt><a name="index-interp3-3"></a>Function File: <em><var>vi</var> =</em> <strong>interp3</strong> <em>(<var>v</var>)</em></dt>
<dt><a name="index-interp3-4"></a>Function File: <em><var>vi</var> =</em> <strong>interp3</strong> <em>(&hellip;, <var>method</var>)</em></dt>
<dt><a name="index-interp3-5"></a>Function File: <em><var>vi</var> =</em> <strong>interp3</strong> <em>(&hellip;, <var>method</var>, <var>extrapval</var>)</em></dt>
<dd>
<p>Perform 3-dimensional interpolation.  Each element of the 3-dimensional
array <var>v</var> represents a value at a location given by the parameters
<var>x</var>, <var>y</var>, and <var>z</var>.  The parameters <var>x</var>, <var>x</var>, and
<var>z</var> are either 3-dimensional arrays of the same size as the array
<var>v</var> in the <code>&quot;meshgrid&quot;</code> format or vectors.  The parameters
<var>xi</var>, etc. respect a similar format to <var>x</var>, etc., and they
represent the points at which the array <var>vi</var> is interpolated.
</p>
<p>If <var>x</var>, <var>y</var>, <var>z</var> are omitted, they are assumed to be
<code>x = 1 : size (<var>v</var>, 2)</code>, <code>y = 1 : size (<var>v</var>, 1)</code> and
<code>z = 1 : size (<var>v</var>, 3)</code>.  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>Method is one of:
</p>
<dl compact="compact">
<dt><code>&quot;nearest&quot;</code></dt>
<dd><p>Return the nearest neighbor.
</p>
</dd>
<dt><code>&quot;linear&quot;</code></dt>
<dd><p>Linear interpolation from nearest neighbors.
</p>
</dd>
<dt><code>&quot;cubic&quot;</code></dt>
<dd><p>Cubic interpolation from four nearest neighbors (not implemented yet).
</p>
</dd>
<dt><code>&quot;spline&quot;</code></dt>
<dd><p>Cubic spline interpolation&mdash;smooth first and second derivatives
throughout the curve.
</p></dd>
</dl>

<p>The default method is <code>&quot;linear&quot;</code>.
</p>
<p>If <var>extrap</var> is the string <code>&quot;extrap&quot;</code>, then extrapolate values
beyond the endpoints.  If <var>extrap</var> is a number, replace values beyond
the endpoints with that number.  If <var>extrap</var> is missing, assume NA.
</p>
<p><strong>See also:</strong> <a href="One_002ddimensional-Interpolation.html#XREFinterp1">interp1</a>, <a href="#XREFinterp2">interp2</a>, <a href="One_002ddimensional-Interpolation.html#XREFspline">spline</a>, <a href="Three_002dDimensional-Plots.html#XREFmeshgrid">meshgrid</a>.
</p></dd></dl>


<a name="XREFinterpn"></a><dl>
<dt><a name="index-interpn"></a>Function File: <em><var>vi</var> =</em> <strong>interpn</strong> <em>(<var>x1</var>, <var>x2</var>, &hellip;, <var>v</var>, <var>y1</var>, <var>y2</var>, &hellip;)</em></dt>
<dt><a name="index-interpn-1"></a>Function File: <em><var>vi</var> =</em> <strong>interpn</strong> <em>(<var>v</var>, <var>y1</var>, <var>y2</var>, &hellip;)</em></dt>
<dt><a name="index-interpn-2"></a>Function File: <em><var>vi</var> =</em> <strong>interpn</strong> <em>(<var>v</var>, <var>m</var>)</em></dt>
<dt><a name="index-interpn-3"></a>Function File: <em><var>vi</var> =</em> <strong>interpn</strong> <em>(<var>v</var>)</em></dt>
<dt><a name="index-interpn-4"></a>Function File: <em><var>vi</var> =</em> <strong>interpn</strong> <em>(&hellip;, <var>method</var>)</em></dt>
<dt><a name="index-interpn-5"></a>Function File: <em><var>vi</var> =</em> <strong>interpn</strong> <em>(&hellip;, <var>method</var>, <var>extrapval</var>)</em></dt>
<dd>
<p>Perform <var>n</var>-dimensional interpolation, where <var>n</var> is at least two.
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>, &hellip;, <var>xn</var>.
The parameters <var>x1</var>, <var>x2</var>, &hellip;, <var>xn</var> are either
<var>n</var>-dimensional arrays of the same size as the array <var>v</var> in
the <code>&quot;ndgrid&quot;</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>, &hellip;, <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>Method is one of:
</p>
<dl compact="compact">
<dt><code>&quot;nearest&quot;</code></dt>
<dd><p>Return the nearest neighbor.
</p>
</dd>
<dt><code>&quot;linear&quot;</code></dt>
<dd><p>Linear interpolation from nearest neighbors.
</p>
</dd>
<dt><code>&quot;cubic&quot;</code></dt>
<dd><p>Cubic interpolation from four nearest neighbors (not implemented yet).
</p>
</dd>
<dt><code>&quot;spline&quot;</code></dt>
<dd><p>Cubic spline interpolation&mdash;smooth first and second derivatives
throughout the curve.
</p></dd>
</dl>

<p>The default method is <code>&quot;linear&quot;</code>.
</p>
<p>If <var>extrapval</var> is the scalar value, use it to replace the values
beyond the endpoints with that number.  If <var>extrapval</var> is missing,
assume NA.
</p>
<p><strong>See also:</strong> <a href="One_002ddimensional-Interpolation.html#XREFinterp1">interp1</a>, <a href="#XREFinterp2">interp2</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 &rsquo;x&rsquo; and &rsquo;y&rsquo; 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, &quot;spline&quot;);
[xxi, yyi, zzi] = ndgrid (xi, yi, zi);
vi2 = interpn (x, y, z, v, xxi, yyi, zzi, &quot;spline&quot;);
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>
<p>In additional the support function <code>bicubic</code> that underlies the
cubic interpolation of <code>interp2</code> function can be called directly.
</p>
<a name="XREFbicubic"></a><dl>
<dt><a name="index-bicubic"></a>Function File: <em><var>zi</var> =</em> <strong>bicubic</strong> <em>(<var>x</var>, <var>y</var>, <var>z</var>, <var>xi</var>, <var>yi</var>, <var>extrapval</var>)</em></dt>
<dd>
<p>Return a matrix <var>zi</var> corresponding to the bicubic
interpolations at <var>xi</var> and <var>yi</var> of the data supplied
as <var>x</var>, <var>y</var> and <var>z</var>.  Points outside the grid are set
to <var>extrapval</var>.
</p>
<p>See <a href="http://wiki.woodpecker.org.cn/moin/Octave/Bicubic">http://wiki.woodpecker.org.cn/moin/Octave/Bicubic</a>
for further information.
</p>
<p><strong>See also:</strong> <a href="#XREFinterp2">interp2</a>.
</p></dd></dl>



<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> &nbsp; [<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>