How does Matlab calculate contour lines?

What is the algorithm used by Matlab to create contour lines? In other words, how does it convert level data into a grid into a set of rows?

I would like to:

  • local criterion for obtaining points lying on the contour?
  • global procedure for capturing all contour lines?

I do not need detailed information about the base code, but general principles would be useful for interpreting the output. I use contour (and derivatives) in my research and want to get an idea of ​​the numerical errors that are introduced at this point.

This is a very simple question, but I could not find an explanation in the Matlab documentation and did not find anything on SO or elsewhere on the Internet. We apologize if it’s still easy to find.

+8
algorithm matlab contour
source share
2 answers

From MATLAB® - Graphics - R2012a , from page 5-73 on page 5-76:

Contour algorithm

The contourc function computes the contour matrix for another contour function. This is a low-level function that is not invoked from the command line. The outline algorithm first determines which outline levels to draw. If you specified an input vector v , the elements of v are the values ​​of the contour level, and length(v) determines the number of contour levels formed. If you do not specify v , the algorithm selects no more than 20 contour levels, which are divided into 2 or 5.

A matrix of height Z is associated with X and Y matrices that are located each value in Z at the intersection of a row and column, or these matrices are deduced when they are not defined. The row and column widths can vary, but they are usually constant (i.e., Z is a regular grid). Before calling contourc to interpolate the contours, contourf populates the height matrix with an additional row or column on each edge. It assigns z-values ​​to the added grid cells, which are significantly lower than the minimum matrix value. The added values ​​allow the contours close to the border of the matrix so that they can be filled with color. When contourc creates a contour matrix, it replaces the x, y coordinates containing low z values ​​with NaN to prevent the line contours that are transmitted along the borders of the matrix. This is why the contour matrices returned by contourf sometimes contain NaN values. Set the current level, c , equal to the lowest level of the circuit built in the range [min(Z) max(Z)] . The contour algorithm checks each edge of each square in the grid to see if c between two z values ​​for the edge points. If so, the contour at this level crosses the edge, and linear interpolation is performed:

 t=(c-Z0)/(Z1-Z0) 

Z0 is the value of z at one boundary point, and Z1 is the value of z at another boundary point.

Start indexing a new contour line ( i=1 ) for level c by interpolating x and y:

 cx(i) = X0+t*(X1-X0) cy(i) = Y0+t*(Y1-Y0) 

Go around the edges of the square you just entered; the outline comes out of the next edge with z values ​​that are brackets c . Increment i , calculate t for the edge, and then calculate cx(i) and cy(i) as above. mark on the area visited. Continue to check the edges of each square entered to determine the exit border until the line ( cx,cy ) closes at the starting point or exits the grid. If the square entered is already marked, the contour line closes there. Copy cx , cy , c and i to the data structure of the contour line (matrix returned by the contour functions described in the near future).

Re-initialize cx , cy and i . Go to the unsigned square and check its edges for intersections; when you find one at level c , repeat the previous steps. Any number of contour lines can exist for a given level. Clear all markers, increase the contour level and repeat until c exceeds max(Z) . For squares, additional logic is needed where the contour passes through all four edges (saddle points) to determine which pairs of edges to connect. contour , contour3 and contourf returns a two-line matrix that defines the entire contour of the row:

 C = [ value1 xdata(1) xdata(2)... numv ydata(1) ydata(2)...] 

The first row of the column, which begins with each path definition, contains the path value as indicated by v and is used by clabel . This value refers to the number (x, y) of vertices in a contour line. The remaining columns contain data for pairs (x, y). For example, the contour matrix calculated by C = contour(peaks(3)) is as follows.

enter image description here

Quota values ​​begin with each outline line definition.

+4
source share

You can read about Marching Squares. ( https://en.wikipedia.org/wiki/Marching_squares )

Typically, linear interpolation is done around the edges of a mesh cell, providing the contour points that you connect to form polylines.

When the cells are too coarse, preliminary interpolation, for example bicubic, can be done to refine the grid.

+3
source share

All Articles