Mercurial > hg > octave-nkf
annotate scripts/geometry/griddata.m @ 14237:11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Add clf() to all demos using plot features to get reproducibility.
Use 64 as input to all colormaps (jet (64)) to get reproducibility.
* bicubic.m, cell2mat.m, celldisp.m, cplxpair.m, interp1.m, interp2.m,
interpft.m, interpn.m, profile.m, profshow.m, convhull.m, delaunay.m,
griddata.m, inpolygon.m, voronoi.m, autumn.m, bone.m, contrast.m, cool.m,
copper.m, flag.m, gmap40.m, gray.m, hot.m, hsv.m, image.m, imshow.m, jet.m,
ocean.m, pink.m, prism.m, rainbow.m, spring.m, summer.m, white.m, winter.m,
condest.m, onenormest.m, axis.m, clabel.m, colorbar.m, comet.m, comet3.m,
compass.m, contour.m, contour3.m, contourf.m, cylinder.m, daspect.m,
ellipsoid.m, errorbar.m, ezcontour.m, ezcontourf.m, ezmesh.m, ezmeshc.m,
ezplot.m, ezplot3.m, ezpolar.m, ezsurf.m, ezsurfc.m, feather.m, fill.m,
fplot.m, grid.m, hold.m, isosurface.m, legend.m, loglog.m, loglogerr.m,
pareto.m, patch.m, pbaspect.m, pcolor.m, pie.m, pie3.m, plot3.m, plotmatrix.m,
plotyy.m, polar.m, quiver.m, quiver3.m, rectangle.m, refreshdata.m, ribbon.m,
rose.m, scatter.m, scatter3.m, semilogx.m, semilogxerr.m, semilogy.m,
semilogyerr.m, shading.m, slice.m, sombrero.m, stairs.m, stem.m, stem3.m,
subplot.m, surf.m, surfc.m, surfl.m, surfnorm.m, text.m, title.m, trimesh.m,
triplot.m, trisurf.m, uigetdir.m, uigetfile.m, uimenu.m, uiputfile.m,
waitbar.m, xlim.m, ylim.m, zlim.m, mkpp.m, pchip.m, polyaffine.m, spline.m,
bicgstab.m, cgs.m, gplot.m, pcg.m, pcr.m, treeplot.m, strtok.m, demo.m,
example.m, rundemos.m, speed.m, test.m, calendar.m, datestr.m, datetick.m,
weekday.m: Revamp %!demos to use Octave coding conventions on spacing, etc.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Fri, 20 Jan 2012 12:59:53 -0800 |
parents | 72c96de7a403 |
children | c4fa5e0b6193 |
rev | line source |
---|---|
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
11588
diff
changeset
|
1 ## Copyright (C) 1999-2012 Kai Habel |
6823 | 2 ## |
3 ## This file is part of Octave. | |
4 ## | |
5 ## Octave is free software; you can redistribute it and/or modify it | |
6 ## under the terms of the GNU General Public License as published by | |
7016 | 7 ## the Free Software Foundation; either version 3 of the License, or (at |
8 ## your option) any later version. | |
6823 | 9 ## |
10 ## Octave is distributed in the hope that it will be useful, but | |
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 ## General Public License for more details. | |
14 ## | |
15 ## You should have received a copy of the GNU General Public License | |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
6823 | 18 |
19 ## -*- texinfo -*- | |
10793
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
20 ## @deftypefn {Function File} {@var{zi} =} griddata (@var{x}, @var{y}, @var{z}, @var{xi}, @var{yi}, @var{method}) |
6823 | 21 ## @deftypefnx {Function File} {[@var{xi}, @var{yi}, @var{zi}] =} griddata (@var{x}, @var{y}, @var{z}, @var{xi}, @var{yi}, @var{method}) |
10945
aa40bdbfa478
griddata.m: Allow x, y inputs to be vectors, and z a matrix.
Ben Abbott <bpabbott@mac.com>
parents:
10793
diff
changeset
|
22 ## |
6823 | 23 ## Generate a regular mesh from irregular data using interpolation. |
24 ## The function is defined by @code{@var{z} = f (@var{x}, @var{y})}. | |
10945
aa40bdbfa478
griddata.m: Allow x, y inputs to be vectors, and z a matrix.
Ben Abbott <bpabbott@mac.com>
parents:
10793
diff
changeset
|
25 ## Inputs @code{@var{x}, @var{y}, @var{z}} are vectors of the same length |
aa40bdbfa478
griddata.m: Allow x, y inputs to be vectors, and z a matrix.
Ben Abbott <bpabbott@mac.com>
parents:
10793
diff
changeset
|
26 ## or @code{@var{x}, @var{y}} are vectors and @code{@var{z}} is matrix. |
aa40bdbfa478
griddata.m: Allow x, y inputs to be vectors, and z a matrix.
Ben Abbott <bpabbott@mac.com>
parents:
10793
diff
changeset
|
27 ## |
6823 | 28 ## The interpolation points are all @code{(@var{xi}, @var{yi})}. If |
11563
3c6e8aaa9555
Grammarcheck m-files before 3.4 release.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
29 ## @var{xi}, @var{yi} are vectors then they are made into a 2-D mesh. |
6823 | 30 ## |
6826 | 31 ## The interpolation method can be @code{"nearest"}, @code{"cubic"} or |
32 ## @code{"linear"}. If method is omitted it defaults to @code{"linear"}. | |
6823 | 33 ## @seealso{delaunay} |
34 ## @end deftypefn | |
35 | |
10549 | 36 ## Author: Kai Habel <kai.habel@gmx.de> |
6823 | 37 ## Adapted-by: Alexander Barth <barth.alexander@gmail.com> |
10945
aa40bdbfa478
griddata.m: Allow x, y inputs to be vectors, and z a matrix.
Ben Abbott <bpabbott@mac.com>
parents:
10793
diff
changeset
|
38 ## xi and yi are not "meshgridded" if both are vectors |
6823 | 39 ## of the same size (for compatibility) |
40 | |
6826 | 41 function [rx, ry, rz] = griddata (x, y, z, xi, yi, method) |
10945
aa40bdbfa478
griddata.m: Allow x, y inputs to be vectors, and z a matrix.
Ben Abbott <bpabbott@mac.com>
parents:
10793
diff
changeset
|
42 |
6823 | 43 if (nargin == 5) |
6826 | 44 method = "linear"; |
6823 | 45 endif |
10945
aa40bdbfa478
griddata.m: Allow x, y inputs to be vectors, and z a matrix.
Ben Abbott <bpabbott@mac.com>
parents:
10793
diff
changeset
|
46 if (nargin < 5 || nargin > 7) |
6826 | 47 print_usage (); |
6823 | 48 endif |
49 | |
6826 | 50 if (ischar (method)) |
51 method = tolower (method); | |
6823 | 52 endif |
10945
aa40bdbfa478
griddata.m: Allow x, y inputs to be vectors, and z a matrix.
Ben Abbott <bpabbott@mac.com>
parents:
10793
diff
changeset
|
53 |
aa40bdbfa478
griddata.m: Allow x, y inputs to be vectors, and z a matrix.
Ben Abbott <bpabbott@mac.com>
parents:
10793
diff
changeset
|
54 if (isvector (x) && isvector (y) && all ([numel(y), numel(x)] == size (z))) |
aa40bdbfa478
griddata.m: Allow x, y inputs to be vectors, and z a matrix.
Ben Abbott <bpabbott@mac.com>
parents:
10793
diff
changeset
|
55 [x, y] = meshgrid (x, y); |
aa40bdbfa478
griddata.m: Allow x, y inputs to be vectors, and z a matrix.
Ben Abbott <bpabbott@mac.com>
parents:
10793
diff
changeset
|
56 elseif (! all (size (x) == size (y) & size (x) == size (z))) |
aa40bdbfa478
griddata.m: Allow x, y inputs to be vectors, and z a matrix.
Ben Abbott <bpabbott@mac.com>
parents:
10793
diff
changeset
|
57 if (isvector (z)) |
11588
d5bd2766c640
style fixes for warning and error messages in script files
John W. Eaton <jwe@octave.org>
parents:
11563
diff
changeset
|
58 error ("griddata: X, Y, and Z, be vectors of same length"); |
10945
aa40bdbfa478
griddata.m: Allow x, y inputs to be vectors, and z a matrix.
Ben Abbott <bpabbott@mac.com>
parents:
10793
diff
changeset
|
59 else |
11588
d5bd2766c640
style fixes for warning and error messages in script files
John W. Eaton <jwe@octave.org>
parents:
11563
diff
changeset
|
60 error ("griddata: lengths of X, Y must match the columns and rows of Z"); |
10945
aa40bdbfa478
griddata.m: Allow x, y inputs to be vectors, and z a matrix.
Ben Abbott <bpabbott@mac.com>
parents:
10793
diff
changeset
|
61 endif |
6823 | 62 endif |
10945
aa40bdbfa478
griddata.m: Allow x, y inputs to be vectors, and z a matrix.
Ben Abbott <bpabbott@mac.com>
parents:
10793
diff
changeset
|
63 |
9677
8cf522ce9c4d
fix griddata with vectors
Jaroslav Hajek <highegg@gmail.com>
parents:
9501
diff
changeset
|
64 ## Meshgrid xi and yi if they are a row and column vector. |
8cf522ce9c4d
fix griddata with vectors
Jaroslav Hajek <highegg@gmail.com>
parents:
9501
diff
changeset
|
65 if (rows (xi) == 1 && columns (yi) == 1) |
6826 | 66 [xi, yi] = meshgrid (xi, yi); |
6823 | 67 endif |
68 | |
9677
8cf522ce9c4d
fix griddata with vectors
Jaroslav Hajek <highegg@gmail.com>
parents:
9501
diff
changeset
|
69 if (! size_equal (xi, yi)) |
11472
1740012184f9
Use uppercase for variable names in error() strings to match Info documentation. Only m-files done.
Rik <octave@nomad.inbox5.com>
parents:
10945
diff
changeset
|
70 error ("griddata: XI and YI must be vectors or matrices of same size"); |
6823 | 71 endif |
72 | |
6826 | 73 [nr, nc] = size (xi); |
10945
aa40bdbfa478
griddata.m: Allow x, y inputs to be vectors, and z a matrix.
Ben Abbott <bpabbott@mac.com>
parents:
10793
diff
changeset
|
74 |
9501 | 75 x = x(:); |
76 y = y(:); | |
77 z = z(:); | |
9495 | 78 |
8507 | 79 ## Triangulate data. |
6826 | 80 tri = delaunay (x, y); |
10548
479536c5bb10
Replace lowercase nan with NaN for visual cue in scripts
Rik <code@nomad.inbox5.com>
parents:
9677
diff
changeset
|
81 zi = NaN (size (xi)); |
10945
aa40bdbfa478
griddata.m: Allow x, y inputs to be vectors, and z a matrix.
Ben Abbott <bpabbott@mac.com>
parents:
10793
diff
changeset
|
82 |
6826 | 83 if (strcmp (method, "cubic")) |
8664 | 84 error ("griddata: cubic interpolation not yet implemented"); |
6823 | 85 |
6826 | 86 elseif (strcmp (method, "nearest")) |
8507 | 87 ## Search index of nearest point. |
6826 | 88 idx = dsearch (x, y, tri, xi, yi); |
89 valid = !isnan (idx); | |
6823 | 90 zi(valid) = z(idx(valid)); |
91 | |
6826 | 92 elseif (strcmp (method, "linear")) |
8507 | 93 ## Search for every point the enclosing triangle. |
94 tri_list = tsearch (x, y, tri, xi(:), yi(:)); | |
6823 | 95 |
8507 | 96 ## Only keep the points within triangles. |
9496 | 97 valid = !isnan (tri_list); |
98 tri_list = tri_list(valid); | |
6826 | 99 nr_t = rows (tri_list); |
6823 | 100 |
9496 | 101 tri = tri(tri_list,:); |
102 | |
8507 | 103 ## Assign x,y,z for each point of triangle. |
9496 | 104 x1 = x(tri(:,1)); |
105 x2 = x(tri(:,2)); | |
106 x3 = x(tri(:,3)); | |
6826 | 107 |
9496 | 108 y1 = y(tri(:,1)); |
109 y2 = y(tri(:,2)); | |
110 y3 = y(tri(:,3)); | |
6826 | 111 |
9496 | 112 z1 = z(tri(:,1)); |
113 z2 = z(tri(:,2)); | |
114 z3 = z(tri(:,3)); | |
6823 | 115 |
8507 | 116 ## Calculate norm vector. |
6826 | 117 N = cross ([x2-x1, y2-y1, z2-z1], [x3-x1, y3-y1, z3-z1]); |
9496 | 118 ## Normalize. |
119 N = diag (norm (N, "rows")) \ N; | |
10945
aa40bdbfa478
griddata.m: Allow x, y inputs to be vectors, and z a matrix.
Ben Abbott <bpabbott@mac.com>
parents:
10793
diff
changeset
|
120 |
8507 | 121 ## Calculate D of plane equation |
122 ## Ax+By+Cz+D = 0; | |
6823 | 123 D = -(N(:,1) .* x1 + N(:,2) .* y1 + N(:,3) .* z1); |
10945
aa40bdbfa478
griddata.m: Allow x, y inputs to be vectors, and z a matrix.
Ben Abbott <bpabbott@mac.com>
parents:
10793
diff
changeset
|
124 |
8507 | 125 ## Calculate zi by solving plane equation for xi, yi. |
9677
8cf522ce9c4d
fix griddata with vectors
Jaroslav Hajek <highegg@gmail.com>
parents:
9501
diff
changeset
|
126 zi(valid) = -(N(:,1).*xi(:)(valid) + N(:,2).*yi(:)(valid) + D) ./ N(:,3); |
10945
aa40bdbfa478
griddata.m: Allow x, y inputs to be vectors, and z a matrix.
Ben Abbott <bpabbott@mac.com>
parents:
10793
diff
changeset
|
127 |
6823 | 128 else |
11472
1740012184f9
Use uppercase for variable names in error() strings to match Info documentation. Only m-files done.
Rik <octave@nomad.inbox5.com>
parents:
10945
diff
changeset
|
129 error ("griddata: unknown interpolation METHOD"); |
6823 | 130 endif |
131 | |
6826 | 132 if (nargout == 3) |
6823 | 133 rx = xi; |
134 ry = yi; | |
135 rz = zi; | |
6826 | 136 elseif (nargout == 1) |
6823 | 137 rx = zi; |
6826 | 138 elseif (nargout == 0) |
139 mesh (xi, yi, zi); | |
6823 | 140 endif |
141 endfunction | |
142 | |
14237
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
143 |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
144 %!demo |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
145 %! clf; |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
146 %! x = 2*rand (100,1) - 1; |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
147 %! y = 2*rand (size (x)) - 1; |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
148 %! z = sin (2*(x.^2 + y.^2)); |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
149 %! [xx,yy] = meshgrid (linspace (-1,1,32)); |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
150 %! griddata (x,y,z,xx,yy); |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
151 %! title ("nonuniform grid sampled at 100 points"); |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
152 |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
153 %!demo |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
154 %! clf; |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
155 %! x = 2*rand (1000,1) - 1; |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
156 %! y = 2*rand (size (x)) - 1; |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
157 %! z = sin (2*(x.^2 + y.^2)); |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
158 %! [xx,yy] = meshgrid (linspace (-1,1,32)); |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
159 %! griddata (x,y,z,xx,yy); |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
160 %! title ("nonuniform grid sampled at 1000 points"); |
6823 | 161 |
162 %!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
|
163 %! clf; |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
164 %! x = 2*rand (1000,1) - 1; |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
165 %! y = 2*rand (size (x)) - 1; |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
166 %! z = sin (2*(x.^2 + y.^2)); |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
167 %! [xx,yy] = meshgrid (linspace (-1, 1, 32)); |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
168 %! griddata (x,y,z,xx,yy,"nearest"); |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
169 %! title ("nonuniform grid sampled at 1000 points with nearest neighbor"); |
6823 | 170 |
14237
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
171 %!testif HAVE_QHULL |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
172 %! [xx,yy] = meshgrid (linspace (-1,1,32)); |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
173 %! x = xx(:); |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
174 %! x = x + 10*(2*round (rand (size(x))) - 1) * eps; |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
175 %! y = yy(:); |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
176 %! y = y + 10*(2*round (rand (size(y))) - 1) * eps; |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
177 %! z = sin (2*(x.^2 + y.^2)); |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
178 %! zz = griddata (x,y,z,xx,yy,"linear"); |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
179 %! zz2 = sin (2*(xx.^2 + yy.^2)); |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
180 %! zz2(isnan (zz)) = NaN; |
11949c9795a0
Revamp %!demos in m-files to use Octave coding conventions on spacing, etc.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
181 %! assert (zz, zz2, 100*eps); |
6823 | 182 |