MATLAB learning-graph drawing

Summary

In a scientific research system, showing mathematical formulas and data in graphs is a scientific and effective way to show the specific physical meaning of symbols and the internal connections and laws of a large amount of data.

MATLAB can draw two-dimensional, three-dimensional and four-dimensional data graphics, and by setting the line type, color, mark, observation angle, coordinate axis range and other attributes of the graphics, the internal connections and laws of a large amount of data can be expressed more delicately, perfect. MATLAB provides many devices to display vectors and matrices with graphs, including annotation and printing of these graphs.

MATLAB has a large number of simple, flexible, and easy-to-use two-dimensional and three-dimensional graphics commands, and users can add sound effects to the MATLAB program. This chapter will describe the data in MATLAB in detail.

1. Discrete data and discrete functions

A binary real scalar pair (X, Y) can be represented by a point on the plane, and a binary real scalar array [(X1,y1) (xz,y2)… (xn,yn)] can be represented by a point on the plane

A set of points to represent. For the discrete function Y=f(X), when X is a one-dimensional scalar array [x1,x>... ,x.], the one-dimensional scalar corresponding to Y can be obtained according to the function relationship

[y> 2…, at]。

When these two vector arrays are represented by a point sequence in a rectangular coordinate system, the visualization of the discrete function is realized. Of course, the discrete sequences on these graphs are

It reflects only the functional relationship on the finite point defined by X or within the finite interval. It should be noted that

MATLAB cannot realize the visualization of data on infinite intervals.

Visualization of discrete data and discrete functions.
(1) Create an M file and use the M file editor to write in the M file:

X1=[1 2 4 6 7 8 10 11 12 14 16 17 18 20];
Y1=[1 2 4 6 7 8 10 10 8 7 6 4 2 1];
figure(1)
plot(X1,Y1,'.','Markersize',15)
X2=1:20;
Y2=log(X2);
figure(2)
plot(X2,Y2,'*','Markersize',15)

(2) Run the M script file, and the graphical results are as follows:
Insert picture description hereInsert picture description here

Two, continuous function

It is impossible to draw a true continuous function in MATLAB. Therefore, when realizing the visualization of continuous function, the continuous function must first be used to calculate the function result on a set of discrete independent variables, and then the independent variable array and the result array are displayed in the graph Show it.

Of course, these discrete points still cannot express the continuity of the function. In order to show the law of the function and its continuous change more vividly, the following two methods are usually used:

(1) Divide the discrete interval more finely, and gradually approach the continuous change characteristics of the function, until the visual continuous effect is achieved.

(2) Connect every two discrete points with a straight line, and use the straight line between every two discrete points to approximate the function characteristics between the two points.

Visualization of continuous functions.

(1) Create an M file and use the M file editor to write in the M file:

X1 = (0:12) *pi/6; Y1 = cos(3*X1) ;
X2 = (0:360) *pi/180; Y2 = cos(3*X2) ;
figure (1)
subplot(2,2,1); plot(X1, Y1, 'o', 'MarkerSize', 3); xlim([0 2*pi])
subplot(2,2,2); plot(X1, Y1, 'LineWidth', 2); xlim([0 2*pi])
subp1ot(2,2,3); plot(X2, Y2,'o', 'MarkerSize', 3); xlim([0 2*pi])
subplot(2,2,4); plot(X2, Y2,'LineWidth', 2); xlim([0 2*pi])

(2) The running results are as follows:
Insert picture description here

Three, graphics drawing example

Suppose the function is
Insert picture description here
to draw the image of the function on MATLAB
Insert picture description here
.

(1) Prepare graphic data. The user needs to select the range of the data, select the independent variable of the corresponding range, and calculate the corresponding function value. As required, in this example,

You need to enter the following commands in the command line window:

x=-pi/2:0.01:pi/2; %自变量x定义域
y=x+sin(x)+exp(x); %函数表达式

(2) Use the plot function to draw graphics, that is, enter the following commands in the command line window:

plot(x,y);  %绘制x、y二维坐标图像

The results obtained are shown in the figure below.
Insert picture description here
(3) In order to better observe the position of each data point, set grid lines for the background, use hollow circles to mark the data points, and set the color of the curve to

red. Therefore, type in the command line window:

plot(x,y,'-ro')
grid on

The graphics are as follows:
Insert picture description here(4) Add some notes to the graphics. In order to further make the graphics readable, users also need to add some notes to the graphics, such as the name and coordinates of the graphics

The name, legend, and text description of the axis. For example, in this example, the graph is named "function image of y"; the x-coordinate axis and the y-coordinate axis are named "x" and "y" respectively; the legend is set

Set to "y=x+sin x+eX". Therefore, you need to enter in the command line window:

title('y的函数图像');
xlabel('x') ;
ylabel('y');
legend('y=x+sinx+e^{x}') ;

The running results are as follows:
Insert picture description here(5) Graphical output. After completing the drawing and editing of the graph, the user can print the graph or select File→Save As in the menu bar of the graph window.

Order to save the graphics in the format required by the user.

Fourth, the basic steps of drawing graphics

**Using MATLAB to draw graphics is roughly divided into the following 7 steps: **

(1) Data preparation. The main task is to generate sample vectors of independent variables and calculate the corresponding function value vectors.

(2) Select the position of the graphics window and the sub-picture. By default, the graphics drawn by the MATLAB system are figure.1, frgrere...

(3) Call drawing functions to draw graphics, such as the plot function.

(4) Set the range, scale and coordinate grid of the coordinate axis.

(5) Use the object attribute value or the graphics window toolbar to set the line type, mark type and size, etc.

(6) Add graphic annotations, such as graphic name, coordinate name, legend, text description, etc. .

(7) Export and printing of graphics.

At this point, the process of using MATLAB to draw two-dimensional coordinate graphics is over!

Guess you like

Origin blog.csdn.net/weixin_43335226/article/details/107253619