Mercurial > hg > octave-lyh
annotate scripts/general/interp1.m @ 14359:7277fe922e99
doc: Use Octave preference for double quote in docstrings in scripts/
* interp1.m, interp2.m, interp3.m, interpn.m, profexplore.m, profile.m,
profshow.m, quadgk.m, lookfor.m, imagesc.m, bzip2.m, gzip.m, parseparams.m,
pkg.m, ancestor.m, caxis.m, ezmesh.m, ezmeshc.m, ezsurf.m, ezsurfc.m, hidden.m,
hold.m, patch.m, pie.m, pie3.m, quiver.m, quiver3.m, scatter3.m,
uicontextmenu.m, uicontrol.m, uipanel.m, uipushtool.m, uitoggletool.m,
uitoolbar.m, xlim.m, ylim.m, zlim.m, ismember.m, setxor.m, filter2.m, bicg.m,
demo.m, example.m, fail.m, test.m: Use Octave preference for double quote in
docstrings in scripts/ directory.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Mon, 13 Feb 2012 07:38:23 -0800 |
parents | ce2b59a6d0e5 |
children | f3d52523cde1 |
rev | line source |
---|---|
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
13141
diff
changeset
|
1 ## Copyright (C) 2000-2012 Paul Kienzle |
9929
45c08d7c2c79
allow discontinuous interpolant in interp1
Jaroslav Hajek <highegg@gmail.com>
parents:
9769
diff
changeset
|
2 ## Copyright (C) 2009 VZLU Prague |
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 -*- | |
10846
a4f482e66b65
Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
21 ## @deftypefn {Function File} {@var{yi} =} interp1 (@var{x}, @var{y}, @var{xi}) |
10820
c44c786f87ba
interp1.m: When absent set X equal to the inices of Y.
Ben Abbott <bpabbott@mac.com>
parents:
10793
diff
changeset
|
22 ## @deftypefnx {Function File} {@var{yi} =} interp1 (@var{y}, @var{xi}) |
5837 | 23 ## @deftypefnx {Function File} {@var{yi} =} interp1 (@dots{}, @var{method}) |
24 ## @deftypefnx {Function File} {@var{yi} =} interp1 (@dots{}, @var{extrap}) | |
14359
7277fe922e99
doc: Use Octave preference for double quote in docstrings in scripts/
Rik <octave@nomad.inbox5.com>
parents:
14335
diff
changeset
|
25 ## @deftypefnx {Function File} {@var{pp} =} interp1 (@dots{}, "pp") |
5837 | 26 ## |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
27 ## One-dimensional interpolation. Interpolate @var{y}, defined at the |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11536
diff
changeset
|
28 ## points @var{x}, at the points @var{xi}. The sample points @var{x} |
10846
a4f482e66b65
Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
29 ## must be monotonic. If not specified, @var{x} is taken to be the |
a4f482e66b65
Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
30 ## indices of @var{y}. If @var{y} is an array, treat the columns |
7001 | 31 ## of @var{y} separately. |
5837 | 32 ## |
33 ## Method is one of: | |
34 ## | |
35 ## @table @asis | |
14359
7277fe922e99
doc: Use Octave preference for double quote in docstrings in scripts/
Rik <octave@nomad.inbox5.com>
parents:
14335
diff
changeset
|
36 ## @item "nearest" |
9070
e9dc2ed2ec0f
Cleanup documentation for poly.texi, interp.texi, geometry.texi
Rik <rdrider0-list@yahoo.com>
parents:
9051
diff
changeset
|
37 ## Return the nearest neighbor. |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10820
diff
changeset
|
38 ## |
14359
7277fe922e99
doc: Use Octave preference for double quote in docstrings in scripts/
Rik <octave@nomad.inbox5.com>
parents:
14335
diff
changeset
|
39 ## @item "linear" |
9070
e9dc2ed2ec0f
Cleanup documentation for poly.texi, interp.texi, geometry.texi
Rik <rdrider0-list@yahoo.com>
parents:
9051
diff
changeset
|
40 ## Linear interpolation from nearest neighbors |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10820
diff
changeset
|
41 ## |
14359
7277fe922e99
doc: Use Octave preference for double quote in docstrings in scripts/
Rik <octave@nomad.inbox5.com>
parents:
14335
diff
changeset
|
42 ## @item "pchip" |
11536
702dbd0c53f5
Add undocumented ppder, ppint, ppjumps functions to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
43 ## Piecewise cubic Hermite interpolating polynomial |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10820
diff
changeset
|
44 ## |
14359
7277fe922e99
doc: Use Octave preference for double quote in docstrings in scripts/
Rik <octave@nomad.inbox5.com>
parents:
14335
diff
changeset
|
45 ## @item "cubic" |
12608
59e2460acae1
make piecewise polynomial (pp) functions more compatible
Kai Habel <kai.habel@gmx.de>
parents:
12459
diff
changeset
|
46 ## Cubic interpolation (same as @code{pchip}) |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10820
diff
changeset
|
47 ## |
14359
7277fe922e99
doc: Use Octave preference for double quote in docstrings in scripts/
Rik <octave@nomad.inbox5.com>
parents:
14335
diff
changeset
|
48 ## @item "spline" |
12175
2090995ca588
Correct en-dash,em-dash instances in docstrings.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
49 ## Cubic spline interpolation---smooth first and second derivatives |
5837 | 50 ## throughout the curve |
51 ## @end table | |
52 ## | |
53 ## Appending '*' to the start of the above method forces @code{interp1} | |
54 ## to assume that @var{x} is uniformly spaced, and only @code{@var{x} | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
55 ## (1)} and @code{@var{x} (2)} are referenced. This is usually faster, |
14359
7277fe922e99
doc: Use Octave preference for double quote in docstrings in scripts/
Rik <octave@nomad.inbox5.com>
parents:
14335
diff
changeset
|
56 ## and is never slower. The default method is "linear". |
5837 | 57 ## |
14359
7277fe922e99
doc: Use Octave preference for double quote in docstrings in scripts/
Rik <octave@nomad.inbox5.com>
parents:
14335
diff
changeset
|
58 ## If @var{extrap} is the string "extrap", then extrapolate values beyond |
5837 | 59 ## the endpoints. If @var{extrap} is a number, replace values beyond the |
6742 | 60 ## endpoints with that number. If @var{extrap} is missing, assume NA. |
5837 | 61 ## |
14359
7277fe922e99
doc: Use Octave preference for double quote in docstrings in scripts/
Rik <octave@nomad.inbox5.com>
parents:
14335
diff
changeset
|
62 ## If the string argument "pp" is specified, then @var{xi} should not be |
11536
702dbd0c53f5
Add undocumented ppder, ppint, ppjumps functions to documentation.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
63 ## supplied and @code{interp1} returns the piecewise polynomial that |
5837 | 64 ## can later be used with @code{ppval} to evaluate the interpolation. |
65 ## There is an equivalence, such that @code{ppval (interp1 (@var{x}, | |
14359
7277fe922e99
doc: Use Octave preference for double quote in docstrings in scripts/
Rik <octave@nomad.inbox5.com>
parents:
14335
diff
changeset
|
66 ## @var{y}, @var{method}, "pp"), @var{xi}) == interp1 (@var{x}, @var{y}, |
7277fe922e99
doc: Use Octave preference for double quote in docstrings in scripts/
Rik <octave@nomad.inbox5.com>
parents:
14335
diff
changeset
|
67 ## @var{xi}, @var{method}, "extrap")}. |
5837 | 68 ## |
10711
fbd7843974fa
Periodic grammar check of documentation files to ensure common format.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
69 ## Duplicate points in @var{x} specify a discontinuous interpolant. There |
9929
45c08d7c2c79
allow discontinuous interpolant in interp1
Jaroslav Hajek <highegg@gmail.com>
parents:
9769
diff
changeset
|
70 ## should be at most 2 consecutive points with the same value. |
45c08d7c2c79
allow discontinuous interpolant in interp1
Jaroslav Hajek <highegg@gmail.com>
parents:
9769
diff
changeset
|
71 ## The discontinuous interpolant is right-continuous if @var{x} is increasing, |
45c08d7c2c79
allow discontinuous interpolant in interp1
Jaroslav Hajek <highegg@gmail.com>
parents:
9769
diff
changeset
|
72 ## left-continuous if it is decreasing. |
45c08d7c2c79
allow discontinuous interpolant in interp1
Jaroslav Hajek <highegg@gmail.com>
parents:
9769
diff
changeset
|
73 ## Discontinuities are (currently) only allowed for "nearest" and "linear" |
45c08d7c2c79
allow discontinuous interpolant in interp1
Jaroslav Hajek <highegg@gmail.com>
parents:
9769
diff
changeset
|
74 ## methods; in all other cases, @var{x} must be strictly monotonic. |
45c08d7c2c79
allow discontinuous interpolant in interp1
Jaroslav Hajek <highegg@gmail.com>
parents:
9769
diff
changeset
|
75 ## |
5837 | 76 ## An example of the use of @code{interp1} is |
77 ## | |
78 ## @example | |
79 ## @group | |
8507 | 80 ## xf = [0:0.05:10]; |
81 ## yf = sin (2*pi*xf/5); | |
82 ## xp = [0:10]; | |
83 ## yp = sin (2*pi*xp/5); | |
84 ## lin = interp1 (xp, yp, xf); | |
85 ## spl = interp1 (xp, yp, xf, "spline"); | |
86 ## cub = interp1 (xp, yp, xf, "cubic"); | |
87 ## near = interp1 (xp, yp, xf, "nearest"); | |
88 ## plot (xf, yf, "r", xf, lin, "g", xf, spl, "b", | |
89 ## xf, cub, "c", xf, near, "m", xp, yp, "r*"); | |
14327
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
90 ## legend ("original", "linear", "spline", "cubic", "nearest"); |
5837 | 91 ## @end group |
92 ## @end example | |
93 ## | |
94 ## @seealso{interpft} | |
95 ## @end deftypefn | |
96 | |
5838 | 97 ## Author: Paul Kienzle |
98 ## Date: 2000-03-25 | |
5837 | 99 ## added 'nearest' as suggested by Kai Habel |
100 ## 2000-07-17 Paul Kienzle | |
101 ## added '*' methods and matrix y | |
102 ## check for proper table lengths | |
103 ## 2002-01-23 Paul Kienzle | |
104 ## fixed extrapolation | |
105 | |
5838 | 106 function yi = interp1 (x, y, varargin) |
5837 | 107 |
10820
c44c786f87ba
interp1.m: When absent set X equal to the inices of Y.
Ben Abbott <bpabbott@mac.com>
parents:
10793
diff
changeset
|
108 if (nargin < 2 || nargin > 6) |
5837 | 109 print_usage (); |
110 endif | |
111 | |
112 method = "linear"; | |
6742 | 113 extrap = NA; |
5837 | 114 xi = []; |
12608
59e2460acae1
make piecewise polynomial (pp) functions more compatible
Kai Habel <kai.habel@gmx.de>
parents:
12459
diff
changeset
|
115 ispp = false; |
5837 | 116 firstnumeric = true; |
117 | |
118 if (nargin > 2) | |
5838 | 119 for i = 1:length (varargin) |
5837 | 120 arg = varargin{i}; |
5838 | 121 if (ischar (arg)) |
10549 | 122 arg = tolower (arg); |
123 if (strcmp ("extrap", arg)) | |
124 extrap = "extrap"; | |
125 elseif (strcmp ("pp", arg)) | |
12608
59e2460acae1
make piecewise polynomial (pp) functions more compatible
Kai Habel <kai.habel@gmx.de>
parents:
12459
diff
changeset
|
126 ispp = true; |
10549 | 127 else |
128 method = arg; | |
129 endif | |
5837 | 130 else |
10549 | 131 if (firstnumeric) |
132 xi = arg; | |
133 firstnumeric = false; | |
134 else | |
135 extrap = arg; | |
136 endif | |
5837 | 137 endif |
138 endfor | |
139 endif | |
140 | |
12608
59e2460acae1
make piecewise polynomial (pp) functions more compatible
Kai Habel <kai.habel@gmx.de>
parents:
12459
diff
changeset
|
141 if (isempty (xi) && firstnumeric && ! ispp) |
10820
c44c786f87ba
interp1.m: When absent set X equal to the inices of Y.
Ben Abbott <bpabbott@mac.com>
parents:
10793
diff
changeset
|
142 xi = y; |
c44c786f87ba
interp1.m: When absent set X equal to the inices of Y.
Ben Abbott <bpabbott@mac.com>
parents:
10793
diff
changeset
|
143 y = x; |
c44c786f87ba
interp1.m: When absent set X equal to the inices of Y.
Ben Abbott <bpabbott@mac.com>
parents:
10793
diff
changeset
|
144 x = 1:numel(y); |
c44c786f87ba
interp1.m: When absent set X equal to the inices of Y.
Ben Abbott <bpabbott@mac.com>
parents:
10793
diff
changeset
|
145 endif |
c44c786f87ba
interp1.m: When absent set X equal to the inices of Y.
Ben Abbott <bpabbott@mac.com>
parents:
10793
diff
changeset
|
146 |
5837 | 147 ## reshape matrices for convenience |
148 x = x(:); | |
9754
4219e5cf773d
improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents:
9070
diff
changeset
|
149 nx = rows (x); |
9769
9a1c4fe44af8
small interp1 simplification
Jaroslav Hajek <highegg@gmail.com>
parents:
9754
diff
changeset
|
150 szx = size (xi); |
9754
4219e5cf773d
improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents:
9070
diff
changeset
|
151 if (isvector (y)) |
5838 | 152 y = y(:); |
5837 | 153 endif |
13141
e81ddf9cacd5
maint: untabify and remove trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
12608
diff
changeset
|
154 |
5838 | 155 szy = size (y); |
9754
4219e5cf773d
improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents:
9070
diff
changeset
|
156 y = y(:,:); |
4219e5cf773d
improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents:
9070
diff
changeset
|
157 [ny, nc] = size (y); |
5837 | 158 xi = xi(:); |
159 | |
160 ## determine sizes | |
161 if (nx < 2 || ny < 2) | |
5838 | 162 error ("interp1: table too short"); |
5837 | 163 endif |
164 | |
9754
4219e5cf773d
improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents:
9070
diff
changeset
|
165 ## check whether x is sorted; sort if not. |
9929
45c08d7c2c79
allow discontinuous interpolant in interp1
Jaroslav Hajek <highegg@gmail.com>
parents:
9769
diff
changeset
|
166 if (! issorted (x, "either")) |
9754
4219e5cf773d
improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents:
9070
diff
changeset
|
167 [x, p] = sort (x); |
4219e5cf773d
improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents:
9070
diff
changeset
|
168 y = y(p,:); |
5837 | 169 endif |
5838 | 170 |
9754
4219e5cf773d
improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents:
9070
diff
changeset
|
171 starmethod = method(1) == "*"; |
4219e5cf773d
improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents:
9070
diff
changeset
|
172 |
4219e5cf773d
improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents:
9070
diff
changeset
|
173 if (starmethod) |
4219e5cf773d
improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents:
9070
diff
changeset
|
174 dx = x(2) - x(1); |
4219e5cf773d
improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents:
9070
diff
changeset
|
175 else |
9929
45c08d7c2c79
allow discontinuous interpolant in interp1
Jaroslav Hajek <highegg@gmail.com>
parents:
9769
diff
changeset
|
176 jumps = x(1:nx-1) == x(2:nx); |
45c08d7c2c79
allow discontinuous interpolant in interp1
Jaroslav Hajek <highegg@gmail.com>
parents:
9769
diff
changeset
|
177 have_jumps = any (jumps); |
45c08d7c2c79
allow discontinuous interpolant in interp1
Jaroslav Hajek <highegg@gmail.com>
parents:
9769
diff
changeset
|
178 if (have_jumps) |
45c08d7c2c79
allow discontinuous interpolant in interp1
Jaroslav Hajek <highegg@gmail.com>
parents:
9769
diff
changeset
|
179 if (any (strcmp (method, {"nearest", "linear"}))) |
45c08d7c2c79
allow discontinuous interpolant in interp1
Jaroslav Hajek <highegg@gmail.com>
parents:
9769
diff
changeset
|
180 if (any (jumps(1:nx-2) & jumps(2:nx-1))) |
45c08d7c2c79
allow discontinuous interpolant in interp1
Jaroslav Hajek <highegg@gmail.com>
parents:
9769
diff
changeset
|
181 warning ("interp1: extra points in discontinuities"); |
45c08d7c2c79
allow discontinuous interpolant in interp1
Jaroslav Hajek <highegg@gmail.com>
parents:
9769
diff
changeset
|
182 endif |
45c08d7c2c79
allow discontinuous interpolant in interp1
Jaroslav Hajek <highegg@gmail.com>
parents:
9769
diff
changeset
|
183 else |
45c08d7c2c79
allow discontinuous interpolant in interp1
Jaroslav Hajek <highegg@gmail.com>
parents:
9769
diff
changeset
|
184 error ("interp1: discontinuities not supported for method %s", method); |
45c08d7c2c79
allow discontinuous interpolant in interp1
Jaroslav Hajek <highegg@gmail.com>
parents:
9769
diff
changeset
|
185 endif |
5837 | 186 endif |
187 endif | |
188 | |
9754
4219e5cf773d
improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents:
9070
diff
changeset
|
189 ## Proceed with interpolating by all methods. |
4219e5cf773d
improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents:
9070
diff
changeset
|
190 |
4219e5cf773d
improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents:
9070
diff
changeset
|
191 switch (method) |
4219e5cf773d
improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents:
9070
diff
changeset
|
192 case "nearest" |
12608
59e2460acae1
make piecewise polynomial (pp) functions more compatible
Kai Habel <kai.habel@gmx.de>
parents:
12459
diff
changeset
|
193 pp = mkpp ([x(1); (x(1:nx-1)+x(2:nx))/2; x(nx)], shiftdim (y, 1), szy(2:end)); |
59e2460acae1
make piecewise polynomial (pp) functions more compatible
Kai Habel <kai.habel@gmx.de>
parents:
12459
diff
changeset
|
194 pp.orient = "first"; |
13141
e81ddf9cacd5
maint: untabify and remove trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
12608
diff
changeset
|
195 |
12608
59e2460acae1
make piecewise polynomial (pp) functions more compatible
Kai Habel <kai.habel@gmx.de>
parents:
12459
diff
changeset
|
196 if (ispp) |
59e2460acae1
make piecewise polynomial (pp) functions more compatible
Kai Habel <kai.habel@gmx.de>
parents:
12459
diff
changeset
|
197 yi = pp; |
5837 | 198 else |
12608
59e2460acae1
make piecewise polynomial (pp) functions more compatible
Kai Habel <kai.habel@gmx.de>
parents:
12459
diff
changeset
|
199 yi = ppval (pp, reshape (xi, szx)); |
5837 | 200 endif |
9754
4219e5cf773d
improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents:
9070
diff
changeset
|
201 case "*nearest" |
12608
59e2460acae1
make piecewise polynomial (pp) functions more compatible
Kai Habel <kai.habel@gmx.de>
parents:
12459
diff
changeset
|
202 pp = mkpp ([x(1), x(1)+[0.5:(nx-1)]*dx, x(nx)], shiftdim (y, 1), szy(2:end)); |
59e2460acae1
make piecewise polynomial (pp) functions more compatible
Kai Habel <kai.habel@gmx.de>
parents:
12459
diff
changeset
|
203 pp.orient = "first"; |
59e2460acae1
make piecewise polynomial (pp) functions more compatible
Kai Habel <kai.habel@gmx.de>
parents:
12459
diff
changeset
|
204 if (ispp) |
59e2460acae1
make piecewise polynomial (pp) functions more compatible
Kai Habel <kai.habel@gmx.de>
parents:
12459
diff
changeset
|
205 yi = pp; |
5837 | 206 else |
12608
59e2460acae1
make piecewise polynomial (pp) functions more compatible
Kai Habel <kai.habel@gmx.de>
parents:
12459
diff
changeset
|
207 yi = ppval(pp, reshape (xi, szx)); |
5837 | 208 endif |
9754
4219e5cf773d
improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents:
9070
diff
changeset
|
209 case "linear" |
4219e5cf773d
improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents:
9070
diff
changeset
|
210 dy = diff (y); |
13141
e81ddf9cacd5
maint: untabify and remove trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
12608
diff
changeset
|
211 dx = diff (x); |
12608
59e2460acae1
make piecewise polynomial (pp) functions more compatible
Kai Habel <kai.habel@gmx.de>
parents:
12459
diff
changeset
|
212 dx = repmat (dx, [1 size(dy)(2:end)]); |
59e2460acae1
make piecewise polynomial (pp) functions more compatible
Kai Habel <kai.habel@gmx.de>
parents:
12459
diff
changeset
|
213 coefs = [(dy./dx).'(:), y(1:nx-1, :).'(:)]; |
59e2460acae1
make piecewise polynomial (pp) functions more compatible
Kai Habel <kai.habel@gmx.de>
parents:
12459
diff
changeset
|
214 xx = x; |
59e2460acae1
make piecewise polynomial (pp) functions more compatible
Kai Habel <kai.habel@gmx.de>
parents:
12459
diff
changeset
|
215 |
59e2460acae1
make piecewise polynomial (pp) functions more compatible
Kai Habel <kai.habel@gmx.de>
parents:
12459
diff
changeset
|
216 if (have_jumps) |
59e2460acae1
make piecewise polynomial (pp) functions more compatible
Kai Habel <kai.habel@gmx.de>
parents:
12459
diff
changeset
|
217 ## Omit zero-size intervals. |
59e2460acae1
make piecewise polynomial (pp) functions more compatible
Kai Habel <kai.habel@gmx.de>
parents:
12459
diff
changeset
|
218 coefs(jumps, :) = []; |
59e2460acae1
make piecewise polynomial (pp) functions more compatible
Kai Habel <kai.habel@gmx.de>
parents:
12459
diff
changeset
|
219 xx(jumps) = []; |
59e2460acae1
make piecewise polynomial (pp) functions more compatible
Kai Habel <kai.habel@gmx.de>
parents:
12459
diff
changeset
|
220 endif |
59e2460acae1
make piecewise polynomial (pp) functions more compatible
Kai Habel <kai.habel@gmx.de>
parents:
12459
diff
changeset
|
221 |
59e2460acae1
make piecewise polynomial (pp) functions more compatible
Kai Habel <kai.habel@gmx.de>
parents:
12459
diff
changeset
|
222 pp = mkpp (xx, coefs, szy(2:end)); |
59e2460acae1
make piecewise polynomial (pp) functions more compatible
Kai Habel <kai.habel@gmx.de>
parents:
12459
diff
changeset
|
223 pp.orient = "first"; |
59e2460acae1
make piecewise polynomial (pp) functions more compatible
Kai Habel <kai.habel@gmx.de>
parents:
12459
diff
changeset
|
224 |
59e2460acae1
make piecewise polynomial (pp) functions more compatible
Kai Habel <kai.habel@gmx.de>
parents:
12459
diff
changeset
|
225 if (ispp) |
59e2460acae1
make piecewise polynomial (pp) functions more compatible
Kai Habel <kai.habel@gmx.de>
parents:
12459
diff
changeset
|
226 yi = pp; |
5837 | 227 else |
12608
59e2460acae1
make piecewise polynomial (pp) functions more compatible
Kai Habel <kai.habel@gmx.de>
parents:
12459
diff
changeset
|
228 yi = ppval(pp, reshape (xi, szx)); |
5837 | 229 endif |
12608
59e2460acae1
make piecewise polynomial (pp) functions more compatible
Kai Habel <kai.habel@gmx.de>
parents:
12459
diff
changeset
|
230 |
9754
4219e5cf773d
improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents:
9070
diff
changeset
|
231 case "*linear" |
4219e5cf773d
improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents:
9070
diff
changeset
|
232 dy = diff (y); |
12608
59e2460acae1
make piecewise polynomial (pp) functions more compatible
Kai Habel <kai.habel@gmx.de>
parents:
12459
diff
changeset
|
233 coefs = [(dy/dx).'(:), y(1:nx-1, :).'(:)]; |
59e2460acae1
make piecewise polynomial (pp) functions more compatible
Kai Habel <kai.habel@gmx.de>
parents:
12459
diff
changeset
|
234 pp = mkpp (x, coefs, szy(2:end)); |
59e2460acae1
make piecewise polynomial (pp) functions more compatible
Kai Habel <kai.habel@gmx.de>
parents:
12459
diff
changeset
|
235 pp.orient = "first"; |
59e2460acae1
make piecewise polynomial (pp) functions more compatible
Kai Habel <kai.habel@gmx.de>
parents:
12459
diff
changeset
|
236 |
59e2460acae1
make piecewise polynomial (pp) functions more compatible
Kai Habel <kai.habel@gmx.de>
parents:
12459
diff
changeset
|
237 if (ispp) |
59e2460acae1
make piecewise polynomial (pp) functions more compatible
Kai Habel <kai.habel@gmx.de>
parents:
12459
diff
changeset
|
238 yi = pp; |
5837 | 239 else |
12608
59e2460acae1
make piecewise polynomial (pp) functions more compatible
Kai Habel <kai.habel@gmx.de>
parents:
12459
diff
changeset
|
240 yi = ppval(pp, reshape (xi, szx)); |
59e2460acae1
make piecewise polynomial (pp) functions more compatible
Kai Habel <kai.habel@gmx.de>
parents:
12459
diff
changeset
|
241 endif |
5837 | 242 |
12608
59e2460acae1
make piecewise polynomial (pp) functions more compatible
Kai Habel <kai.habel@gmx.de>
parents:
12459
diff
changeset
|
243 case {"pchip", "*pchip", "cubic", "*cubic"} |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11536
diff
changeset
|
244 if (nx == 2 || starmethod) |
6374 | 245 x = linspace (x(1), x(nx), ny); |
5837 | 246 endif |
13141
e81ddf9cacd5
maint: untabify and remove trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
12608
diff
changeset
|
247 |
12608
59e2460acae1
make piecewise polynomial (pp) functions more compatible
Kai Habel <kai.habel@gmx.de>
parents:
12459
diff
changeset
|
248 if (ispp) |
59e2460acae1
make piecewise polynomial (pp) functions more compatible
Kai Habel <kai.habel@gmx.de>
parents:
12459
diff
changeset
|
249 y = shiftdim (reshape (y, szy), 1); |
59e2460acae1
make piecewise polynomial (pp) functions more compatible
Kai Habel <kai.habel@gmx.de>
parents:
12459
diff
changeset
|
250 yi = pchip (x, y); |
9754
4219e5cf773d
improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents:
9070
diff
changeset
|
251 else |
12608
59e2460acae1
make piecewise polynomial (pp) functions more compatible
Kai Habel <kai.habel@gmx.de>
parents:
12459
diff
changeset
|
252 y = shiftdim (y, 1); |
59e2460acae1
make piecewise polynomial (pp) functions more compatible
Kai Habel <kai.habel@gmx.de>
parents:
12459
diff
changeset
|
253 yi = pchip (x, y, reshape (xi, szx)); |
9754
4219e5cf773d
improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents:
9070
diff
changeset
|
254 endif |
4219e5cf773d
improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents:
9070
diff
changeset
|
255 case {"spline", "*spline"} |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11536
diff
changeset
|
256 if (nx == 2 || starmethod) |
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11536
diff
changeset
|
257 x = linspace(x(1), x(nx), ny); |
5837 | 258 endif |
13141
e81ddf9cacd5
maint: untabify and remove trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
12608
diff
changeset
|
259 |
12608
59e2460acae1
make piecewise polynomial (pp) functions more compatible
Kai Habel <kai.habel@gmx.de>
parents:
12459
diff
changeset
|
260 if (ispp) |
59e2460acae1
make piecewise polynomial (pp) functions more compatible
Kai Habel <kai.habel@gmx.de>
parents:
12459
diff
changeset
|
261 y = shiftdim (reshape (y, szy), 1); |
59e2460acae1
make piecewise polynomial (pp) functions more compatible
Kai Habel <kai.habel@gmx.de>
parents:
12459
diff
changeset
|
262 yi = spline (x, y); |
5837 | 263 else |
12608
59e2460acae1
make piecewise polynomial (pp) functions more compatible
Kai Habel <kai.habel@gmx.de>
parents:
12459
diff
changeset
|
264 y = shiftdim (y, 1); |
59e2460acae1
make piecewise polynomial (pp) functions more compatible
Kai Habel <kai.habel@gmx.de>
parents:
12459
diff
changeset
|
265 yi = spline (x, y, reshape (xi, szx)); |
5837 | 266 endif |
9754
4219e5cf773d
improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents:
9070
diff
changeset
|
267 otherwise |
5838 | 268 error ("interp1: invalid method '%s'", method); |
9754
4219e5cf773d
improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents:
9070
diff
changeset
|
269 endswitch |
5837 | 270 |
12608
59e2460acae1
make piecewise polynomial (pp) functions more compatible
Kai Habel <kai.habel@gmx.de>
parents:
12459
diff
changeset
|
271 if (! ispp) |
9754
4219e5cf773d
improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents:
9070
diff
changeset
|
272 if (! ischar (extrap)) |
4219e5cf773d
improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents:
9070
diff
changeset
|
273 ## determine which values are out of range and set them to extrap, |
4219e5cf773d
improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents:
9070
diff
changeset
|
274 ## unless extrap == "extrap". |
4219e5cf773d
improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents:
9070
diff
changeset
|
275 minx = min (x(1), x(nx)); |
4219e5cf773d
improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents:
9070
diff
changeset
|
276 maxx = max (x(1), x(nx)); |
4219e5cf773d
improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents:
9070
diff
changeset
|
277 |
4219e5cf773d
improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents:
9070
diff
changeset
|
278 outliers = xi < minx | ! (xi <= maxx); # this catches even NaNs |
12608
59e2460acae1
make piecewise polynomial (pp) functions more compatible
Kai Habel <kai.habel@gmx.de>
parents:
12459
diff
changeset
|
279 if (size_equal (outliers, yi)) |
59e2460acae1
make piecewise polynomial (pp) functions more compatible
Kai Habel <kai.habel@gmx.de>
parents:
12459
diff
changeset
|
280 yi(outliers) = extrap; |
59e2460acae1
make piecewise polynomial (pp) functions more compatible
Kai Habel <kai.habel@gmx.de>
parents:
12459
diff
changeset
|
281 yi = reshape (yi, szx); |
59e2460acae1
make piecewise polynomial (pp) functions more compatible
Kai Habel <kai.habel@gmx.de>
parents:
12459
diff
changeset
|
282 elseif (!isvector (yi)) |
59e2460acae1
make piecewise polynomial (pp) functions more compatible
Kai Habel <kai.habel@gmx.de>
parents:
12459
diff
changeset
|
283 if (strcmp (method, "pchip") || strcmp (method, "*pchip") |
59e2460acae1
make piecewise polynomial (pp) functions more compatible
Kai Habel <kai.habel@gmx.de>
parents:
12459
diff
changeset
|
284 ||strcmp (method, "cubic") || strcmp (method, "*cubic") |
59e2460acae1
make piecewise polynomial (pp) functions more compatible
Kai Habel <kai.habel@gmx.de>
parents:
12459
diff
changeset
|
285 ||strcmp (method, "spline") || strcmp (method, "*spline")) |
59e2460acae1
make piecewise polynomial (pp) functions more compatible
Kai Habel <kai.habel@gmx.de>
parents:
12459
diff
changeset
|
286 yi(:, outliers) = extrap; |
59e2460acae1
make piecewise polynomial (pp) functions more compatible
Kai Habel <kai.habel@gmx.de>
parents:
12459
diff
changeset
|
287 yi = shiftdim(yi, 1); |
59e2460acae1
make piecewise polynomial (pp) functions more compatible
Kai Habel <kai.habel@gmx.de>
parents:
12459
diff
changeset
|
288 else |
59e2460acae1
make piecewise polynomial (pp) functions more compatible
Kai Habel <kai.habel@gmx.de>
parents:
12459
diff
changeset
|
289 yi(outliers, :) = extrap; |
59e2460acae1
make piecewise polynomial (pp) functions more compatible
Kai Habel <kai.habel@gmx.de>
parents:
12459
diff
changeset
|
290 endif |
59e2460acae1
make piecewise polynomial (pp) functions more compatible
Kai Habel <kai.habel@gmx.de>
parents:
12459
diff
changeset
|
291 else |
59e2460acae1
make piecewise polynomial (pp) functions more compatible
Kai Habel <kai.habel@gmx.de>
parents:
12459
diff
changeset
|
292 yi(outliers.') = extrap; |
59e2460acae1
make piecewise polynomial (pp) functions more compatible
Kai Habel <kai.habel@gmx.de>
parents:
12459
diff
changeset
|
293 endif |
9754
4219e5cf773d
improve interp1 and pchip
Jaroslav Hajek <highegg@gmail.com>
parents:
9070
diff
changeset
|
294 endif |
12608
59e2460acae1
make piecewise polynomial (pp) functions more compatible
Kai Habel <kai.habel@gmx.de>
parents:
12459
diff
changeset
|
295 else |
59e2460acae1
make piecewise polynomial (pp) functions more compatible
Kai Habel <kai.habel@gmx.de>
parents:
12459
diff
changeset
|
296 yi.orient = "first"; |
5837 | 297 endif |
298 | |
299 endfunction | |
300 | |
14237
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
301 |
5837 | 302 %!demo |
14237
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
303 %! clf; |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
304 %! xf = 0:0.05:10; yf = sin (2*pi*xf/5); |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
305 %! xp = 0:10; yp = sin (2*pi*xp/5); |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
306 %! lin = interp1 (xp,yp,xf, "linear"); |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
307 %! spl = interp1 (xp,yp,xf, "spline"); |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
308 %! cub = interp1 (xp,yp,xf, "pchip"); |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
309 %! near= interp1 (xp,yp,xf, "nearest"); |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
310 %! plot (xf,yf,"r",xf,near,"g",xf,lin,"b",xf,cub,"c",xf,spl,"m",xp,yp,"r*"); |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
311 %! legend ("original", "nearest", "linear", "pchip", "spline"); |
5837 | 312 %! %-------------------------------------------------------- |
313 %! % confirm that interpolated function matches the original | |
314 | |
315 %!demo | |
14237
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
316 %! clf; |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
317 %! xf = 0:0.05:10; yf = sin (2*pi*xf/5); |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
318 %! xp = 0:10; yp = sin (2*pi*xp/5); |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
319 %! lin = interp1 (xp,yp,xf, "*linear"); |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
320 %! spl = interp1 (xp,yp,xf, "*spline"); |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
321 %! cub = interp1 (xp,yp,xf, "*cubic"); |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
322 %! near= interp1 (xp,yp,xf, "*nearest"); |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
323 %! plot (xf,yf,"r",xf,near,"g",xf,lin,"b",xf,cub,"c",xf,spl,"m",xp,yp,"r*"); |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
324 %! legend ("*original", "*nearest", "*linear", "*cubic", "*spline"); |
5837 | 325 %! %-------------------------------------------------------- |
326 %! % confirm that interpolated function matches the original | |
327 | |
6721 | 328 %!demo |
14237
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
329 %! clf; |
6721 | 330 %! t = 0 : 0.3 : pi; dt = t(2)-t(1); |
331 %! n = length (t); k = 100; dti = dt*n/k; | |
332 %! ti = t(1) + [0 : k-1]*dti; | |
333 %! y = sin (4*t + 0.3) .* cos (3*t - 0.1); | |
14237
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
334 %! ddyc = diff (diff (interp1 (t,y,ti, "cubic")) ./dti)./dti; |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
335 %! ddys = diff (diff (interp1 (t,y,ti, "spline"))./dti)./dti; |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
336 %! ddyp = diff (diff (interp1 (t,y,ti, "pchip")) ./dti)./dti; |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
337 %! plot (ti(2:end-1),ddyc,'g+', ti(2:end-1),ddys,'b*', ti(2:end-1),ddyp,'c^'); |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
338 %! legend ("cubic", "spline", "pchip"); |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
339 %! title ("Second derivative of interpolated 'sin (4*t + 0.3) .* cos (3*t - 0.1)'"); |
6721 | 340 |
9929
45c08d7c2c79
allow discontinuous interpolant in interp1
Jaroslav Hajek <highegg@gmail.com>
parents:
9769
diff
changeset
|
341 %!demo |
14237
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
342 %! clf; |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
343 %! xf = 0:0.05:10; yf = sin (2*pi*xf/5) - (xf >= 5); |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
344 %! xp = [0:.5:4.5,4.99,5:.5:10]; yp = sin (2*pi*xp/5) - (xp >= 5); |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
345 %! lin = interp1 (xp,yp,xf, "linear"); |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
346 %! near= interp1 (xp,yp,xf, "nearest"); |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
347 %! plot (xf,yf,"r", xf,near,"g", xf,lin,"b", xp,yp,"r*"); |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
348 %! legend ("original", "nearest", "linear"); |
9929
45c08d7c2c79
allow discontinuous interpolant in interp1
Jaroslav Hajek <highegg@gmail.com>
parents:
9769
diff
changeset
|
349 %! %-------------------------------------------------------- |
45c08d7c2c79
allow discontinuous interpolant in interp1
Jaroslav Hajek <highegg@gmail.com>
parents:
9769
diff
changeset
|
350 %! % confirm that interpolated function matches the original |
45c08d7c2c79
allow discontinuous interpolant in interp1
Jaroslav Hajek <highegg@gmail.com>
parents:
9769
diff
changeset
|
351 |
12608
59e2460acae1
make piecewise polynomial (pp) functions more compatible
Kai Habel <kai.habel@gmx.de>
parents:
12459
diff
changeset
|
352 ##FIXME: add test for n-d arguments here |
9929
45c08d7c2c79
allow discontinuous interpolant in interp1
Jaroslav Hajek <highegg@gmail.com>
parents:
9769
diff
changeset
|
353 |
6374 | 354 ## For each type of interpolated test, confirm that the interpolated |
355 ## value at the knots match the values at the knots. Points away | |
14359
7277fe922e99
doc: Use Octave preference for double quote in docstrings in scripts/
Rik <octave@nomad.inbox5.com>
parents:
14335
diff
changeset
|
356 ## from the knots are requested, but only "nearest" and "linear" |
6374 | 357 ## confirm they are the correct values. |
358 | |
5837 | 359 %!shared xp, yp, xi, style |
14237
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
360 %! xp = 0:2:10; |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
361 %! yp = sin (2*pi*xp/5); |
6374 | 362 %! xi = [-1, 0, 2.2, 4, 6.6, 10, 11]; |
363 | |
364 ## The following BLOCK/ENDBLOCK section is repeated for each style | |
365 ## nearest, linear, cubic, spline, pchip | |
366 ## The test for ppval of cubic has looser tolerance, but otherwise | |
367 ## the tests are identical. | |
368 ## Note that the block checks style and *style; if you add more tests | |
14237
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
369 ## be sure to add them to both sections of each block. One test, |
6374 | 370 ## style vs. *style, occurs only in the first section. |
371 ## There is an ENDBLOCKTEST after the final block | |
14237
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
372 |
5837 | 373 %!test style = "nearest"; |
6374 | 374 ## BLOCK |
6742 | 375 %!assert (interp1(xp, yp, [min(xp)-1, max(xp)+1],style), [NA, NA]); |
5837 | 376 %!assert (interp1(xp,yp,xp,style), yp, 100*eps); |
377 %!assert (interp1(xp,yp,xp',style), yp', 100*eps); | |
378 %!assert (interp1(xp',yp',xp',style), yp', 100*eps); | |
379 %!assert (interp1(xp',yp',xp,style), yp, 100*eps); | |
380 %!assert (isempty(interp1(xp',yp',[],style))); | |
381 %!assert (isempty(interp1(xp,yp,[],style))); | |
382 %!assert (interp1(xp,[yp',yp'],xi(:),style),... | |
10549 | 383 %! [interp1(xp,yp,xi(:),style),interp1(xp,yp,xi(:),style)]); |
6374 | 384 %!assert (interp1(xp,yp,xi,style),... |
10549 | 385 %! interp1(fliplr(xp),fliplr(yp),xi,style),100*eps); |
6374 | 386 %!assert (ppval(interp1(xp,yp,style,"pp"),xi), |
10549 | 387 %! interp1(xp,yp,xi,style,"extrap"),10*eps); |
6374 | 388 %!error interp1(1,1,1, style); |
5837 | 389 %!assert (interp1(xp,[yp',yp'],xi,style), |
10549 | 390 %! interp1(xp,[yp',yp'],xi,["*",style]),100*eps); |
6374 | 391 %!test style=['*',style]; |
6742 | 392 %!assert (interp1(xp, yp, [min(xp)-1, max(xp)+1],style), [NA, NA]); |
6374 | 393 %!assert (interp1(xp,yp,xp,style), yp, 100*eps); |
394 %!assert (interp1(xp,yp,xp',style), yp', 100*eps); | |
395 %!assert (interp1(xp',yp',xp',style), yp', 100*eps); | |
396 %!assert (interp1(xp',yp',xp,style), yp, 100*eps); | |
397 %!assert (isempty(interp1(xp',yp',[],style))); | |
398 %!assert (isempty(interp1(xp,yp,[],style))); | |
399 %!assert (interp1(xp,[yp',yp'],xi(:),style),... | |
10549 | 400 %! [interp1(xp,yp,xi(:),style),interp1(xp,yp,xi(:),style)]); |
6374 | 401 %!assert (interp1(xp,yp,xi,style),... |
10549 | 402 %! interp1(fliplr(xp),fliplr(yp),xi,style),100*eps); |
6374 | 403 %!assert (ppval(interp1(xp,yp,style,"pp"),xi), |
10549 | 404 %! interp1(xp,yp,xi,style,"extrap"),10*eps); |
6374 | 405 %!error interp1(1,1,1, style); |
406 ## ENDBLOCK | |
14237
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
407 |
6374 | 408 %!test style='linear'; |
409 ## BLOCK | |
6742 | 410 %!assert (interp1(xp, yp, [min(xp)-1, max(xp)+1],style), [NA, NA]); |
6374 | 411 %!assert (interp1(xp,yp,xp,style), yp, 100*eps); |
412 %!assert (interp1(xp,yp,xp',style), yp', 100*eps); | |
413 %!assert (interp1(xp',yp',xp',style), yp', 100*eps); | |
414 %!assert (interp1(xp',yp',xp,style), yp, 100*eps); | |
415 %!assert (isempty(interp1(xp',yp',[],style))); | |
416 %!assert (isempty(interp1(xp,yp,[],style))); | |
417 %!assert (interp1(xp,[yp',yp'],xi(:),style),... | |
10549 | 418 %! [interp1(xp,yp,xi(:),style),interp1(xp,yp,xi(:),style)]); |
6374 | 419 %!assert (interp1(xp,yp,xi,style),... |
10549 | 420 %! interp1(fliplr(xp),fliplr(yp),xi,style),100*eps); |
6374 | 421 %!assert (ppval(interp1(xp,yp,style,"pp"),xi), |
10549 | 422 %! interp1(xp,yp,xi,style,"extrap"),10*eps); |
6374 | 423 %!error interp1(1,1,1, style); |
424 %!assert (interp1(xp,[yp',yp'],xi,style), | |
10549 | 425 %! interp1(xp,[yp',yp'],xi,["*",style]),100*eps); |
6374 | 426 %!test style=['*',style]; |
6742 | 427 %!assert (interp1(xp, yp, [min(xp)-1, max(xp)+1],style), [NA, NA]); |
6374 | 428 %!assert (interp1(xp,yp,xp,style), yp, 100*eps); |
429 %!assert (interp1(xp,yp,xp',style), yp', 100*eps); | |
430 %!assert (interp1(xp',yp',xp',style), yp', 100*eps); | |
431 %!assert (interp1(xp',yp',xp,style), yp, 100*eps); | |
432 %!assert (isempty(interp1(xp',yp',[],style))); | |
433 %!assert (isempty(interp1(xp,yp,[],style))); | |
434 %!assert (interp1(xp,[yp',yp'],xi(:),style),... | |
10549 | 435 %! [interp1(xp,yp,xi(:),style),interp1(xp,yp,xi(:),style)]); |
6374 | 436 %!assert (interp1(xp,yp,xi,style),... |
10549 | 437 %! interp1(fliplr(xp),fliplr(yp),xi,style),100*eps); |
6374 | 438 %!assert (ppval(interp1(xp,yp,style,"pp"),xi), |
10549 | 439 %! interp1(xp,yp,xi,style,"extrap"),10*eps); |
6374 | 440 %!error interp1(1,1,1, style); |
441 ## ENDBLOCK | |
14237
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
442 |
6374 | 443 %!test style='cubic'; |
444 ## BLOCK | |
6742 | 445 %!assert (interp1(xp, yp, [min(xp)-1, max(xp)+1],style), [NA, NA]); |
5837 | 446 %!assert (interp1(xp,yp,xp,style), yp, 100*eps); |
447 %!assert (interp1(xp,yp,xp',style), yp', 100*eps); | |
448 %!assert (interp1(xp',yp',xp',style), yp', 100*eps); | |
449 %!assert (interp1(xp',yp',xp,style), yp, 100*eps); | |
450 %!assert (isempty(interp1(xp',yp',[],style))); | |
451 %!assert (isempty(interp1(xp,yp,[],style))); | |
452 %!assert (interp1(xp,[yp',yp'],xi(:),style),... | |
10549 | 453 %! [interp1(xp,yp,xi(:),style),interp1(xp,yp,xi(:),style)]); |
6374 | 454 %!assert (interp1(xp,yp,xi,style),... |
10549 | 455 %! interp1(fliplr(xp),fliplr(yp),xi,style),100*eps); |
6374 | 456 %!assert (ppval(interp1(xp,yp,style,"pp"),xi), |
10549 | 457 %! interp1(xp,yp,xi,style,"extrap"),100*eps); |
6374 | 458 %!error interp1(1,1,1, style); |
5837 | 459 %!assert (interp1(xp,[yp',yp'],xi,style), |
10549 | 460 %! interp1(xp,[yp',yp'],xi,["*",style]),100*eps); |
6374 | 461 %!test style=['*',style]; |
6742 | 462 %!assert (interp1(xp, yp, [min(xp)-1, max(xp)+1],style), [NA, NA]); |
6374 | 463 %!assert (interp1(xp,yp,xp,style), yp, 100*eps); |
464 %!assert (interp1(xp,yp,xp',style), yp', 100*eps); | |
465 %!assert (interp1(xp',yp',xp',style), yp', 100*eps); | |
466 %!assert (interp1(xp',yp',xp,style), yp, 100*eps); | |
467 %!assert (isempty(interp1(xp',yp',[],style))); | |
468 %!assert (isempty(interp1(xp,yp,[],style))); | |
469 %!assert (interp1(xp,[yp',yp'],xi(:),style),... | |
10549 | 470 %! [interp1(xp,yp,xi(:),style),interp1(xp,yp,xi(:),style)]); |
6374 | 471 %!assert (interp1(xp,yp,xi,style),... |
10549 | 472 %! interp1(fliplr(xp),fliplr(yp),xi,style),100*eps); |
6374 | 473 %!assert (ppval(interp1(xp,yp,style,"pp"),xi), |
10549 | 474 %! interp1(xp,yp,xi,style,"extrap"),100*eps); |
6374 | 475 %!error interp1(1,1,1, style); |
476 ## ENDBLOCK | |
14237
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
477 |
6374 | 478 %!test style='pchip'; |
479 ## BLOCK | |
6742 | 480 %!assert (interp1(xp, yp, [min(xp)-1, max(xp)+1],style), [NA, NA]); |
5837 | 481 %!assert (interp1(xp,yp,xp,style), yp, 100*eps); |
482 %!assert (interp1(xp,yp,xp',style), yp', 100*eps); | |
483 %!assert (interp1(xp',yp',xp',style), yp', 100*eps); | |
484 %!assert (interp1(xp',yp',xp,style), yp, 100*eps); | |
485 %!assert (isempty(interp1(xp',yp',[],style))); | |
486 %!assert (isempty(interp1(xp,yp,[],style))); | |
487 %!assert (interp1(xp,[yp',yp'],xi(:),style),... | |
10549 | 488 %! [interp1(xp,yp,xi(:),style),interp1(xp,yp,xi(:),style)]); |
6374 | 489 %!assert (interp1(xp,yp,xi,style),... |
10549 | 490 %! interp1(fliplr(xp),fliplr(yp),xi,style),100*eps); |
6374 | 491 %!assert (ppval(interp1(xp,yp,style,"pp"),xi), |
10549 | 492 %! interp1(xp,yp,xi,style,"extrap"),10*eps); |
6374 | 493 %!error interp1(1,1,1, style); |
5837 | 494 %!assert (interp1(xp,[yp',yp'],xi,style), |
10549 | 495 %! interp1(xp,[yp',yp'],xi,["*",style]),100*eps); |
6374 | 496 %!test style=['*',style]; |
6742 | 497 %!assert (interp1(xp, yp, [min(xp)-1, max(xp)+1],style), [NA, NA]); |
5837 | 498 %!assert (interp1(xp,yp,xp,style), yp, 100*eps); |
499 %!assert (interp1(xp,yp,xp',style), yp', 100*eps); | |
500 %!assert (interp1(xp',yp',xp',style), yp', 100*eps); | |
501 %!assert (interp1(xp',yp',xp,style), yp, 100*eps); | |
502 %!assert (isempty(interp1(xp',yp',[],style))); | |
503 %!assert (isempty(interp1(xp,yp,[],style))); | |
504 %!assert (interp1(xp,[yp',yp'],xi(:),style),... | |
10549 | 505 %! [interp1(xp,yp,xi(:),style),interp1(xp,yp,xi(:),style)]); |
6374 | 506 %!assert (interp1(xp,yp,xi,style),... |
10549 | 507 %! interp1(fliplr(xp),fliplr(yp),xi,style),100*eps); |
6374 | 508 %!assert (ppval(interp1(xp,yp,style,"pp"),xi), |
10549 | 509 %! interp1(xp,yp,xi,style,"extrap"),10*eps); |
6374 | 510 %!error interp1(1,1,1, style); |
511 ## ENDBLOCK | |
14237
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
512 |
6374 | 513 %!test style='spline'; |
514 ## BLOCK | |
6742 | 515 %!assert (interp1(xp, yp, [min(xp)-1, max(xp)+1],style), [NA, NA]); |
6374 | 516 %!assert (interp1(xp,yp,xp,style), yp, 100*eps); |
517 %!assert (interp1(xp,yp,xp',style), yp', 100*eps); | |
518 %!assert (interp1(xp',yp',xp',style), yp', 100*eps); | |
519 %!assert (interp1(xp',yp',xp,style), yp, 100*eps); | |
520 %!assert (isempty(interp1(xp',yp',[],style))); | |
521 %!assert (isempty(interp1(xp,yp,[],style))); | |
522 %!assert (interp1(xp,[yp',yp'],xi(:),style),... | |
10549 | 523 %! [interp1(xp,yp,xi(:),style),interp1(xp,yp,xi(:),style)]); |
6374 | 524 %!assert (interp1(xp,yp,xi,style),... |
10549 | 525 %! interp1(fliplr(xp),fliplr(yp),xi,style),100*eps); |
6374 | 526 %!assert (ppval(interp1(xp,yp,style,"pp"),xi), |
10549 | 527 %! interp1(xp,yp,xi,style,"extrap"),10*eps); |
6374 | 528 %!error interp1(1,1,1, style); |
5837 | 529 %!assert (interp1(xp,[yp',yp'],xi,style), |
10549 | 530 %! interp1(xp,[yp',yp'],xi,["*",style]),100*eps); |
6374 | 531 %!test style=['*',style]; |
6742 | 532 %!assert (interp1(xp, yp, [min(xp)-1, max(xp)+1],style), [NA, NA]); |
6374 | 533 %!assert (interp1(xp,yp,xp,style), yp, 100*eps); |
534 %!assert (interp1(xp,yp,xp',style), yp', 100*eps); | |
535 %!assert (interp1(xp',yp',xp',style), yp', 100*eps); | |
536 %!assert (interp1(xp',yp',xp,style), yp, 100*eps); | |
537 %!assert (isempty(interp1(xp',yp',[],style))); | |
538 %!assert (isempty(interp1(xp,yp,[],style))); | |
539 %!assert (interp1(xp,[yp',yp'],xi(:),style),... | |
10549 | 540 %! [interp1(xp,yp,xi(:),style),interp1(xp,yp,xi(:),style)]); |
6374 | 541 %!assert (interp1(xp,yp,xi,style),... |
10549 | 542 %! interp1(fliplr(xp),fliplr(yp),xi,style),100*eps); |
6374 | 543 %!assert (ppval(interp1(xp,yp,style,"pp"),xi), |
10549 | 544 %! interp1(xp,yp,xi,style,"extrap"),10*eps); |
6374 | 545 %!error interp1(1,1,1, style); |
546 ## ENDBLOCK | |
547 ## ENDBLOCKTEST | |
5837 | 548 |
549 %!# test linear extrapolation | |
550 %!assert (interp1([1:5],[3:2:11],[0,6],"linear","extrap"), [1, 13], eps); | |
551 %!assert (interp1(xp, yp, [-1, max(xp)+1],"linear",5), [5, 5]); | |
552 | |
553 %!error interp1 | |
554 %!error interp1(1:2,1:2,1,"bogus") | |
555 | |
556 %!assert (interp1(1:2,1:2,1.4,"nearest"),1); | |
557 %!error interp1(1,1,1, "linear"); | |
558 %!assert (interp1(1:2,1:2,1.4,"linear"),1.4); | |
559 %!assert (interp1(1:4,1:4,1.4,"cubic"),1.4); | |
12459
b3f9f98e7640
Fix interp1 tests according to spline update of 07e102029d2a
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
12175
diff
changeset
|
560 %!assert (interp1(1:2,1:2,1.1, "spline"), 1.1); |
5837 | 561 %!assert (interp1(1:3,1:3,1.4,"spline"),1.4); |
562 | |
563 %!error interp1(1,1,1, "*nearest"); | |
564 %!assert (interp1(1:2:4,1:2:4,1.4,"*nearest"),1); | |
565 %!error interp1(1,1,1, "*linear"); | |
6742 | 566 %!assert (interp1(1:2:4,1:2:4,[0,1,1.4,3,4],"*linear"),[NA,1,1.4,3,NA]); |
5837 | 567 %!assert (interp1(1:2:8,1:2:8,1.4,"*cubic"),1.4); |
12459
b3f9f98e7640
Fix interp1 tests according to spline update of 07e102029d2a
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
12175
diff
changeset
|
568 %!assert (interp1(1:2,1:2,1.3, "*spline"), 1.3); |
5837 | 569 %!assert (interp1(1:2:6,1:2:6,1.4,"*spline"),1.4); |
7671
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
570 |
4fbaba9abec1
implement compiled binary lookup
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
571 %!assert (interp1([3,2,1],[3,2,2],2.5),2.5) |
9929
45c08d7c2c79
allow discontinuous interpolant in interp1
Jaroslav Hajek <highegg@gmail.com>
parents:
9769
diff
changeset
|
572 |
45c08d7c2c79
allow discontinuous interpolant in interp1
Jaroslav Hajek <highegg@gmail.com>
parents:
9769
diff
changeset
|
573 %!assert (interp1 ([1,2,2,3,4],[0,1,4,2,1],[-1,1.5,2,2.5,3.5], "linear", "extrap"), [-2,0.5,4,3,1.5]) |
12608
59e2460acae1
make piecewise polynomial (pp) functions more compatible
Kai Habel <kai.habel@gmx.de>
parents:
12459
diff
changeset
|
574 %!assert (interp1 ([4,4,3,2,0],[0,1,4,2,1],[1.5,4,4.5], "linear"), [1.75,1,NA]) |
10820
c44c786f87ba
interp1.m: When absent set X equal to the inices of Y.
Ben Abbott <bpabbott@mac.com>
parents:
10793
diff
changeset
|
575 %!assert (interp1 (0:4, 2.5), 1.5) |