Source: Internet
Author: User

Label:

One. Two-dimensional data curve

1.1Draw a single two-dimensional curve

The basic of the plot functionThe calling format is:

Plot (x, y)

where x and y are vectors of the same length, respectively, for storing Xcoordinate and y-coordinate data.

Example 1-1 in the 0≤x≤2p interval, draw the curve

Y=2e-0.5xcos (4πx)

The procedure is as follows:

X=0:pi/100:2*pi;

Y=2*exp ( -0.5*x). *cos (4*pi*x);

Plot (x, y)

Example 1-2 draws a curve.

The procedure is as follows:

T=0:0.1:2*pi;

X=t.*sin (3*t);

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

Plot (x, y);

The simplest invocation format for the plot function is to include only one inputParameters:

Plot (x)

In this case, when x is a real vector, with the subscript of the vector element as the horizontal axis, the element value is the ordinate to draw a continuous curve, which is actually to draw a line chart.

1.2 Drawing multiple two-dimensional curves

1. The input parameters of the plot function areMatrix form

(1) When x is a vector, y is a matrix with one dimension and x in the same dimension, then it draws many differentThe curve of the color. The number of curve bars equals the other dimension of the y matrix, and X is used as the horizontal axis of these curves.

(2) When x, Y is the same dimensional matrix, the x, y corresponding column elements are plotted as horizontal and vertical, and the number of curve bars equals the number of columns of the matrix.

(3) For the plot function that contains only one input parameter, when the input parameter is a real matrix, the curve of each column element value relative to its subscript is plotted by column, and the number of curve bars equals the number of columns of the input parameter matrix.

When the input parameter is a complex matrix, multiple curves are plotted by column in both the real and imaginary parts of the element as horizontal and vertical.

2. Plot function with multiple input parameters

The calling format is:

Plot (X1,y1,x2,y2,..., Xn,yn)

(1) When the input parameters are vectors, X1 and y1,x2 and y2,...,xn and yn form a set of vector pairs, each set of vector pairs can be of different lengths. Each vector pair can draw a curve so that more than one curve can be drawn within the same coordinate.

(2) When the input parameter has a matrix form, paired x, y by the corresponding column elements for the horizontal and vertical axis, respectively, the number of curves is equal to the number of columns of the matrix.

Example 1-3 analyzes the curves drawn by the following program.

X1=linspace (0,2*pi,100);

X2=linspace (0,3*pi,100);

X3=linspace (0,4*pi,100);

Y1=sin (x1);

Y2=1+sin (x2);

Y3=2+sin (x3);

X=[X1;X2;X3] ';

Y=[y1;y2;y3] ';

Plot (x,y,x1,y1-1)

3. A graphic with two ordinate scales

In MATLAB, if you need to draw two graphs with different ordinate scales, you can use the Plotyy drawing function. The calling format is:

Plotyy (X1,y1,x2,y2)

Where x1,y1 corresponds to a curve, x2,y2 corresponds to another curve. Horizontal axis of the same scale, the ordinate has two, left ordinate for x1,y1 data pairs, right ordinate for x2,y2 data pair.

Example 1-4 draws curves Y1=0.2e-0.5xcos (4πx) and Y2=2e-0.5xcos (πx) in the same coordinates with different scales.

The procedure is as follows:

X=0:pi/100:2*pi;

Y1=0.2*exp ( -0.5*x). *cos (4*pi*x);

Y2=2*exp ( -0.5*x). *cos (pi*x);

Plotyy (X,Y1,X,Y2);

4. Graphics hold

On/OffCommandControls whether to keep the original shape or refresh the original shape, and the hold command without parameters toggles between the two states.

Example 1-5 uses graphic hold to draw curves Y1=0.2e-0.5xcos (4πx) and Y2=2e-0.5xcos (πx) within the same coordinates.

The procedure is as follows:

X=0:pi/100:2*pi;

Y1=0.2*exp ( -0.5*x). *cos (4*pi*x);

Plot (X,Y1)

On

Y2=2*exp ( -0.5*x). *cos (pi*x);

Plot (x,y2);

Hold off

1.3 Setting the Curve style

MATLAB provides a number of drawing options to determine the Linetype, color, and data point marker symbols for the plotted curve, which can be combined. For example, "B-." Represents a blue dash, "y:d" denotes a yellow dashed line and marks a data point with a diamond character. When the item is omitted, MATLAB stipulates that the line will be solid, and the color shall be in accordance with the order of the curve.

To set the curve style, you can add a plot option to the plot function, which is called in the format:

Plot (x1,y1, option 1,x2,y2, option 2,..., xn,yn, option N)

Example 1-6: In the same coordinate, the curves Y1=0.2e-0.5xcos (4πx) and Y2=2e-0.5xcos (πx) are plotted with different linetype and colors, and the intersection points of the two curves are marked.

The procedure is as follows:

X=linspace (0,2*pi,1000);

Y1=0.2*exp ( -0.5*x). *cos (4*pi*x);

Y2=2*exp ( -0.5*x). *cos (pi*x);

K=find (ABS (Y1-Y2) <1e-2); % find subscript for Y1 and Y2 equal points (approximately equal)

X1=x (k); % y1 x coordinate with y2 equal point

Y3=0.2*exp ( -0.5*x1). *cos (4*PI*X1); % to find the y-coordinate of the Y1 and Y2 values equal points

Plot (X,y1,x,y2, ' K: ', X1,y3, ' BP ');

1.4 Graphical dimensioning and coordinate control

1. Graphic callouts

The calling format for the graphical callout function is:

Title (graphic name)

Xlabel (X-axis description)

Ylabel (Y-axis description)

Text (x, y, graphical description)

Legend (Fig. 1, fig. 2,...)

The description text in the function, in addition to the use of standard ASCII characters, can also use Latex format control characters, so you can add Greek letters, mathematical symbols and formulas and other content. For example, text (0.3,0.5, ' sin ({/omega}t+{/beta}) ') will get the label Effect sin (ωt+β).

Example 1-7: In the 0≤x≤2p interval, the curves y1=2e-0.5x and Y2=cos (4πx) are plotted, and the graphics are added to the graphic callout.

The procedure is as follows:

X=0:pi/100:2*pi;

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

Y2=cos (4*PI*X);

Plot (X,y1,x,y2)

Title (' x from 0 to 2{/pi} '); % plus graphic title

Xlabel (' Variable X '); % plus x Axis description

Ylabel (' Variable Y '); % plus y-axis description

Text (0.8,1.5, ' curve y1=2e^{-0.5x} '); % Add a graphical description at the specified location

Text (2.5,1.1, ' curve y2=cos (4{/pi}x) ');

Legend (' y1 ', ' y2 ')% plus legend

2. Coordinate control

AxiThe invocation format of the S function is:

Axis ([xmin xmax ymin ymax zmin Zmax])

Axis functions are rich in functionality and are commonly used in the following formats:

Axis equal: vertical and horizontal axes with equal length scale.

Axis Square: Produces a square coordinate system (the default is a rectangle).

Axis Auto: Use the default settings.

Axis off: cancels the axis.

Axis on:Displays the axis.

Grid commands are used to control coordinates and grids. The grid on/off command controls whether a grid line is drawn or not drawn, and the grid command without parameters toggles between the two states.

Use the box command to control the coordinates and borders. box on/OFF command controls whether to add or no border lines, and the box command without parameters toggles between the two states.

Example 1-8 in the same coordinate, you can draw 3 concentric circles, and coordinate control.

The procedure is as follows:

T=0:0.01:2*pi;

X=exp (i*t);

Y=[x;2*x;3*x] ';

Plot (y)

Grid on; % plus grid lines

box on; % Plus coordinate border

Axis equal% axis with equal scale

1.5 Visual editing of graphs

MATLAB version 6.5 provides a visual graphical editing tool in the graphics window, which allows you to edit the various graphical objects in the window using the commands on the menu bar or toolbar of the graphics window.

There is a menu bar and toolbar on the graphics window. The menu bar contains a total of 7 menu items for file, Edit, View, Insert, Tools, window, and help, and the toolbar contains 11 command buttons.

1.6 Mapping functions for adaptive sampling of functions

The call format for the Fplot function is:

Fplot (fname,lims,tol, option)

Where fname is the function name, appears as a string, LIMs is the range of x, Y, tol is the relative allowable error, itsThe system default value is 2e-3. The option definition is the same as the plot function.

Example 1-9 draws the curve of f (x) =cos (Tan (πx)) with the Fplot function.

The command is as follows:

Fplot (' cos (tan (pi*x)) ', [0,1],1e-4)

1.7 Segmentation of the graphics window

The call format for the subplot function is:

Subplot (M,N,P)

This function divides the current graphics window into MXN plots, that is, each row is n, a total of M rows, the area code is prioritized by row, and the P area is selected as the current active area. Each plot area allows the drawing to be drawn separately in a different coordinate system.

Example 5-10 in the graphics window, the Dogan curve is plotted as a sub-graph.

Two. Other two-dimensional graphics

2.1 Two-dimensional data graphs in other coordinate systems

1. Logarithmic coordinate graphics

MATLAB provides functions for plotting logarithmic and semi-logarithmic coordinate curves in the form of:

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,...)

2. Polar Chart

The polar function is used to draw polar plots, which are called in the following format:

Polar (Theta,rho, options)

Where Theta is the polar polar angle, rho is the vector diameter of polar coordinates, and the content of the option is similar to the plot function.

Example 1-12 plots the polar plot of the R=sin (t) cos (t) and marks the data points.

The procedure is as follows:

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

R=sin (t). *cos (t);

Polar (t,r, '-* ');

2.2 Two-dimensional statistical analysis diagram

In Matlab, the two-dimensional statistical analysis of a lot of graphics, the common bar chart, Ladder diagram, rod and fill charts, etc., the functions used are:

Bar (x, y, Options)

Stairs (x, Y, Options)

Stem (x, y, Options)

Fill (x1,y1, option 1,x2,y2, option 2,...)

Example 1-13 plots the curve y=2sin (x) in the form of bar, ladder, bar, and fill charts, respectively.

The procedure is as follows:

X=0:pi/10:2*pi;

Y=2*sin (x);

Subplot (2,2,1); bar (x, y, ' G ');

Title (' Bar (x, y, ' g ') '); axis ([0,7,-2,2]);

Subplot (2,2,2); Stairs (x, y, ' B ');

Title (' Stairs (x, y, ' B ') '), axis ([0,7,-2,2]);

Subplot (2,2,3); Stem (x, y, ' k ');

Title (' Stem (x, y, ' k ') '); axis ([0,7,-2,2]);

Subplot (2,2,4); Fill (x, y, ' y ');

Title (' Fill (x, y, ' y ') '); axis ([0,7,-2,2]);

MATLAB provides a number of statistical analysis plotting functions, for example, a pie chart that represents the percentage of each element as a sum, a complex number of phasor graphs, and so on.

Example 1-14 drawing:

(1) The annual output value of an enterprise (unit: million) is: 2347,1827,2043,3025, trial pie chart for statistical analysis.

(2) Plot of complex numbers: 7+2.9i, 2-3i and -1.5-6i.

The procedure is as follows:

Subplot (1,2,1);

Pie ([2347,1827,2043,3025]);

Title (' Pie chart ');

Legend (' One quarter ', ' two quarter ', ' three quarter ', ' four Quarter ');

Subplot (1,2,2);

Compass ([7+2.9i,2-3i,-1.5-6i]);

Title (' Phasor chart ');

Three. Implicit function drawing

MATLAB provides a ezplot function to draw an implicit function graph, which is described below for its usage.

(1) for function f = f (x), the call format of the Ezplot function is:

Ezplot (f): Draws a graph of F = f (x) in the default interval -2π<x<2π.

Ezplot (f, [A, b]): Draws a graph of F = f (x) in the interval a<x<b.

(2) for the implicit function f = f (x, y), the Ezplot function is called in the following format:

Ezplot (f): Plots f (x, Y) = 0 in the default interval -2π<x<2π and -2π<y<2π.

Ezplot (f, [Xmin,xmax,ymin,ymax]): Plots f (x, Y) = 0 in the interval Xmin<x<xmax and Ymin<y<ymax.

Ezplot (f, [b]): Plots f (x, Y) = 0 in the interval a<x<b and a<y< B.

(3) for parametersEquation x = x (t) and y = y (t), the call format of the Ezplot function is:

Ezplot (x, y): Draws a graphic of x=x (T) and y=y (t) in the default interval 0<t<2π.

Ezplot (x, Y, [Tmin,tmax]): Plots x=x (t) and y=y (t) in the interval tmin < T < Tmax.

Example 1-15 implicit function drawingExamples of applications.

The procedure is as follows:

Subplot (2,2,1);

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

Subplot (2,2,2);

Ezplot (' X^3+Y^3-5*X*Y+1/5 ')

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])

Four. Three-dimensional graphics

4.1 Three-dimensional curve

The PLOT3 function is very similar to the use of the plot function, with the following invocation format:

PLOT3 (x1,y1,z1, option 1,x2,y2,z2, option 2,..., xn,yn,zn, option N)

Each of these groups of X, Y and z form the coordinate parameters of a set of curves, with the same definition of options as the plot function. When x, Y, z is a vector of the same dimension, the x, y, z corresponding elements form a three-dimensional curve. When x, Y, Z is the same-dimensional matrix, three-dimensional curves are plotted in X, Y, z corresponding column elements, which equals the number of matrix columns.

Example 1-16 draws a three-dimensional curve.

The procedure is as follows:

T=0:pi/100:20*pi;

X=sin (t);

Y=cos (t);

Z=t.*sin (t). *cos (t);

PLOT3 (x, y, z);

Title (' line in Space ');

Xlabel (' X '); Ylabel (' Y '); Zlabel (' Z ');

Grid on;

4.2 Three-dimensional surface

1. Generate three-dimensional data

In MATLAB, the Meshgrid function is used to produce a grid coordinate matrix in a planar area. The format is:

X=a:d1:b; Y=c:d2:d;

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

After the statement executes, each row of the matrix x is a vector x, the number of rows equals the number of elements of the vector y, each column of the Matrix y is vector y, and the number of columns equals the number of elements of Vector x.

2. Functions for drawing three-dimensional surfaces

The surf function and the mesh function are called in the following format:

Mesh (X,Y,Z,C)

Surf (x,y,z,c)

In general, X, Y, Z is a matrix with the same number of dimensions. X, y is the grid coordinate matrix, z is the height matrix on the mesh point, and C is used to specify the color range at different heights.

Example 1-17 draws a three-dimensional surface plot z=sin (X+sin (y))-x/10.

The procedure is as follows:

[X,y]=meshgrid (0:0.25:4*PI);

Z=sin (X+sin (y))-X/10;

Mesh (x, y, z);

Axis ([0 4*pi 0 4*pi-2.5 1]);

In addition, there are three-dimensional mesh surface function MESHC with contour line and three-dimensional mesh surface function meshz with base. The usage is similar to mesh, and the difference is that the MESHC also draws the contours of the surface in the z-direction on the XY plane, and Meshz also draws the base of the surface on the XY plane.

Example 1-18: Select a region [ -8,8]x[-8,8] within the XY plane to draw 4 three-dimensional surface plots.

The procedure is as follows:

[X,y]=meshgrid ( -8:0.5:8);

Z=sin (sqrt (x.^2+y.^2))./SQRT (X.^2+y.^2+eps);

Subplot (2,2,1);

Mesh (x, y, z);

Title (' Mesh (x, y, z) ')

Subplot (2,2,2);

MESHC (x, y, z);

Title (' MESHC (x, y, z) ')

Subplot (2,2,3);

Meshz (x, Y, z)

Title (' Meshz (x, y, z) ')

Subplot (2,2,4);

Surf (x, y, z);

Title (' Surf (x, y, z) ')

3. Standard three-dimensional surface

The call format for the Sphere function is:

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

The call format for the cylinder function is:

[X,y,z]= Cylinder (R,N)

Matlab also has a peaks function, called a multi-peak function, commonly used for three-dimensional surface demonstrations.

<!--[if!supportemptyparas]--> <!--[endif]-->

Example 1-19 draws a standard three-dimensional surface shape.

The procedure is as follows:

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

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

Subplot (2,2,1);

Surf (x, y, z);

Subplot (2,2,2);

[X,y,z]=sphere;

Surf (x, y, z);

Subplot (2,1,2);

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

Surf (x, y, z);

<!--[if!supportemptyparas]--> <!--[endif]-->

4.3 Other three-dimensional graphics

When introducing two-dimensional graphics, it was mentioned that special graphs, such as bar, bar, pie and fill, can also appear in three-dimensional form, using the functions of Bar3, STEM3, Pie3 and Fill3 respectively.

The BAR3 function draws a three-dimensional bar chart in the usual format:

Bar3 (y)

Bar3 (x, y)

The STEM3 function draws a three-dimensional bar graph of discrete series data, commonly used in the following formats:

Stem3 (z)

Stem3 (x, Y, z)

The PIE3 function draws a three-dimensional pie chart in the usual format:

PIE3 (x)

The FILL3 function is equivalent to the three-dimensional function fill, which draws a filled polygon in three dimensions, usually in the following format:

Fill3 (X,Y,Z,C)

<!--[if!supportemptyparas]--> <!--[endif]-->

Example 1-20: Drawing a three-dimensional graphic

(1) Draw the three-dimensional bar chart of the magic square.

(2) Draw the curve Y=2sin (x) in the form of three-dimensional bar graph.

(3) Known x=[2347,1827,2043,3025], draw pie chart.

(4) Draw five yellow triangles with a random vertex coordinate value.

The procedure is as follows:

Subplot (2,2,1);

Bar3 (Magic (4))

Subplot (2,2,2);

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

Stem3 (y);

Subplot (2,2,3);

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

Subplot (2,2,4);

Fill3 (rand (3,5), Rand (3,5), Rand (3,5), ' Y ')

Example 1-21 plots the waterfall and contour plots of the multi-peak function.

The procedure is as follows:

Subplot (1,2,1);

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

Waterfall (x, y, z)

Xlabel (' x-axis '), Ylabel (' Y-axis '), Zlabel (' Z-axis ');

Subplot (1,2,2);

CONTOUR3 (x,y,z,12, ' K '); % of which 12 represents the height of the rank number

Xlabel (' x-axis '), Ylabel (' Y-axis '), Zlabel (' Z-axis ');

<!--[If!supportemptyparas]-->

Five. Graphics decoration Processing

5.1 Viewpoint Processing

MATLAB provides a function view that sets the viewpoint, which is called in the following format:

View (Az,el)

Where AZ is the azimuth, El is the elevation, and they are all measured in degrees. The system default viewpoint is defined as azimuth -37.5°, elevation 30 °.

Example 5-22 three-dimensional curves are observed from different viewpoints.

5.2 Color Processing

1. Vector Representation of color

In addition to using characters to represent colors, MATLAB can also represent colors with vectors containing 3 elements. Vector elements in the [0,1] range of values, 3 elements represent red, green, and blue 3 colors of the relative brightness, called the RGB ternary group.

<!--[if!supportemptyparas]--> <!--[endif]-->

2. Color map

Color map is the concept introduced by MATLAB system. In MATLAB, you can have only one color map per graphics window. A color graph is a numerical matrix of mx3, each of which is an RGB ternary group. The color map matrix can be artificially generated, or you can call the functions provided by MATLAB to define a color graph matrix.

3. Coloring of three-dimensional surface graphics

A three-dimensional surface view is actually a color on every mesh in the grid. The surf function shades the mesh with the default shading method. In addition, you can use the shading command to change the coloring mode.

The shading faceted command shades each mesh with its height-corresponding color, but the gridlines remain and the color is black. This is the default coloring method for the system.

The Shading flat command shades each mesh with the same color, and the grid lines are colored accordingly, making the surface of the graphic appear smoother.

Shading Interp command in the mesh with color interpolation processing, the resulting surface image appears the smoothest.

Example 1-23 3 ways of rendering the effect of the graphic coloring.

The procedure is as follows:

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

ColorMap (copper);

Subplot (1,3,1);

Surf (x, y, z);

Axis equal

Subplot (1,3,2);

Surf (x, y, z); shading flat;

Axis equal

Subplot (1,3,3);

Surf (x, y, z); shading interp;

Axis equal

5.3 Light Processing

MATLAB provides a function for lighting settings, which are called in the following format:

Light (' Color ', option 1, ' Style ', option 2, ' Position ', option 3)

<!--[if!supportemptyparas]--> <!--[endif]-->

Example 5-24 the spherical surface after light treatment.

The procedure is as follows:

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

Subplot (1,2,1);

Surf (x, y, z); axis equal;

Light (' posi ', [0,1,1]);

Shading Interp;

Hold on;

PLOT3 (0,1,1, ' P '); text (0,1,1, ' light ');

Subplot (1,2,2);

Surf (x, y, z); axis equal;

Light (' posi ', [1,0,1]);

Shading Interp;

Hold on;

PLOT3 (1,0,1, ' P '); text (1,0,1, ' light ');

5.4 Crop processing of graphics

Example 5-25 draws a three-dimensional surface plot, and the interpolation shading processing, the cut out of the graph X and y are less than 0 parts.

The procedure is as follows:

[X,y]=meshgrid ( -5:0.1:5);

Z=cos (x). *cos (y). *exp (-sqrt (x.^2+y.^2)/4);

Surf (x, y, z); shading interp;

Pause% Program paused

I=find (x<=0&y<=0);

Z1=Z;Z1 (i) =nan;

Surf (X,Y,Z1); shading Interp;

To show the cropping effect, the first surface is drawn and then paused, and then the cropped surface is displayed.

Six.Image processing and animation production

6.1 Image Processing

1. Imread and Imwrite functions

The Imread and Imwrite functions are used to separate the imageThe file reads into the MATLAB workspace and writes the image data and the color map data to a certain format of the image file. MATLAB supports a variety of image file formats, such as. bmp,. jpg,. jpeg,. tif, and so on.

2. Image and Imagesc functions

These two functions are used for image display. In order to ensure the appearance of the image, you should also use the COLORMAP function to set the image color map.

Example 1-26 has an image file Flower.jpg, which displays the image in the graphics window.

The procedure is as follows:

[X,cmap]=imread (' flower.jpg '); % data array and color graph array for reading images

Image (x); ColorMap (CMAP);

Axis image off% maintain aspect ratio and cancel axis

6.2 Animation Production

MATLAB provides GetFrame, Moviein, and movie functions for animation.

1. GetFrame function

The GetFrame function intercepts a picture message (called a frame in the animation), and a picture information forms a large column vector. Obviouslya large matrix is required to save the N-surface.

2. Moviein function

The Moviein (n) function is used to create an n-column matrix that is large enough. The matrix is used to store the data of the N-frames for playback. The reason to build a large matrix in advance is to improve the speed of program operation.

3. Movie function

The movie (m,n) function plays the screen defined by the matrix M n Times, which is played once by default.

Example 1-27 draws the peaks function surface and rotates it around the z axis.

The procedure is as follows

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

Surf (x, y, z)

Axis ([ -3,3,-3,3,-10,10])

Axis off;

Shading Interp;

ColorMap (Hot);

M=moviein (20); % build a 20-column large matrix

For i=1:20

View ( -37.5+24* (i-1), 30)% change viewpoint

M (:, i) =getframe; % save graph to M matrix

End

Movie (m,2); % Playback Screen 2 times

< Span class= "T_tag" >from: http://hi.baidu.com/ fightiger/blog/item/b2c1831382bf52d9f7039eab.html

http://blog.csdn.net/abcjennifer/article/details/7456839

Matlab Curve drawing