Using MATLAB (779505), страница 46
Текст из файла (страница 46)
The delaunay function returnsa Delaunay triangulation as a set of triangles such that no data points arecontained in any triangle’s circumcircle.12-2112Polynomials and InterpolationYou can use triplot to print the resulting triangles in two-dimensional space.You can also add data for a third dimension to the output of delaunay and plotthe result as a surface with trisurf, or as a mesh with trimesh.Plotting a Delaunay Triangulation. To try delaunay, load the seamount data set andview the longitude (x) and latitude (y) data as a scatter plot.load seamountplot(x,y,'.','markersize',12)xlabel('Longitude'), ylabel('Latitude')grid on−47.95−48−48.05−48.1Latitude−48.15−48.2−48.25−48.3−48.35−48.4−48.45210.8211211.2211.4Longitude211.6211.8Apply Delaunay triangulation and use triplot to overplot the resultingtriangles on the scatter plot.tri = delaunay(x,y);hold on, triplot(tri,x,y), hold off12-22Interpolation−47.95−48−48.05−48.1Latitude−48.15−48.2−48.25−48.3−48.35−48.4−48.45210.8211211.2211.4Longitude211.6211.8Mesh and Surface Plots.
Add the depth data (z) from seamount, to the Delaunaytriangulation, and use trimesh to produce a mesh in three-dimensional space.Similarly, you can use trisurf to produce a surface.figurehidden ontrimesh(tri,x,y,z)grid onxlabel('Longitude'); ylabel('Latitude'); zlabel('Depth in Feet')12-2312Polynomials and Interpolation0Depth in Feet−1000−2000−3000−4000−5000−48−48.1211.6−48.2211.4211.2−48.3−48.4Latitude211210.8LongitudeContour Plots. This code uses meshgrid, griddata, and contour to produce acontour plot of the seamount data.figure[xi,yi] = meshgrid(210.8:.01:211.8,-48.5:.01:-47.9);zi = griddata(x,y,z,xi,yi,'cubic');[c,h] = contour(xi,yi,zi,'b-');clabel(c,h)xlabel('Longitude'), ylabel('Latitude')12-24Interpolation−47.95−4000−48−48.05−3−4000−48.150−2500000−4000000−48.30−250−3−3550Latitude00−30−2000−10000−2 −1500−48.15−48.20−4000−3−−48.250000035−3000−48.3500−4000−4−48.4−48.45210.8211211.2211.4Longitude211.6211.8The arguments for meshgrid encompass the largest and smallest x and yvalues in the original seamount data.
To obtain these values, use min(x),max(x), min(y), and max(y).Closest-Point Searches. You can search through the Delaunay triangulation datawith two functions:• dsearch finds the indices of the (x,y) points in a Delaunay triangulationclosest to the points you specify. This code searches for the point closest to(211.32, -48.35) in the triangulation of the seamount data.xi = 211.32; yi = -48.35;p = dsearch(x,y,tri,xi,yi);[x(p), y(p)]ans =211.3400-48.3700• tsearch, finds the indices into the delaunay output that specify theenclosing triangles of the points you specify. This example uses the index ofthe enclosing triangle for the point (211.32, -48.35) to obtain the coordinatesof the vertices of the triangle.12-2512Polynomials and Interpolationxi = 211.32; yi = -48.35;t = tsearch(x,y,tri,xi,yi);r = tri(t,:);A = [x(r) y(r)]A =211.3000211.3400211.2800-48.3000-48.3700-48.3200Voronoi DiagramsVoronoi diagrams are a closest-point plotting technique related to Delaunaytriangulation.For each point in a set of coplanar points, you can draw a polygon that enclosesall the intermediate points that are closer to that point than to any other pointin the set.
Such a polygon is called a Voronoi polygon, and the set of all Voronoipolygons for a given point set is called a Voronoi diagram.The voronoi function can plot the cells of the Voronoi diagram, or return thevertices of the edges of the diagram. This example loads the seamount data,then uses the voronoi function to produce the Voronoi diagram for thelongitudinal (x) and latitudinal (y) dimensions.
Note that voronoi plots onlythe bounded cells of the Voronoi diagram.load seamountvoronoi(x,y)grid onxlabel('Longitude'), ylabel('Latitude')12-26Interpolation−48−48.05−48.1Latitude−48.15−48.2−48.25−48.3−48.35−48.4210.9211211.1211.2211.3Longitude211.4211.5211.6Note See the voronoi function for an example that uses the vertices of theedges to plot a Voronoi diagram.Tessellation and Interpolation of Scattered Data inHigher DimensionsMany applications in science, engineering, statistics, and mathematics requirestructures like convex hulls, Voronoi diagrams, and Delaunay tessellations.12-2712Polynomials and InterpolationUsing Qhull [1], MATLAB functions enable you to geometrically analyze datasets in any dimension.Functions for Multidimensional Geometrical AnalysisFunctionDescriptionconvhullnn-D convex hull.delaunaynn-D Delaunay tessellation.dsearchnn-D nearest point search.griddatann-D data gridding and hypersurface fitting.tsearchnn-D closest simplex search.voronoinn-D Voronoi diagrams.This section demonstrates these geometric analysis techniques:• Convex hulls• Delaunay triangulations• Voronoi diagrams• Interpolation of scattered multidimensional dataConvex HullsThe convex hull of a data set in n-dimensional space is defined as the smallestconvex region that contains the data set.Computing a Convex Hull.
The convhulln function returns the indices of thepoints in a data set that comprise the facets of the convex hull for the set. Forexample, suppose X is an 8-by-3 matrix that consists of the 8 vertices of a cube.The convex hull of X then consists of 12 facets.d = [-1 1];[x,y,z] = meshgrid(d,d,d);X = [x(:),y(:),z(:)];C = convhulln(X)C =12-28% 8 corner points of a cubeInterpolation312787626842121378862424553553558338Because the data is three-dimensional, the facets that make up the convex hullare triangles. The 12 rows of C represent 12 triangles.
The elements of C areindices of points in X. For example, the first row, 3 1 5, means that the firsttriangle has X(3,:), X(1,:), and X(5,:) as its vertices.For three-dimensional convex hulls, you can use trisurf to plot the output.However, using patch to plot the output gives you more control over the colorof the facets. Note that you cannot plot convhulln output for n > 3.This code plots the convex hull by drawing the triangles as three-dimensionalpatches.figure, hold ond = [1 2 3 1% Index into C column.for i = 1:size(C,1) % Draw each triangle.j= C(i,d);% Get the ith C to make a patch.h(i)=patch(X(j,1),X(j,2),X(j,3),i,'FaceAlpha',0.9);end% 'FaceAlpha' is used to make it transparent.hold offview(3), axis equal, axis offcamorbit(90,-5);% To view it from another angletitle('Convex hull of a cube')12-2912Polynomials and InterpolationDelaunay TessellationsA Delaunay tessellation is a set of simplices such that no data points arecontained in any simplex’s circumsphere.
In two-dimensional space, a simplexis a triangle. In three-dimensional space, a simplex is a tetrahedron.Computing a Delaunay Tessellation. The delaunayn function returns the indices ofthe points in a data set that comprise the simplices of an n-dimensionalDelaunay tessellation of the data set.This example uses the same X as in the convex hull example, i.e. the 8 cornerpoints of a cube, with the addition of a center point.d = [-1 1];[x,y,z] = meshgrid(d,d,d);X = [x(:),y(:),z(:)];% 8 corner points of a cubeX(9,:) = [0 0 0];% Add center to the vertex list.T = delaunayn(X)% Generate Delaunay tessellation.12-30InterpolationT =932227788888199339372237511995999993656416566449The 12 rows of T represent the 12 simplices, in this case irregular tetrahedrons,that partition the cube.
Each row represents one tetrahedron, and the rowelements are indices of points in X.For three-dimensional tessellations, you can use tetramesh to plot the output.However, using patch to plot the output gives you more control over the colorof the facets. Note that you cannot plot delaunayn output for n > 3.This code plots the tessellation T by drawing the tetrahedrons usingthree-dimensional patches.figure, hold ond = [1 1 1 2; 2 2 3 3; 3 4 4 4]; % Index into Tfor i = 1:size(T,1)% Draw each tetrahedron.y = T(i,d);% Get the ith T to make a patch.x1 = reshape(X(y,1),3,4);x2 = reshape(X(y,2),3,4);x3 = reshape(X(y,3),3,4);h(i)=patch(x1,x2,x3,(1:4)*i,'FaceAlpha',0.9);endhold offview(3), axis equalaxis offcamorbit(65,120)% To view it from another angletitle('Delaunay tessellation of a cube with a center point')You can use cameramenu to rotate the figure in any direction.12-3112Polynomials and InterpolationVoronoi DiagramsGiven m data points in n-dimensional space, a Voronoi diagram is the partitionof n-dimensional space into m polyhedral regions, one region for each datapoint.
Such a region is called a Voronoi cell. A Voronoi cell satisfies thecondition that it contains all points that are closer to its data point than anyother data point in the set.Computing a Voronoi Diagram. The voronoin function returns two outputs:• V is an m-by-n matrix of m points in n-space. Each row of V represents aVoronoi vertex.• C is a cell array of vectors. Each vector in the cell array C represents a Voronoicell.















