# LEVELS Choosing Contour Level Values

LEVELS is a MATLAB program which draws a contour plot of a function, using sampling to determine the appropriate contour levels.

MATLAB's contour function displays a contour plot of a data, described either as a table Z(X,Y), or as a function F(X,Y). (From here on, we'll assume that the functional description is being used.) The user can specify the number of contour levels, and can also specify the values of the contour levels, but it is common to let MATLAB handle this task with default settings.

But for some functions, the evenly spaced contour levels, from FMIN to FMAX, will not do a good job of showing the "shape" of the data. This typically happens where there are some extreme, localized values of the function, or when a large area of the region has roughly the same value. In such cases, the contour lines will cluster around a small area of rapid variation, and ignore the rest of the region.

LEVELS offers a simple solution. It simply samples the data at a random number of points and uses those values as the contour levels. The reason this helps is that this procedure will tend to use more contour lines exactly in those ranges that occur more often. If a large area has roughly the same value, then there will be many more contour levels used to try to discriminate the behavior in that area.

### Usage:

LEVELS_XY is appropriate when the variables X and Y are treated separately.

levels_xy ( @f, level_num, x_range, y_range )
where
• @f is the name of the function that evaluates the data to be contoured. This function should have the form
function z = f(x,y)
• level_num is the number of contour levels to use; if omitted, 50 will be used.
• x_range contains [x_min, x_max], defining the range of the X variable. If omitted, [-1,+1] is used.
• y_range contains [y_min, y_max], defining the range of the Y variable. If omitted, the X range is used.

### Languages:

LEVELS is available in a MATLAB version.

### Related Data and Programs:

CONTOUR_SEQUENCE4, a MATLAB program which expects a separate node coordinate file of the XY coordinates, and a separate file for the scalar quantity.

DIST_PLOT, a MATLAB program which makes contour plots of the distance function, as defined and used in Persson and Strang's distmesh code;

HYPERSPHERE_SURFACE, a MATLAB library which illustrates a procedure for estimating the location of a hypersurface defined by a characteristic function or a signed function.

MATLAB_SURF, a MATLAB library which demonstrates the MATLAB surf() function for displaying a 3D surface of the form Z=F(X,Y).

TRIANGULATION_ORDER3_CONTOUR, a MATLAB program which makes contour plot of scattered data, or of data defined on an order 3 triangulation.

TRIANGULATION_ORDER6_CONTOUR, a MATLAB program which makes contour plots of data defined on an order 6 triangulation.

VECTOR_MAGNITUDE_GRID, a MATLAB program which reads vector data at "scattered" points, computes an interpolatory function, evaluates it on a uniform grid of user-specified density, and produces a contour plot of the vector magnitude.

### Source Code:

• levels.m, the code to use when the function expects a single XY input vector variable.
• levels_xy.m, the code to use when the function expects a a pair of X, Y input scalar variables.

### Examples and Tests:

BEALE is the Beale function.

• beale_xy.m, defines the function in terms of X and Y.
• beale_xy.png, a PNG image created by
levels_xy ( @beale_xy, 50, [-5,5], [-5,5] )

BOHACH1 is the Bohachevsky function #1.

• bohach1_xy.m, defines the function in terms of X and Y.
• bohach1_xy.png, a PNG image created by
levels_xy ( @bohach1_xy, 50, [-0.75,0.75], [-0.75,+0.75] )

BOHACH2 is the Bohachevsky function #2.

• bohach2_xy.m, defines the function in terms of X and Y.
• bohach2_xy.png, a PNG image created by
levels_xy ( @bohach2_xy, 50, [-1,+1], [-1,+1] )

GOLDSTEIN_PRICE is the Goldstein-Price function.

LOCAL is a badly scaled function with a local minimum. The local minimum is at roughly X=(0.285,0.279) with F(X)=5.92 while the global minimum is at roughly X=(-21.02,-36.76) with F(X)=0. Because of the nature of this function, minimization algorithms may have a very hard time.

• local_xy.m, defines the function in terms of X and Y.
• local_xy.png, a PNG image created by
levels_xy ( @local_xy, 50, [-35,20], [-40,15] )

You can go up one level to the MATLAB source codes.

Last revised on 09 February 2008.