Mercurial > hg > octave-nkf
annotate scripts/general/interp2.m @ 9399:a5f6b5800f86
fix bug in recursive lookup
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Fri, 26 Jun 2009 07:13:28 +0200 |
parents | 1bf0ce0930be |
children | 4f4873f6f873 |
rev | line source |
---|---|
8920 | 1 ## Copyright (C) 2000, 2006, 2007, 2008 Kai Habel |
2 ## Copyright (C) 2009 Jaroslav Hajek | |
5837 | 3 ## |
4 ## This file is part of Octave. | |
5 ## | |
6 ## Octave is free software; you can redistribute it and/or modify it | |
7 ## under the terms of the GNU General Public License as published by | |
7016 | 8 ## the Free Software Foundation; either version 3 of the License, or (at |
9 ## your option) any later version. | |
5837 | 10 ## |
11 ## Octave is distributed in the hope that it will be useful, but | |
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 ## General Public License for more details. | |
15 ## | |
16 ## You should have received a copy of the GNU General Public License | |
7016 | 17 ## along with Octave; see the file COPYING. If not, see |
18 ## <http://www.gnu.org/licenses/>. | |
5837 | 19 |
20 ## -*- texinfo -*- | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
21 ## @deftypefn {Function File} {@var{zi} =} interp2 (@var{x}, @var{y}, @var{z}, @var{xi}, @var{yi}) |
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
22 ## @deftypefnx {Function File} {@var{zi} =} interp2 (@var{Z}, @var{xi}, @var{yi}) |
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
23 ## @deftypefnx {Function File} {@var{zi} =} interp2 (@var{Z}, @var{n}) |
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
24 ## @deftypefnx {Function File} {@var{zi} =} interp2 (@dots{}, @var{method}) |
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
25 ## @deftypefnx {Function File} {@var{zi} =} interp2 (@dots{}, @var{method}, @var{extrapval}) |
5837 | 26 ## |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
27 ## Two-dimensional interpolation. @var{x}, @var{y} and @var{z} describe a |
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
28 ## surface function. If @var{x} and @var{y} are vectors their length |
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
29 ## must correspondent to the size of @var{z}. @var{x} and @var{y} must be |
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
30 ## monotonic. If they are matrices they must have the @code{meshgrid} |
5837 | 31 ## format. |
32 ## | |
33 ## @table @code | |
34 ## @item interp2 (@var{x}, @var{y}, @var{Z}, @var{xi}, @var{yi}, @dots{}) | |
35 ## Returns a matrix corresponding to the points described by the | |
8478
5aafd61e4c3c
[docs] @var{XI}, @var{YI}. => @var{xi}, @var{yi}.
Brian Gough <bjg@gnu.org>
parents:
8347
diff
changeset
|
36 ## matrices @var{xi}, @var{yi}. |
5837 | 37 ## |
38 ## If the last argument is a string, the interpolation method can | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
39 ## be specified. The method can be 'linear', 'nearest' or 'cubic'. |
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
40 ## If it is omitted 'linear' interpolation is assumed. |
5837 | 41 ## |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
42 ## @item interp2 (@var{z}, @var{xi}, @var{yi}) |
5837 | 43 ## Assumes @code{@var{x} = 1:rows (@var{z})} and @code{@var{y} = |
44 ## 1:columns (@var{z})} | |
45 ## | |
46 ## @item interp2 (@var{z}, @var{n}) | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
47 ## Interleaves the matrix @var{z} n-times. If @var{n} is omitted a value |
5837 | 48 ## of @code{@var{n} = 1} is assumed. |
49 ## @end table | |
50 ## | |
51 ## The variable @var{method} defines the method to use for the | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
52 ## interpolation. It can take one of the following values |
5837 | 53 ## |
54 ## @table @asis | |
55 ## @item 'nearest' | |
6218 | 56 ## Return the nearest neighbor. |
5837 | 57 ## @item 'linear' |
6218 | 58 ## Linear interpolation from nearest neighbors. |
5837 | 59 ## @item 'pchip' |
6218 | 60 ## Piece-wise cubic hermite interpolating polynomial (not implemented yet). |
5837 | 61 ## @item 'cubic' |
6218 | 62 ## Cubic interpolation from four nearest neighbors. |
5837 | 63 ## @item 'spline' |
64 ## Cubic spline interpolation--smooth first and second derivatives | |
6702 | 65 ## throughout the curve. |
5837 | 66 ## @end table |
67 ## | |
68 ## If a scalar value @var{extrapval} is defined as the final value, then | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
69 ## values outside the mesh as set to this value. Note that in this case |
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
70 ## @var{method} must be defined as well. If @var{extrapval} is not |
6742 | 71 ## defined then NA is assumed. |
5837 | 72 ## |
73 ## @seealso{interp1} | |
74 ## @end deftypefn | |
75 | |
76 ## Author: Kai Habel <kai.habel@gmx.de> | |
77 ## 2005-03-02 Thomas Weber <weber@num.uni-sb.de> | |
78 ## * Add test cases | |
79 ## 2005-03-02 Paul Kienzle <pkienzle@users.sf.net> | |
80 ## * Simplify | |
81 ## 2005-04-23 Dmitri A. Sergatskov <dasergatskov@gmail.com> | |
82 ## * Modified demo and test for new gnuplot interface | |
83 ## 2005-09-07 Hoxide <hoxide_dirac@yahoo.com.cn> | |
84 ## * Add bicubic interpolation method | |
5838 | 85 ## * Fix the eat line bug when the last element of XI or YI is |
86 ## negative or zero. | |
5837 | 87 ## 2005-11-26 Pierre Baldensperger <balden@libertysurf.fr> |
88 ## * Rather big modification (XI,YI no longer need to be | |
89 ## "meshgridded") to be consistent with the help message | |
90 ## above and for compatibility. | |
91 | |
92 function ZI = interp2 (varargin) | |
5838 | 93 Z = X = Y = XI = YI = n = []; |
5837 | 94 method = "linear"; |
6742 | 95 extrapval = NA; |
5837 | 96 |
5838 | 97 switch (nargin) |
98 case 1 | |
99 Z = varargin{1}; | |
100 case 2 | |
101 if (ischar (varargin{2})) | |
102 [Z, method] = deal (varargin{:}); | |
103 else | |
104 [Z, n] = deal (varargin{:}); | |
105 endif | |
106 case 3 | |
107 if (ischar (varargin{3})) | |
108 [Z, n, method] = deal (varargin{:}); | |
109 else | |
110 [Z, XI, YI] = deal (varargin{:}); | |
111 endif | |
112 case 4 | |
113 if (ischar (varargin{4})) | |
114 [Z, XI, YI, method] = deal (varargin{:}); | |
115 else | |
116 [Z, n, method, extrapval] = deal (varargin{:}); | |
117 endif | |
118 case 5 | |
119 if (ischar (varargin{4})) | |
120 [Z, XI, YI, method, extrapval] = deal (varargin{:}); | |
121 else | |
122 [X, Y, Z, XI, YI] = deal (varargin{:}); | |
123 endif | |
124 case 6 | |
125 [X, Y, Z, XI, YI, method] = deal (varargin{:}); | |
126 case 7 | |
127 [X, Y, Z, XI, YI, method, extrapval] = deal (varargin{:}); | |
128 otherwise | |
129 print_usage (); | |
5837 | 130 endswitch |
131 | |
132 ## Type checking. | |
5838 | 133 if (!ismatrix (Z)) |
134 error ("interp2 expected matrix Z"); | |
5837 | 135 endif |
5838 | 136 if (!isempty (n) && !isscalar (n)) |
137 error ("interp2 expected scalar n"); | |
5837 | 138 endif |
5838 | 139 if (!ischar (method)) |
140 error ("interp2 expected string 'method'"); | |
5837 | 141 endif |
8712
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
142 if (ischar (extrapval) || strcmp (extrapval, "extrap")) |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
143 extrapval = []; |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
144 elseif (!isscalar (extrapval)) |
5838 | 145 error ("interp2 expected n extrapval"); |
5837 | 146 endif |
147 | |
5838 | 148 ## Define X, Y, XI, YI if needed |
5837 | 149 [zr, zc] = size (Z); |
5838 | 150 if (isempty (X)) |
151 X = 1:zc; | |
152 Y = 1:zr; | |
5837 | 153 endif |
5838 | 154 if (! isnumeric (X) || ! isnumeric (Y)) |
155 error ("interp2 expected numeric X, Y"); | |
5837 | 156 endif |
5838 | 157 if (! isempty (n)) |
158 p = 2^n; | |
159 XI = (p:p*zc)/p; | |
160 YI = (p:p*zr)'/p; | |
5837 | 161 endif |
5838 | 162 if (! isnumeric (XI) || ! isnumeric (YI)) |
163 error ("interp2 expected numeric XI, YI"); | |
5837 | 164 endif |
165 | |
6702 | 166 |
8712
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
167 if (strcmp (method, "linear") || strcmp (method, "nearest") ... |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
168 || strcmp (method, "pchip")) |
6702 | 169 |
170 ## If X and Y vectors produce a grid from them | |
171 if (isvector (X) && isvector (Y)) | |
8712
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
172 X = X(:); Y = Y(:); |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
173 elseif (size_equal (X, Y)) |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
174 X = X(1,:)'; Y = Y(:,1); |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
175 else |
6702 | 176 error ("X and Y must be matrices of same size"); |
177 endif | |
8712
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
178 if (columns (Z) != length (X) || rows (Z) != length (Y)) |
6702 | 179 error ("X and Y size must match Z dimensions"); |
180 endif | |
181 | |
182 ## If Xi and Yi are vectors of different orientation build a grid | |
183 if ((rows (XI) == 1 && columns (YI) == 1) | |
184 || (columns (XI) == 1 && rows (YI) == 1)) | |
185 [XI, YI] = meshgrid (XI, YI); | |
186 elseif (! size_equal (XI, YI)) | |
187 error ("XI and YI must be matrices of same size"); | |
188 endif | |
5837 | 189 |
8712
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
190 ## if XI, YI are vectors, X and Y should share their orientation. |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
191 if (rows (XI) == 1) |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
192 if (rows (X) != 1) |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
193 X = X.'; |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
194 endif |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
195 if (rows (Y) != 1) |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
196 Y = Y.'; |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
197 endif |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
198 elseif (columns (XI) == 1) |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
199 if (columns (X) != 1) |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
200 X = X.'; |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
201 endif |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
202 if (columns (Y) != 1) |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
203 Y = Y.'; |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
204 endif |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
205 endif |
6702 | 206 |
8712
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
207 xidx = lookup (X, XI, "lr"); |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
208 yidx = lookup (Y, YI, "lr"); |
5837 | 209 |
9399
a5f6b5800f86
fix bug in recursive lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
210 if (min (xidx) <= 0) |
a5f6b5800f86
fix bug in recursive lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
211 save bad_data X XI |
a5f6b5800f86
fix bug in recursive lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
212 endif |
a5f6b5800f86
fix bug in recursive lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
213 |
a5f6b5800f86
fix bug in recursive lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
9051
diff
changeset
|
214 |
6702 | 215 if (strcmp (method, "linear")) |
216 ## each quad satisfies the equation z(x,y)=a+b*x+c*y+d*xy | |
217 ## | |
218 ## a-b | |
219 ## | | | |
220 ## c-d | |
221 a = Z(1:(zr - 1), 1:(zc - 1)); | |
222 b = Z(1:(zr - 1), 2:zc) - a; | |
223 c = Z(2:zr, 1:(zc - 1)) - a; | |
224 d = Z(2:zr, 2:zc) - a - b - c; | |
5837 | 225 |
6702 | 226 idx = sub2ind (size (a), yidx, xidx); |
227 | |
228 ## scale XI, YI values to a 1-spaced grid | |
8712
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
229 Xsc = (XI - X(xidx)) ./ (X(xidx + 1) - X(xidx)); |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
230 Ysc = (YI - Y(yidx)) ./ (Y(yidx + 1) - Y(yidx)); |
6702 | 231 |
232 ## apply plane equation | |
233 ZI = a(idx) + b(idx).*Xsc + c(idx).*Ysc + d(idx).*Xsc.*Ysc; | |
5837 | 234 |
6702 | 235 elseif (strcmp (method, "nearest")) |
8712
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
236 ii = (XI - X(xidx) > X(xidx + 1) - XI); |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
237 jj = (YI - Y(yidx) > Y(yidx + 1) - YI); |
6702 | 238 idx = sub2ind (size (Z), yidx+jj, xidx+ii); |
239 ZI = Z(idx); | |
8712
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
240 |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
241 elseif (strcmp (method, "pchip")) |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
242 |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
243 if (length (X) < 2 || length (Y) < 2) |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
244 error ("interp2: pchip2 requires at least 2 points in each dimension") |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
245 endif |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
246 |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
247 ## first order derivatives |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
248 DX = __pchip_deriv__ (X, Z, 2); |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
249 DY = __pchip_deriv__ (Y, Z, 1); |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
250 ## Compute mixed derivatives row-wise and column-wise, use the average. |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
251 DXY = (__pchip_deriv__ (X, DY, 2) + __pchip_deriv__ (Y, DX, 1))/2; |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
252 |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
253 ## do the bicubic interpolation |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
254 hx = diff (X); hx = hx(xidx); |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
255 hy = diff (Y); hy = hy(yidx); |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
256 |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
257 tx = (XI - X(xidx)) ./ hx; |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
258 ty = (YI - Y(yidx)) ./ hy; |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
259 |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
260 ## construct the cubic hermite base functions in x, y |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
261 |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
262 ## formulas: |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
263 ## b{1,1} = ( 2*t.^3 - 3*t.^2 + 1); |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
264 ## b{2,1} = h.*( t.^3 - 2*t.^2 + t ); |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
265 ## b{1,2} = (-2*t.^3 + 3*t.^2 ); |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
266 ## b{2,2} = h.*( t.^3 - t.^2 ); |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
267 |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
268 ## optimized equivalents of the above: |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
269 t1 = tx.^2; |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
270 t2 = tx.*t1 - t1; |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
271 xb{2,2} = hx.*t2; |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
272 t1 = t2 - t1; |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
273 xb{2,1} = hx.*(t1 + tx); |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
274 t2 += t1; |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
275 xb{1,2} = -t2; |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
276 xb{1,1} = t2 + 1; |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
277 |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
278 t1 = ty.^2; |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
279 t2 = ty.*t1 - t1; |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
280 yb{2,2} = hy.*t2; |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
281 t1 = t2 - t1; |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
282 yb{2,1} = hy.*(t1 + ty); |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
283 t2 += t1; |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
284 yb{1,2} = -t2; |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
285 yb{1,1} = t2 + 1; |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
286 |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
287 ZI = zeros (size (XI)); |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
288 for i = 1:2 |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
289 for j = 1:2 |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
290 zidx = sub2ind (size (Z), yidx+(j-1), xidx+(i-1)); |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
291 ZI += xb{1,i} .* yb{1,j} .* Z(zidx); |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
292 ZI += xb{2,i} .* yb{1,j} .* DX(zidx); |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
293 ZI += xb{1,i} .* yb{2,j} .* DY(zidx); |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
294 ZI += xb{2,i} .* yb{2,j} .* DXY(zidx); |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
295 endfor |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
296 endfor |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
297 |
6702 | 298 endif |
5837 | 299 |
8712
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
300 if (! isempty (extrapval)) |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
301 ## set points outside the table to 'extrapval' |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
302 if (X (1) < X (end)) |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
303 if (Y (1) < Y (end)) |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
304 ZI (XI < X(1,1) | XI > X(end) | YI < Y(1,1) | YI > Y(end)) = ... |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
305 extrapval; |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
306 else |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
307 ZI (XI < X(1) | XI > X(end) | YI < Y(end) | YI > Y(1)) = ... |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
308 extrapval; |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
309 endif |
6979 | 310 else |
8712
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
311 if (Y (1) < Y (end)) |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
312 ZI (XI < X(end) | XI > X(1) | YI < Y(1) | YI > Y(end)) = ... |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
313 extrapval; |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
314 else |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
315 ZI (XI < X(1,end) | XI > X(1) | YI < Y(end) | YI > Y(1)) = ... |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
316 extrapval; |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
317 endif |
6979 | 318 endif |
319 endif | |
320 | |
6702 | 321 else |
5837 | 322 |
6702 | 323 ## If X and Y vectors produce a grid from them |
324 if (isvector (X) && isvector (Y)) | |
325 X = X(:).'; | |
326 Y = Y(:); | |
327 if (!isequal ([length(X), length(Y)], size(Z))) | |
328 error ("X and Y size must match Z dimensions"); | |
329 endif | |
330 elseif (!size_equal (X, Y)) | |
331 error ("X and Y must be matrices of same size"); | |
332 if (! size_equal (X, Z)) | |
333 error ("X and Y size must match Z dimensions"); | |
334 endif | |
335 endif | |
5837 | 336 |
6702 | 337 ## If Xi and Yi are vectors of different orientation build a grid |
338 if ((rows (XI) == 1 && columns (YI) == 1) | |
339 || (columns (XI) == 1 && rows (YI) == 1)) | |
340 ## Do nothing | |
341 elseif (! size_equal (XI, YI)) | |
342 error ("XI and YI must be matrices of same size"); | |
343 endif | |
5837 | 344 |
6702 | 345 ## FIXME bicubic/__splinen__ don't handle arbitrary XI, YI |
346 if (strcmp (method, "cubic")) | |
347 ZI = bicubic (X, Y, Z, XI(1,:), YI(:,1), extrapval); | |
5837 | 348 |
6702 | 349 elseif (strcmp (method, "spline")) |
350 ZI = __splinen__ ({Y(:,1).', X(1,:)}, Z, {YI(:,1), XI(1,:)}, extrapval, | |
351 "spline"); | |
352 else | |
353 error ("interpolation method not recognized"); | |
354 endif | |
5837 | 355 |
6702 | 356 endif |
5837 | 357 endfunction |
358 | |
359 %!demo | |
360 %! A=[13,-1,12;5,4,3;1,6,2]; | |
361 %! x=[0,1,4]; y=[10,11,12]; | |
362 %! xi=linspace(min(x),max(x),17); | |
363 %! yi=linspace(min(y),max(y),26)'; | |
364 %! mesh(xi,yi,interp2(x,y,A,xi,yi,'linear')); | |
365 %! [x,y] = meshgrid(x,y); | |
366 %! hold on; plot3(x(:),y(:),A(:),"b*"); hold off; | |
367 | |
368 %!demo | |
8712
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
369 %! [x,y,A] = peaks(10); |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
370 %! x = x(1,:)'; y = y(:,1); |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
371 %! xi=linspace(min(x),max(x),41); |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
372 %! yi=linspace(min(y),max(y),41)'; |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
373 %! mesh(xi,yi,interp2(x,y,A,xi,yi,'linear')); |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
374 %! [x,y] = meshgrid(x,y); |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
375 %! hold on; plot3(x(:),y(:),A(:),"b*"); hold off; |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
376 |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
377 %!demo |
5837 | 378 %! A=[13,-1,12;5,4,3;1,6,2]; |
379 %! x=[0,1,4]; y=[10,11,12]; | |
380 %! xi=linspace(min(x),max(x),17); | |
381 %! yi=linspace(min(y),max(y),26)'; | |
382 %! mesh(xi,yi,interp2(x,y,A,xi,yi,'nearest')); | |
383 %! [x,y] = meshgrid(x,y); | |
384 %! hold on; plot3(x(:),y(:),A(:),"b*"); hold off; | |
385 | |
6702 | 386 %!demo |
8712
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
387 %! [x,y,A] = peaks(10); |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
388 %! x = x(1,:)'; y = y(:,1); |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
389 %! xi=linspace(min(x),max(x),41); |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
390 %! yi=linspace(min(y),max(y),41)'; |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
391 %! mesh(xi,yi,interp2(x,y,A,xi,yi,'nearest')); |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
392 %! [x,y] = meshgrid(x,y); |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
393 %! hold on; plot3(x(:),y(:),A(:),"b*"); hold off; |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
394 |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
395 %!demo |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
396 %! A=[13,-1,12;5,4,3;1,6,2]; |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
397 %! x=[0,1,2]; y=[10,11,12]; |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
398 %! xi=linspace(min(x),max(x),17); |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
399 %! yi=linspace(min(y),max(y),26)'; |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
400 %! mesh(xi,yi,interp2(x,y,A,xi,yi,'pchip')); |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
401 %! [x,y] = meshgrid(x,y); |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
402 %! hold on; plot3(x(:),y(:),A(:),"b*"); hold off; |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
403 |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
404 %!demo |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
405 %! [x,y,A] = peaks(10); |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
406 %! x = x(1,:)'; y = y(:,1); |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
407 %! xi=linspace(min(x),max(x),41); |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
408 %! yi=linspace(min(y),max(y),41)'; |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
409 %! mesh(xi,yi,interp2(x,y,A,xi,yi,'pchip')); |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
410 %! [x,y] = meshgrid(x,y); |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
411 %! hold on; plot3(x(:),y(:),A(:),"b*"); hold off; |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
412 |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
413 %!demo |
5837 | 414 %! A=[13,-1,12;5,4,3;1,6,2]; |
415 %! x=[0,1,2]; y=[10,11,12]; | |
416 %! xi=linspace(min(x),max(x),17); | |
6702 | 417 %! yi=linspace(min(y),max(y),26)'; |
5837 | 418 %! mesh(xi,yi,interp2(x,y,A,xi,yi,'cubic')); |
419 %! [x,y] = meshgrid(x,y); | |
420 %! hold on; plot3(x(:),y(:),A(:),"b*"); hold off; | |
421 | |
6702 | 422 %!demo |
8712
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
423 %! [x,y,A] = peaks(10); |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
424 %! x = x(1,:)'; y = y(:,1); |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
425 %! xi=linspace(min(x),max(x),41); |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
426 %! yi=linspace(min(y),max(y),41)'; |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
427 %! mesh(xi,yi,interp2(x,y,A,xi,yi,'cubic')); |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
428 %! [x,y] = meshgrid(x,y); |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
429 %! hold on; plot3(x(:),y(:),A(:),"b*"); hold off; |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
430 |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
431 %!demo |
6702 | 432 %! A=[13,-1,12;5,4,3;1,6,2]; |
433 %! x=[0,1,2]; y=[10,11,12]; | |
434 %! xi=linspace(min(x),max(x),17); | |
435 %! yi=linspace(min(y),max(y),26)'; | |
436 %! mesh(xi,yi,interp2(x,y,A,xi,yi,'spline')); | |
437 %! [x,y] = meshgrid(x,y); | |
438 %! hold on; plot3(x(:),y(:),A(:),"b*"); hold off; | |
439 | |
8712
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
440 %!demo |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
441 %! [x,y,A] = peaks(10); |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
442 %! x = x(1,:)'; y = y(:,1); |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
443 %! xi=linspace(min(x),max(x),41); |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
444 %! yi=linspace(min(y),max(y),41)'; |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
445 %! mesh(xi,yi,interp2(x,y,A,xi,yi,'spline')); |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
446 %! [x,y] = meshgrid(x,y); |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
447 %! hold on; plot3(x(:),y(:),A(:),"b*"); hold off; |
010e15c7be9a
support pchip method in interp2
Jaroslav Hajek <highegg@gmail.com>
parents:
8479
diff
changeset
|
448 |
5837 | 449 %!test % simple test |
450 %! x = [1,2,3]; | |
451 %! y = [4,5,6,7]; | |
452 %! [X, Y] = meshgrid(x,y); | |
453 %! Orig = X.^2 + Y.^3; | |
454 %! xi = [1.2,2, 1.5]; | |
455 %! yi = [6.2, 4.0, 5.0]'; | |
456 %! | |
457 %! Expected = ... | |
458 %! [243, 245.4, 243.9; | |
459 %! 65.6, 68, 66.5; | |
460 %! 126.6, 129, 127.5]; | |
461 %! Result = interp2(x,y,Orig, xi, yi); | |
462 %! | |
463 %! assert(Result, Expected, 1000*eps); | |
464 | |
465 %!test % 2^n form | |
466 %! x = [1,2,3]; | |
467 %! y = [4,5,6,7]; | |
468 %! [X, Y] = meshgrid(x,y); | |
469 %! Orig = X.^2 + Y.^3; | |
470 %! xi = [1:0.25:3]; yi = [4:0.25:7]'; | |
471 %! Expected = interp2(x,y,Orig, xi, yi); | |
472 %! Result = interp2(Orig,2); | |
473 %! | |
474 %! assert(Result, Expected, 10*eps); | |
475 | |
476 %!test % matrix slice | |
477 %! A = eye(4); | |
478 %! assert(interp2(A,[1:4],[1:4]),[1,1,1,1]); | |
479 | |
480 %!test % non-gridded XI,YI | |
481 %! A = eye(4); | |
482 %! assert(interp2(A,[1,2;3,4],[1,3;2,4]),[1,0;0,1]); | |
483 | |
484 %!test % for values outside of boundaries | |
485 %! x = [1,2,3]; | |
486 %! y = [4,5,6,7]; | |
487 %! [X, Y] = meshgrid(x,y); | |
488 %! Orig = X.^2 + Y.^3; | |
489 %! xi = [0,4]; | |
490 %! yi = [3,8]'; | |
6742 | 491 %! assert(interp2(x,y,Orig, xi, yi),[NA,NA;NA,NA]); |
5837 | 492 %! assert(interp2(x,y,Orig, xi, yi,'linear', 0),[0,0;0,0]); |
493 | |
494 %!test % for values at boundaries | |
495 %! A=[1,2;3,4]; | |
496 %! x=[0,1]; | |
497 %! y=[2,3]'; | |
498 %! assert(interp2(x,y,A,x,y,'linear'), A); | |
499 %! assert(interp2(x,y,A,x,y,'nearest'), A); | |
500 |