MATLAB tutorial-2D drawing [Z]

Source: Internet
Author: User

MATLAB is not only good at matrix-related numerical operations,It is also suitable for use in various scientific visual representations.
(Scientific Visualization ).This section describes the basic xy plane and XYZ space of Matlab.
Drawing commands, including drawing, printing, and archiving of one-dimensional curves and two-dimensional surfaces.
Plot is a basic function for drawing a one-dimensional curve, but before using this function,We need to first define the Qu
The X and Y coordinates of each point on the line. The following example shows a sine curve:
Close all; X = linspace (0, 2 * Pi, 100); % x coordinates of the 100 points
Y = sin (x); % corresponds to the Y coordinate
Plot (x, y );
==========================================================================
Small organization: basic plot functions of MATLAB
Plot: the X and Y axes are linear scales (linear scale)
Loglog: logarithmic scale)
Semilogx: the X axis is a logarithm scale, and the Y axis is a linear scale.
Semilogy: the X axis is a linear scale, and the Y axis is a logarithm scale.
==========================================================================
To draw multiple curves, you just need to add the coordinate pairs to the plot function in sequence:
Plot (x, sin (x), X, cos (x ));
To change the color, add the corresponding string to the coordinate pair:
Plot (x, sin (x), 'C', X, cos (x), 'G ');
To change the color and line style at the same time
Close the string:
Plot (x, sin (x), 'co', X, cos (x), 'G *');
==========================================================================
Small sorting: parameter values of the plot Function
Character Color character line type
Y yellow. Dot
K black o circle
W white X
B blue ++
G green **
R red-solid line
C bright blue: dot line
M manganese purple-. dotted line
-- Dotted line
==========================================================================
Axis ([xmin, xmax,Ymin, Ymax]) function to adjust the Axis
Perimeter:
Axis ([0, 6,-1.2, 1.2]);
In addition, MATLAB can also add various annotations and processing to the image:
Xlabel ('input value'); % x axis Annotation
Ylabel ('function value'); % Y axis Annotation
Title ('two Trigonometric Functions '); % graphic title
Legend ('y = sin (x) ', 'y = cos (x)'); % graphical Annotation
Grid on; % display grid lines
We can use subplot to draw several small images in the same window at the same time:
Subplot (2, 2, 1); plot (x, sin (x ));
Subplot (2, 2); plot (x, cos (x ));
Subplot (2, 2, 3); plot (x, sinh (x ));
Subplot (2, 2, 4); plot (x, cosh (x ));
MATLAB also has various other two-dimensional plotting functions to suit different applications,See the table below.
==========================================================================
Small organization: other two-dimensional drawing functions
Bar Chart
Errorbar image plus error range
Fplot more precise function graphics
Polar coordinate chart
Hist accumulative Graph
Rose polar coordinate accumulative chart
Stairs step chart
Stem needle chart
Fill solid graph
Feather chart
Compass chart
Quiver Vector Field
==========================================================================
The following is an example of each function.
When the number of data points is small, the bar chart is a suitable representation:
Close all; % close all graph windows
X =;
Y = rand (size (x ));
Bar (x, y );
If the amount of data error is known, it can be expressed by errorbar.In the following example, the unit standard deviation is used.
Data Error volume:
X = linspace (0, 2 * pi, 30 );
Y = sin (x );
E = std (y) * ones (size (x ));
Errorbar (x, y, e)
Fplot can be used for more precise plotting of rapidly changing functions,Changes will occur.
For example:
Fplot ('sin (1/x) ', [0.02 0.2]); % [0.02 0.2] is the drawing range
To generate polar coordinates, use polar:
Theta = linspace (0, 2 * pi );
R = cos (4 * theta );
Polar (theta, r );
For a large amount of data, we can use hist to display the data score and statistical characteristics. Below
Several commands can be used to verify the Gaussian MESS score generated by randn:
X = randn (5000, 1); % generates 5000? = 0 ,? Gaussian Number of = 1
Hist (x, 20); % 20 indicates the number of bars

Rose and hist are very similar, but they regard the data size as an angle,The number of data items is considered as a distance ,?
Why does my sister have a ticket?
X = randn (1000, 1 );
Rose (x );
Stairs can draw a step chart:
X = linspace (0, 10, 50 );
Y = sin (x). * exp (-x/3 );
Stairs (x, y );
Stems can generate a needle chart, which is often used to draw digital signals:
X = linspace (0, 10, 50 );
Y = sin (x). * exp (-x/3 );
Stem (x, y );

Stairs regards the data point as the vertex of a multilateral row and colors the multilateral row:
X = linspace (0, 10, 50 );
Y = sin (x). * exp (-x/3 );
Fill (x, y, 'B'); % 'B' is blue
Feather views each data point in the plural and draws it with an arrow:
Theta = linspace (0, 2 * pi, 20 );
Z = cos (theta) + I * sin (theta );
Feather (z );
Compass and feather are very close,Only the starting point of each arrow is a dot:
Theta = linspace (0, 2 * pi, 20 );
Z = cos (theta) + I * sin (theta );
Compass (z );
3. Basic XYZ stereo drawing command
In Scientific visualization
A very important technique.This chapter introduces the plot life of the basic XYZ three-degree Space in MATLAB.
.
Mesh and plot are basic commands for three-dimensional 3D plotting,Mesh can draw a three-dimensional mesh,
The plot can draw a three-dimensional surface map,The two generate different colors based on the height. Lower
The column command can draw a three-dimensional mesh map formed by the function:
X = linspace (-2, 2, 25); % 25 points on the x axis
Y = linspace (-2, 2, 25); % Take 25 points on the y axis
[Xx, yy] = meshgrid (x, y); % xx and yy are matrices of 21x21
Zz = xx. * exp (-xx. ^ 2-yy. ^ 2); % calculate the function value. zz is also the matrix of 21x21.
Mesh (xx, yy, zz); % draw a three-dimensional mesh

The usage of surf and mesh is similar to the following:
X = linspace (-2, 2, 25); % 25 points on the x axis
Y = linspace (-2, 2, 25); % Take 25 points on the y axis
[Xx, yy] = meshgrid (x, y); % xx and yy are matrices of 21x21
Zz = xx. * exp (-xx. ^ 2-yy. ^ 2); % calculate the function value. zz is also the matrix of 21x21.
Surf (xx, yy, zz); % plot a three-dimensional curved surface
To facilitate the test of stereoscopic plotting, MATLAB provides a peaks function,Can generate a concave and convex
The resulting surface contains three local vertices and three Local Minimization points. The equation is:
The fastest way to draw this function is to directly type peaks:
Peaks
Z = 3 * (_x). ^ 2. * exp (-(x. ^ 2)-(y + 1). ^ 2 )...
-10 * (x/5-x. ^ 3-y. ^ 5). * exp (-x. ^ 2-y. ^ 2 )...
-1/3 * exp (-(x + 1). ^ 2-y. ^ 2)
We can also take the point of the peaks function and plot it in different ways.Meshz Surface
Append apron:
[X, y, z] = peaks;
Meshz (x, y, z );
Axis ([-inf]);
Waterfall can generate water flow in the x or y direction:
[X, y, z] = peaks;
Waterfall (x, y, z );
Axis ([-inf]);
The following command produces the effect of water flow in the y direction:
[X, y, z] = peaks;
Waterfall (x', y', Z ');
Axis ([-inf]);
Meshc simultaneously draws the mesh and contour lines:
[X, y, z] = peaks;
Meshc (x, y, z );
Axis ([-INF]);
Surfc simultaneously draws a curved surface and a contour line:
[X, y, z] = peaks;
Surfc (x, y, z );
Axis ([-INF]);
3. Draw the contour lines of a surface in a three-degree space:
Latency 3 (peaks, 20 );
Axis ([-INF]);
Contour draws the projection of the Surface Contour lines on the XY plane:
Contour (peaks, 20 );
Plot3 can draw a curve in a three-degree space:
T = linspace (501 * Pi );
Plot3 (T. * sin (t), T. * Cos (t), t );
You can also draw two curves in a three-degree space at the same time:
T = linspace (0, 10 * Pi, 501 );
Plot3 (T. * sin (t), T. * Cos (t), T, T. * sin (t), T. * Cos (t),-t );

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.