Detailed and comprehensive MATLAB drawing tutorial

Source: Internet
Author: User
Tags in degrees

MATLAB plot

The powerful drawing function is one of the characteristics of MATLAB. MATLAB provides a series of drawing functions. You don't need to think too much about drawing details, but you only need to give some basic parameters to obtain the required graphics, such functions are called high-level plotting functions. In addition, Matlab also provides a low-level Drawing operation to operate the graphic handle directly. This type of operation treats every Graphic Element (such as the coordinate axis, curve, and text) of a graph as an independent object, and the system assigns a handle to each object, you can use a handle to operate the graphic element without affecting other parts.

This chapter introduces the use of high-level plotting functions and other graphic control functions for drawing 2D and 3D images. On this basis, it introduces the operation of low-level plotting for various graphic objects.

1. Two-dimensional Drawing

A two-dimensional chart is a plane chart that connects data points on a plane coordinate. Different coordinate systems can be used, such as Cartesian coordinates, logarithm coordinates, and polar coordinates. Drawing Two-dimensional images is the basis of other drawing operations.

1. Basic functions for drawing two-dimensional Curves

In Matlab, the most basic and widely used plotting function is plot, which can draw different curves on two-dimensional planes.

1. Basic usage of the plot Function

The plot function is used to draw a linear coordinate curve on a two-dimensional plane. To provide a set of X coordinates and corresponding Y coordinates, you can draw a two-dimensional curve with X and Y as the horizontal and vertical coordinates respectively. Application Format of the plot Function

Plot (x, y) where X and Y are vectors of the same length, and the X and Y coordinates are stored.

Example 51 plot a curve in the range of [0, 2 Pi]

The program is as follows: Enter the following command in the Command window:

> X = 0: PI/100:2 * PI;

> Y = 2 * exp (-0.5 * X). * sin (2 * pI * X );

> Plot (x, y)

After the program is executed, open a graphical window and draw the following Curve

Note: The exponential function and the sine function must use the dot Multiplication operation because the two are vectors.

 

Example 52 plot a curve

This is a curve equation given in the form of parameters. As long as the parameter vectors are given and then the X and Y vectors are obtained respectively, the curve can be output:

> T =-Pi: PI/100: PI;

> X = T. * Cos (3 * t );

> Y = T. * sin (T). * sin (t );

> Plot (x, y)

After the program is executed, open a graphical window and draw the following Curve

 

As mentioned above, the independent variable X and Y of the plot function are vectors of the same length. This is the most common and basic usage. There are still some changes in the actual application. Description:

2. Plot function with multiple input parameters

The plot function can contain several sets of vector pairs, and each group can draw a curve. The Calling format of the plot function containing multiple input parameters is: plot (x1, Y1, X2, Y2 ,..., XN, yn)

Use the following command to draw three curves in the same coordinate.

> X = linspace (100 * Pi );

> Plot (x, sin (x), X, 2 * sin (x), X, 3 * sin (x ))

 

When the input parameters are in the matrix form, the X and Y pairs draw curves based on the corresponding column elements as the horizontal and vertical coordinates, and the number of curves equals to the number of columns in the matrix.

 

> X = linspace (100 * Pi );

> Y1 = sin (X );

> Y2 = 2 * sin (X );

> Y3 = 3 * sin (X );

> X = [X; X] ';

> Y = [Y1; Y2; Y3] ';

> Plot (X, Y, X, cos (x ))

All arrays X and Y contain three columns. They form input parameter pairs and draw three curves. X and cos (x) form a pair to draw a cosine curve.

The plot function can be used to directly plot the matrix data in a graphical form. In this case, the plot function draws each column of the Matrix data as a curve in the form. For example

> A = Pascal (5)

A =

1 1 1 1

1 2 3 4 5

1 3 6 10 15

1 4 10 20 35

1 5 15 35 70

> Plot ()

 

3. Plot functions with options

MATLAB provides some plotting options for determining the line type, color, and data point marking of the drawn curve. These options are shown in the table:

Line Type

Color

Markup symbol

-Solid line

B blue

.

S Square

: Dotted line

G green

O circle

D-Diamond

-. Dashes

R red

× Cross

Vertical triangle

-- Double dashes

C-blue

+ Plus sign

Forward-up triangle symbol

 

M pinhong

* Asterisks

<Left-facing triangle symbol

 

Y yellow

 

> Right-to-right triangle symbol

 

K black

 

P pentagram

 

W white

 

H hexagonal star

 

For example, draw a curve and its envelope line in the same coordinate with different line types and colors.

> X = (0: PI/100:2 * PI )';

> Y1 = 2 * exp (-0.5 * X) * [1,-1];

> Y2 = 2 * exp (-0.5 * X). * sin (2 * pI * X );

> X1 = (0: 12)/2;

> Y3 = 2 * exp (-0.5 * X1). * sin (2 * pI * X1 );

> Plot (x, Y1, 'K: ', X, Y2,' B -- ', X1, Y3, 'rp ');

 

The plot function contains three groups of drawing parameters. The first group draws two envelopes with a black dotted line, the second group draws a curve y with a blue dash, and the third group marks the data points with a red pentagram discretely.

4. Double ordinate function plotyy

In Matlab, if you need to draw two graphs with different ordinate scales, you can use the plotyy function to draw two functions with different dimensions and different orders of magnitude in the same coordinate, it is conducive to comparative analysis of graphic data. Format: plotyy (x1, Y1, X2, Y2)

X1 and Y1 correspond to one curve, while X2 and Y2 correspond to another curve. The scale of the horizontal coordinates is the same. There are two vertical coordinates, the corresponding X1 and Y1 data pairs on the left, and the corresponding X2 and Y2 on the right.

Example: (omitted)

2. auxiliary operations for Drawing Images

After drawing the image, you may need to perform some auxiliary operations on the image to make it clearer and more readable.

1. Graphic tagging

When drawing a graph, you can add some instructions to the graph, such as the shape name, coordinate axis description, and the meaning of a certain part of the graph. These operations are called adding a graphic annotation. The call format for graphic annotation functions is:

Title ('graphic name') (all placed in single quotes)

Xlabel ('x axis description ')

Ylabel ('y axis description ')

Text (X, Y, 'graphic description ')

Legend ('legend 1', 'legend 2 ',...) P190

The title, xlabel, and ylabel functions are used to describe the names of the graphics and coordinate axes respectively. The text function adds graphical descriptions at coordinate points (x, y. (P88 or gtext command ). The legend function is used to mark a legend with a line type, color, or data point used to draw a curve. The legend is placed in a blank space. You can move the legend to the desired position. Except for the legend function, other functions are also applicable to 3D graphics. zlabel function is used to describe the zcoordinate axis in 3D.

In addition to standard ASCII characters, the explanatory text in the above functions can also be used to control characters in latex (a popular mathematical typographical software) format, in this way, you can add Greek characters, mathematical symbols, formulas, and other content to the graph. In Latex strings supported by MATLAB, the/BF,/it, And/RM control characters are used to define the italic, italic, and positive body characters, respectively, the string control section of latex should be enclosed in braces. For example, text (0.3, 0.5, 'the usful {/bf matlab} ') will be displayed in the simhei of Matlab. Some commonly used latex characters are shown in the table. Each character can be used separately or with other characters and commands. For example, text (0.3, 0.5, 'sin ({/Omega} t + {/beta })')

The annotation is displayed.

Identifier

Symbol

Identifier

Symbol

Identifier

Symbol

/Alpha

 

/Epsilon

 

/Infty

 

/Beta

 

/ETA

 

/Int

 

/Gamma

 

/Gamma

 

/Partial

 

/Delta

 

/Delta

 

/Leftarrow

 

/Theta

 

/Theta

 

/Rightarrow

 

/Lambda

 

/Lambda

 

/Downarrow

 

/XI

 

/XI

 

/Uparrow

 

/PI

 

/PI

 

/Div

 

/Omega

 

/Omega

 

/Times

 

/SIGMA

 

/SIGMA

 

/PM

 

/PHI

 

/PHI

 

/Leq

 

/PSI

 

/PSI

 

/Geq

 

/Rock

 

/Tau

 

/NEQ

 

/Mu

 

/Heze

 

/Forall

 

/Nu

 

/CHI

 

/Exists

 

2. Coordinate Control

When drawing a chart, MATLAB can automatically select an appropriate coordinate scale based on the range of the curve data to be drawn, so that the curve can be displayed as clearly as possible. Therefore, you do not need to select the scale range of the coordinate axis. However, if you are not satisfied with the coordinates, you can use the axis function to reset them. The call format is

Axis ([xmin xmax ymin Ymax zmin zmax])

If only the first four parameters are given, select the coordinate system range based on the minimum and maximum values of the X and Y axes to draw a proper two-dimensional curve. If all parameters are given, a 3D image is drawn.

The axis function has rich functions and is commonly used in the following ways:

Axis equal

Axis square: generate a square coordinate system (rectangle by default)

Axis Auto: use the default settings

Axis off: cancel the Axis

Axis on: displays the axis.

Also, you can use the grid command to control the addition of gridlines to coordinates. the grid on/off command controls whether to draw or not, and the grid command without parameters switches between the two methods.

Add Borders to coordinates and use the box command to control them. Same usage as grid

For example, draw a segmentation function and add a graphic annotation. (Omitted)

3. Image persistence

Generally, every time a drawing command is executed, the current drawing window is refreshed, and the original drawing in the drawing window no longer exists. If you want to add a new drawing on an existing drawing, you can use the image persistence command hold. The hold on/off Command maintains the original image or refreshes the original image. The hold command without parameters switches between the two.

Example: (omitted)

4. Image window Segmentation

In practical applications, you often need to draw several independent images in a graphic window, which requires splitting the graphic window. The split graph window consists of several drawing areas. Each drawing area can create an independent coordinate system and draw a drawing. Different graphs in the same graph window are called subgraphs. MATLAB provides the subplot function to divide the current window into several drawing areas. Each area represents an independent subgraph and is also an independent coordinate system. You can use the subplot function to activate a certain area, this area is the active area, and all the drawing commands are applied to this active area. Call format:

Subplot (m, n, p)

This function divides the current window into m x n drawing areas, m rows, n drawing areas per line, and area numbers are prioritized by rows. Where area P is the current activity area. Each drawing area allows separate drawing in different coordinate systems.

Example: (omitted)

3. Other functions for drawing 2D images

1. Other linear Cartesian coordinates

In linear Cartesian coordinates, other types of graphs include bar charts, step charts, bar charts, and fill charts. The functions used are:

Bar (X, Y, option) options are in single quotes

Stairs (X, Y, option)

Stem (X, Y, option)

Fill (x1, Y1, option 1, X2, Y2, option 2 ,...)

The usage of the first three functions is similar to that of the plot, but there is no form of multiple input variables. The Fill function uses straight lines to connect the data points defined by the elements of x and y in sequence by the ascending order of vector element subscripts.

Example 5-8: Draw curves by bar chart, fill chart, step chart, and bar chart respectively

X = 0. 35: 7;

Y = 2 * exp (-0.5 * X );

Subplot (2, 2, 1); Bar (X, Y, 'G ');

Title ('bar (X, Y, ''g'') '); axis ([0, 7, 0, 2]);

Subplot (2, 2); fill (X, Y, 'R ');

Title ('fill (X, Y, ''r'') '); axis ([0, 7, 0, 2]);

Subplot (2, 2, 3); stairs (X, Y, 'B ');

Title ('stairs (X, Y, ''B'') '); axis ([0, 7, 0, 2]);

Subplot (2, 2, 4); stem (X, Y, 'k ');

Title ('stem (X, Y, ''k'') '); axis ([0, 7, 0, 2]);

 

2. Polar chart

The polar function is used to draw a polar chart. The call format is:

Polar (Theta, rock, option)

Among them, Theta is the polar coordinate polar angle, and rock is the polar path. The content of the option is similar to that of the plot function.

Example 5-9: Polar chart

 

Theta = 0: 0. 0* PI;

ROV = sin (3 * theta). * Cos (5 * theta );

Polar (Theta, rock, 'R ');

3. logarithm coordinate chart

In practical applications, logarithm coordinates are often used. MATLAB provides a function for drawing the logarithm and semi-logarithm coordinate curves. The call format is as follows:

Semilogx (x1, Y1, option 1, X2, Y2, option 2 ,...)

Semilogy (x1, Y1, option 1, X2, Y2, option 2 ,...)

Loglog (x1, Y1, option 1, X2, Y2, option 2 ,...)

The options defined in these functions are exactly the same as those defined in the plot function. The difference is the selection of coordinate axes. The semilogx function uses semi-logarithm coordinates. the X axis is a common logarithm scale, while the Y axis remains linear. Semilogy is the opposite of semilogx. The loglog function uses the full logarithm coordinates, and the X and Y axes use the logarithm scales.

Example: omitted

4. Plotting functions for adaptive function sampling

5. other two-dimensional images

Ii. 3D plotting

1. Basic functions for drawing 3D Curves

The most basic 3D graphic function is plot3, which extends the related functions of the two-dimensional drawing function plot to three-dimensional space and can be used to draw three-dimensional curves. The call format is:

Plot3 (x1, Y1, Z1, option 1, X2, Y2, Z2, option 2 ,...)

Each group of X, Y, and Z forms the coordinate parameters of a set of curves. The options are defined in the same way as those of plot. When x, y, and z are the same-dimensional vectors, the elements of X, Y, and Z form a three-dimensional curve. When x, y, and z are in the same dimension matrix, a three-dimensional curve is drawn based on the elements of X, Y, and Z. The number of curves equals to the number of columns in the matrix.

Example 513 draw a spatial curve

The parameter equation corresponding to this curve is

T = 0: PI/50: 2 * PI;

X = 8 * Cos (t );

Y = 4 * SQRT (2) * sin (t );

Z =-4 * SQRT (2) * sin (t );

Plot3 (X, Y, Z, 'P ');

Title ('line in 3-D space ');

Text (0, 0, 0, 'origin ');

Xlabel ('x'); ylabel ('y'); zlabel ('Z'); grid;

 

Ii. 3D Surfaces

1. Generate a coordinate matrix of a plane mesh

When drawing a 3D surface map represented by Z = f (x, y), first select a rectangular area on the XY plane, assuming that the rectangular area is d = [, b] × [c, d], then [a, B] is divided into M portions in the X direction, and [c, d] is divided into N portions in the Y direction, divide Area D into m × n small rectangles by dividing points into straight lines of the parallel axis. Generate a plane grid coordinate matrix that represents the coordinates of each vertex of a small rectangle, and use the relevant function plot.

There are two ways to generate a grid coordinate matrix in a plane area:

Generate with matrix operations.

X = A: DX: B;

Y = (C: Dy: d )';

X = ones (SIZE (y) * X;

Y = y * ones (SIZE (x ));

After the preceding statement is executed, each row of matrix X is vector X, the number of rows is equal to the number of elements of vector y, and each column of matrix Y is vector y, the number of columns equals the number of elements in vector X.

Generate using the meshgrid function;

X = A: DX: B;

Y = C: Dy: D;

[X, y] = meshgrid (x, y );

After the statement is executed, the obtained grid coordinate matrix is the same as the preceding method. when X is Y, it can be written as meshgrid (X)

2. Functions for drawing 3D Surfaces

MATLAB provides the mesh function and surf function to draw a 3D surface map. The mesh function is used to draw a three-dimensional graph, while the surf function is used to draw a three-dimensional curved surface graph, and fill the surface of each line with color. The call format is:

Mesh (X, Y, Z, c)

Surf (X, Y, Z, c)

In general, x, y, and z are matrices with the same dimension, X and Y are the grid coordinate matrices, and Z is the height matrices on the grid points, C is used to specify the color range at different heights. When C is omitted, Matlab considers c = z, that is, the color setting is proportional to the image height. In this way, you can obtain a hierarchical 3D image. When X and Y are omitted, the column subscript of the Z matrix is treated as the coordinate of the X axis, the row subscript of the Z matrix is treated as the coordinate of the Y axis, and a three-dimensional image is drawn. When X and Y are vectors, the length of X must be equal to the column of the Z matrix, and the length of Y must be equal to the row of Z, the combination of Y vector elements forms the X and Y coordinates of grid points. The zcoordinates are taken from the Z matrix and then draw three-dimensional curves.

Example 515 use a 3D curved surface map to present functions:

To facilitate the analysis of various features of a three-dimensional surface, three different forms of surface are shown below.

% Program 1

X =. * PI;

[X, y] = meshgrid (X );

Z = sin (Y). * Cos (X );

Mesh (x, y, z );

Xlabel ('x-axis '), ylabel ('Y-axis'), zlabel ('z-axis ');

Title ('mesh '); pause;

% Program 2

X =. * PI;

[X, y] = meshgrid (X );

Z = sin (Y). * Cos (X );

Surf (x, y, z );

Xlabel ('x-axis '), ylabel ('Y-axis'), zlabel ('z-axis ');

Title ('surf '); pause;

% Program 3

X =. * PI;

[X, y] = meshgrid (X );

Z = sin (Y). * Cos (X );

Plot3 (x, y, z );

Xlabel ('x-axis '), ylabel ('Y-axis'), zlabel ('z-axis ');

Title ('plot3-1'); grid;

 

 

 

Shows the program execution results. It can be found that the lines in the mesh have colors, and there is no color between the lines. The line of a curved surface (SURF) is black, and the surface is colored between the lines. Further observation, the surface fill color and the line color of the grid chart change along the Z axis. A 3D Surface drawn using plot3 is actually composed of a 3D curve. The characteristics of the surface drawn by plot (x', y', Z') can be analyzed.

For example, 516 plot the intersection of two circular tubes with the same diameter.

M = 30;

Z = 1.2 * (0: M)/m;

R = ones (SIZE (z ));

Theta = (0: M)/m * 2 * PI;

X1 = R' * Cos (theta); Y1 = R' * sin (theta); % generate the coordinate matrix of the first Circular Tube

Z1 = Z' * ones (1, m + 1 );

X = (-M: 2: M)/m;

X2 = x' * ones (1, m + 1); y2 = R' * Cos (theta); % generate the coordinate matrix of the first Circular Tube

Z2 = R' * sin (theta );

Surf (x1, Y1, Z1); % draw an upright Circular Tube

Axis equal, axis off

Hold on

Surf (X2, Y2, Z2); % draw a flat circular tube

Axis equal, axis off

Title ('intersection of two equal-diameter cylines ');

Hold off

 

In Example 517, the intersection of the Surface Shape and plane Z = A composed of functions is analyzed.

In addition, there are two functions similar to the mesh function, I .e., the three-dimensional mesh surface function meshc with contour lines and the three-dimensional mesh surface function meshz with base, which are similar to the mesh function. The difference is that meshc also draws the contour lines of the surface in the Z axis direction on the XY plane, and meshz also draws the base of the surface on the XY plane.

The surf function has two similar functions: surfc with contour lines and surfl with illumination effects.

For example, 518 select [-8, 8] × [-8, 8] In the XY plane to draw a function,

[X, y] = meshgrid );

Z = sin (SQRT (X. ^ 2 + Y. ^ 2)./SQRT (X. ^ 2 + Y. ^ 2 + EPS );

Subplot (2, 2, 1 );

Meshc (x, y, z );

Title ('meshc ');

Subplot (2, 2 );

Meshz (x, y, z );

Title ('meshz ');

Subplot (2, 2, 3 );

Surfc (x, y, z );

Title ('surfc ');

Subplot (2, 2, 4 );

Surfl (x, y, z );

Title ('surfl ');

3. Standard 3D Surface

MATLAB provides some functions for drawing standard 3D surfaces. These functions can generate corresponding drawing data and are often used for 3D graphics demonstration. For example, the Sphere function and cylinder function are used to draw a three-dimensional sphere and a cylinder. The Calling format of the sphere function is:

[X, y, z] = sphere (N );

This function will generate (n + 1) x (n + 1 matrix X, Y, Z. Using these three matrices, we can draw a sphere with a center located at the origin and a radius of 1. If the function is called without output parameters, the required sphere is drawn directly. N determines the smoothness of the sphere. The default value is 20. If the N value is small, the surface map of the polygon is drawn.

The call format of the cylinder function is:

[X, y, z] = cylinder (R, n)

Here, R is a vector that stores the radius of the cylinder height at various intervals. N indicates that there are n intervals on the circumference of the cylinder. By default, there are 20 intervals. For example, cylinder (3) generates a cylindrical, cylinder ([10, 1]) generates a cone. T = 0: PI/100:4 * PI; r = sin (t); cylinder (R, 30); generates a sine cylindrical surface.

In addition, Matlab also provides a peaks function, called a multi-peak function, which is often used for 3D Surface demonstration. This function can be used to generate a drawing data matrix. The matrix elements are defined by the function:

 

In the rectangle area, the function value on the equi grid point of [-3 3] × [-3 3] is determined. For example, Z = peaks (30)

A 30 × 30 matrix is generated,

Example 519 draw a standard 3D curved surface image

T = 0: PI/20:2 * PI;

[X, y, z] = cylinder (2 + sin (t), 30 );

Subplot (1, 3 );

Surf (x, y, z );

Subplot (1, 3, 2 );

[X, y, z] = sphere;

Surf (x, y, z );

Subplot (1, 3 );

[X, y, z] = peaks (30 );

Meshz (x, y, z );

 

3. Other 3D images.

When introducing two-dimensional graphics, we once mentioned special graphics, such as bar charts, bar charts, pie charts, and fill charts. They can also appear in three-dimensional form. Their functions are bar3, stem3, pie3 and fill3.

Bar3 draws a three-dimensional bar chart in the following format:

Bar3 (y );

Bar3 (x, y)

In the first format, each element of Y corresponds to a bar. The second format draws a bar chart of elements in Y at the position specified by X.

The stem3 function is used to draw a three-dimensional bar chart of discrete sequence data. The common format is:

Stem3 (z)

Stem3 (x, y, z)

In the first format, the data sequence Z is represented as a bar chart extending upwards from the XY plane. X and Y are automatically generated. In the second format, draw the bar chart of the Data Sequence Z at the position specified by X and Y. the dimension of X, Y, and Z must be the same.

The pie3 function is used to draw a three-dimensional pie chart. The common format is:

Pie3 (X)

X is a vector and uses the data in X to draw a three-dimensional pie chart.

The fill3 function can draw filled polygon in a 3D space. The common format is:

Fill3 (X, Y, Z, c)

X, Y, and Z are used as the vertices of the polygon, while C specifies the fill color.

In Example 520, draw a 3D image.

1. Draw a three-dimensional bar chart of cube arrays. 2. Draw a curve with a three-dimensional bar chart. Y = 2 SiNx 3. known results: x = [2], 1827, 2043,3025]. 3D Pie Chart 4. Draw 5 yellow triangles with random vertex coordinate values

Subplot (2, 2, 1 );

Bar3 (MAGIC (4 ));

Subplot (2, 2 );

Y = 2 * sin (0: PI/10:2 * PI );

Stem3 (y );

Subplot (2, 2, 3 );

Pie3 ([22.16,1827, 2043,3025]);

Subplot (2, 2, 4 );

Fill3 (RAND (3,5), Rand (3,5), Rand (3,5), 'y ');

In addition to the 3D images discussed above, common images also include the contour map of the waterfall chart and three-dimensional surface. The waterfall function is used to draw a waterfall chart. Its usage is similar to that of the meshz function, but its gridlines appear on the X axis and have the waterfall effect. A contour map is drawn in two or three dimensions by using the contour and limit 3 functions, respectively.

In Example 521, the waterfall chart and contour map of the Multi-peak function are drawn.

 

Subplot (1, 2 );

[X, y, z] = peaks (30 );

Waterfall (x, y, z );

Xlabel ('xx'); ylabel ('yy '); zlabel ('zz ');

Subplot (1, 2 );

Partition 3 (x, y, z, 12, 'k'); % 12 indicates the number of levels of height

Xlabel ('xx'); ylabel ('yy '); zlabel ('zz ');

3. Fine Processing of 3D images

I. Point of View Processing

In daily life, we observe objects from different angles and see different shapes. Similarly, 3D images drawn from different points of view have different shapes. The viewpoint position can be expressed by the azimuth and elevation.

Azimuth

MATLAB provides a function view for setting the viewpoint. The Calling format is:

View (AZ, El)

AZ is the azimuth angle, El is the elevation angle, and they are all measured in degrees. By default, the system defines the point of view as-37.5 degrees, and the angle of elevation is 30 degrees.

Example 522 draw a multi-peak functional surface from different viewpoints.

 

Subplot (2, 2, 1); mesh (peaks );

View (-37.5, 30 );

Title ('1 ');

Subplot (2, 2); mesh (peaks );

View (0, 90 );

Title ('2 ');

Subplot (2, 2, 3); mesh (peaks );

View (90,0 );

Title ('3 ');

Subplot (2, 2, 4); mesh (peaks );

View (-7,-10 );

Title ('4 ');

Ii. Color Processing

3. Image Cropping

The Nan constant defined by MATLAB can be used to represent the unusable data. With these features, the corresponding function values of the cropped part of the graph can be set to Nan, so that when drawing the graph, if the function value is Nan, the image is cropped. For example, you can use the following program to remove a part greater than 0.5 at the top or bottom of a sine wave.

X = 0: PI/10:4 * PI;

Y = sin (X );

I = find (ABS (y)> 0.5 );

X (I) = Nan;

Plot (x, y );

In Example 524, draw two globes, one of which is inside the other, and cut the outer ball to see the ball inside.

[X, y, z] = sphere (25 );

% Generate the outer dashboard

Z1 = z;

Z1 (:,) = Nan; % cut the ball to a part

C1 = ones (SIZE (Z1 ));

Surf (3 * X, 3 * y, 3 * Z1, C1); % generate the ball inside

Hold on

Z2 = z;

C2 = 2 * ones (SIZE (Z2 ));

C2 (:,) = 3 * ones (SIZE (C2 )));

Surf (1.5 * X, 1.5 * y, 1.5 * Z2, C2 );

Colormap ([0 1 0; 0.5 0 0; 1 0 0]);

Grid on

Hold off

 

Three colors are used in the color chart. The ball outside is green, and the ball inside uses two different colors of red.

Iv. Implicit Function plot

If an explicit expression of a function is given, you can first set the independent variable vector, then calculate the function Vector Based on the expression, and then draw a graph using functions such as plot. However, when functions are in the form of implicit functions, such as:, it is difficult to use the above method to draw graphics. MATLAB provides an ezplot function to plot the hidden function image. The usage is as follows:

① For function f = f (x), the call format of ezplot is:

Ezplot (F): draws a graph in the default range (-2 pi, 2 pi.

Ezplot (F, [a, B]), drawn in the range (A, B)

② For Implicit Functions f = f (x, y), the call format of ezplot is;

Ezplot (F): draws an image with f (x, y) = 0 in the default range (-2PI, 2PI) and (-2PI, 2PI.

Ezplot (F, [xmin, xmax, ymin, Ymax]); draw a graph in the interval.

Ezplot (F, [a, B]), drawn in the range (A, B), (a, B)

③ For the parameter equations x = x (t), Y = Y (t), the call format of the ezplot function is:

Ezplot (x, y), draw x = x (t), Y = Y (t) in the default range.

Ezplot (X, Y, [tmin, Tmax]), draw x = x (t), Y = Y (t) in the interval (tmin, Tmax.

Example 525 implicit function plot.

 

Subplot (2, 2, 1 );

Ezplot ('x ^ 2 + y ^ 2-9 '); axis equal;

Subplot (2, 2 );

Ezplot ('x ^ 3 + y ^ 3-5 * x * Y + 100 ')

Subplot (2, 2, 3 );

Ezplot ('cos (TAN (pI * X) ', [0, 1]);

Subplot (2, 2, 4 );

Ezplot ('8 * Cos (t) ', '4 * SQRT (2) * sin (t)', [0, 2 * Pi]);

Other implicit function plotting methods include ezpolar, ezcontour, ezplot3, ezmesh, ezmeshc, ezsurf, and ezsurfc.

Http://hi.baidu.com/wqccwang/blog/item/ad896634718d441d91ef392e.html

Http://blog.csdn.net/wangcj625/article/details/6287735

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.