MATLAB drawing (1)

        The drawing of a two-dimensional curve is to connect the data on the plane, and the data points can be represented by vectors or matrices. This time we will introduce some commonly used drawing knowledge.

1. Plot function : When executing this command, a new graphics window will be created. If there is a graphics window open at this time, the original image will be overwritten.

(1)plot(x):

        ① When x is a real vector, the data is used as the ordinate by default, and the abscissa corresponds to the index according to the number of data;

plot([11:20]);

        ②When x is a real matrix, draw the element value of each column relative to its subscript curve (similar to the real vector form) by column, and the number of curves is equal to the number of columns of x;

plot(randi(20,3,3));

        ③ When x is a complex matrix, draw multiple curves with the real part of the element as the abscissa and the imaginary part as the ordinate by column.

(2)plot(x,y)

x=0:0.01:(pi*4);        
y=sin(x); 
plot(x,y);

(3) plot(x1,y1,x2,y2,...): similar to (2) usage

(4) plot(x,y,s): s is a string marked with single quotes, which is used to set the type, size or line color, thickness, etc. of data points

x=0:0.01:(pi*4);        
y=sin(x);     %以红色双划线的样式绘图
hold on;      %保持原图并继续作图
y1=cos(x);    %以蓝色点划线的样式绘图
plot(x,y,'--r',x,y1,'-.b')
grid on;    %网格
axis([0 pi*4 -1.1 1.1]);     %范围
legend('Sin(x)', 'Cos(x)');  %图例
title('sin,cos函数曲线');     %标题名
xlabel('x 坐标');      
ylabel('y 坐标');  % 横 、纵坐标名

        The figure below is a table of commonly used characters 2. Multi-graphic display

(1) subplot(m,n,p): Divide the current figure window into m×n areas, and draw under the p-th view.

x=0:0.01:(pi*2);   
y=sin(x);    y1=cos(x);    y2=tan(x);    y3=cot(x);
subplot(2,2,1)    plot(x,y)
subplot(2,2,2)    plot(x,y1)
subplot(2,2,3)    plot(x,y2)
subplot(2,2,4)    plot(x,y3)

(2) tiledlayout and nexttile functions: create a tiled area, and then call the axes.

x=linspace(-pi,pi);    y=cos(x);
tiledlayout(2,2)    % 将当前窗口布局为2*2的试图区域
nexttile    % 在第一个图块中创建一个坐标区对象
plot(x)
nexttile
plot(x,y)
nexttile([1 2])    % 第三个图块占据一行两列的坐标区
plot(x,y)

3. fplot function : a command specially used to draw the image of a one-variable function. The image drawn by this command is smoother and more accurate than that drawn by plot.

(1) fplot(f): Draw the curve of y=f(x) on the default interval [-5,5]. When defining the curve, pay attention to use the function handle instead;

(2) fplot(f, lim): lim is the specified range;

(3) fplot(funx, funy, lim): Draw the curves of x=fun(t) and y=fun(t) within the specified lim range;

y=@(x)x.^2;     % 匿名函数,通过函数句柄的写法

% 自定义的函数必须要通过匿名函数的形式进行定义     fplot(@fun)
figure(1);      fplot(y,'r'); 
figure(2);      fplot(y,'b',[-20,20]);  % 设定自变量范围

% MATLAB中自带的函数在调用时也需要加上 @ 符号
figure(3);      fplot(@sin,'g');
figure(4);      x1=@(t)sin(t);  y1=@(t)cos(t);
fplot(x1,y1,'--r')

figure(5);      syms x2;        y2=exp(x2);
h=fplot(y2,'b');  % 符号函数本身就是一个表达式,所以不需要添加 @
h.Color='g';    % 通过句柄修改线条颜色

(4) Drawing of piecewise function: drawing through range limitation and hold on function.

figure(6);       
hold on;
y1=@(t)t.^2-1;          
y2=@(t)log(t);
fplot(y1,[-1,1]);        
fplot(y2,[1,3]);
hold off;        
grid on;

4. Auxiliary drawing

 

Guess you like

Origin blog.csdn.net/weixin_58351753/article/details/127154155