Article Directory
The most commonly used drawing function in MATLAB is plot. According to different coordinate parameters, it can draw different curves on a two-dimensional plane.
plot function
Calling format : plot (x, y) where x and y are coordinate vector
functions Function : draw XY two-dimensional curve with vector x as X axis and vector y as Y axis
Example 1: Draw a sine curve in the interval [0,2π] y = sin (x)
x=0:pi/100:2*pi;
y=sin(x);
plot(x,y)
Results:
Example 2 Simultaneous drawing of sine curve y = sin (x) and cosine function y = cos (x) in the interval [0,2π]
x=0:pi/100:2*pi;
y1=sin(x);
y2=cos(x);
plot(x,y1,x,y2)
Result:
Adding some parameters to the plot drawing instruction can draw graphics with different colors and different line types
Example 3 Simultaneously draw sinusoids y = sin (x) and cosine function y = cos (x) of different line types and different colors in the interval [0,2π]
x=0:pi/100:2*pi;
y1=sin(x);
y2=cos(x);
plot(x,y1,'k:',x,y2,'b-')
result:
The line style and color of each curve is specified by the string 'cs' where c is the color s is the line style
Correspondence between color and line type:
Color symbol | colour | Line symbol | Linear |
---|---|---|---|
and | yellow | . | point |
m | purple | - - | dotted line |
r | red | + | plus |
g | green | * | Asterisk |
b | blue | - | solid line |
w | white | : | Dotted line |
k | black | .- | Dotted line |
Set the coordinate axis
When drawing a graph, the system automatically gives the coordinate axis of the graph. The user can also use the axis function to reset it.
Example 4 Draw a sine curve in the coordinate range 0 ≤ x ≤ 2π, −1 ≤ y ≤ 2
x=linspace(0,2*pi,60);
y=sin(x); %生成含有 60 个数据元素的向量 x
plot(x,y);
axis([0,2*pi,-1,2]); %设定坐标范围
The result is:
axis function call
form | Features |
---|---|
axis([xmin xmax yminmax]) | Set the maximum and minimum values of the coordinate axis |
axis ('auto') | Return the coordinate system to the automatic default state |
axis(‘off’) | Turn off the coordinate system |
axis(‘on’) | Display coordinate system |
Add legend
x=0:pi/100:2*pi;
y1=sin(x);
y2=cos(x);
plot(x,y1,'k:',x,y2,'b-')
title('sine and cosine curves');
xlabel('independent variable X');
ylabel('dependent variable Y');
text(2.8,0.5,'sin(x)');
text(1.4,0.3,'cos(x)');
legend('sin(x)','cos(x)');
The result is:
subplot function-draw multiple parallel graphs
Form: subplot (m, n, p) The
effect is: the drawn sub-plot has m lines, each line has n figures, and the currently drawn figure is the p-th
such as subplot (4,2,3) refers to co-drawing Four rows of subgraphs, each row has 2 graphs, the graph currently being drawn is the third one, which is the first one in the second row
Example:
x=linspace(0,2*pi,60);
y=sin(x);
z=cos(x);
t=sin(x)./(cos(x)+eps);
ct=cos(x)./(sin(x)+eps);
subplot(2,2,1);
plot(x,y);
title('sin(x)');
subplot(2,2,2);
plot(x,z);
title('cos(x)');
subplot(2,2,3);
plot(x,t);
title('tangent(x)');
subplot(2,2,4);
plot(x,ct);
title('cotangent(x)');
The effect is:
Two-dimensional drawing summary: