Matlab Chapter III Drawing and Graphics
Basics of 2D Drawing
In this chapter, it is necessary to open Matlab and draw by yourself; the article only provides the corresponding code, encourage you to try and draw beautiful function images by yourself
Matlab drawing mainly includes 3 processes:
- Define function
- Specify drawing range
- Calling the function
plot(x,y)
Let us take the normal function as an example:
>> x=[ -5:1:5];
y=normpdf(x,0,1);
plot(x,y)
But the image drawn like this is rough. This is because our step size is too large, so we need to adjust the interval of the function:
x=[ start: interval: end ]; the default value of interval is 1
We might as well adjust the step size to 0.1:
>> x1=[-5:5];
x2=[-5:0.1:5];
y1=normpdf(x1,0,1);
y2=normpdf(x2,0,1);
plot(x1,y1,x2,y2)
This instruction will help you draw two normal functions with steps of 1 and 0.1 in the same picture. You can clearly see that the curve has become smoother!
We can add labels to the axis through xlabel
and ylabel
, and use to title
add a title, as long as the last line is slightly changed:
plot(x1,y1,x2,y2),xlabel('x'),ylabel('f(x)'),title('标准正态函数的图像');
In particular, when the image function we need to draw includes multiplication, for example: f = exp(-2*t)*sin(t);
calling plot directly will prompt error, we must use matrix multiplication: f = exp(-2*t).*sin(t);
(Squared equivalent)
Or use fplot
function fplot('exp(-2*t)*sin(t)',[0,4])
:;
fplot syntax:
fplot('function string', [xstart, xend])
The coordinate axis command
axis square
generates a square image;
axis equal
generates an image with exactly the same coordinates;
you can also use it axis auto
to let matlab choose the appropriate style;
axis()
you can set the coordinate axis range, the syntax is:axis([xmin xmax ymin ymax])
Adding a legend
Professional images are always accompanied by a legend to tell the reader what a certain curve is. It also only needs to be added plot(x,y)
later. Let's continue to take the normal distribution image as an example:
>> x = [-5:0.1:5];
y=normpdf(x,0,1);
plot(x,y),xlabel('x'),ylabel('f(x)'),legend('标准正态分布的概率密度函数')
Set lines and colors
matlab contains 4 kinds of lines, namely:
-
--
-.
:
And can call many different colors:
r-red; g-green; b-blue; k-black; w-white; y-yellow
Sub-pictures
subplot(m,n,p)
m and n indicate that the generated sub-picture has m rows and n columns, and p points to the graphics window. Let us illustrate with an example:
>> x=[-5:0.1:5];
y=sin(3*x);
z=2*cos(2*x);
subplot(1,2,1)
>>plot(x,y,'r'),xlabel('x'),ylabel('sin3x'),title('figure1')
>> subplot(1,2,2)
>> plot(x,z,'b'),xlabel('x'),ylabel('2cos2x'),title('figure2')
- Note that we must first use subplot to point to a specific subgraph, and then draw it with plot
The overlap of linspace and image
x=linspace(a,b,n)
means that n points are taken out uniformly between a and b. The default value of n is 100, for example:
>> x=linspace(0,2*pi);
>> plot(x,cos(x))
Each time the plot function is called again, the original image will be deleted. In order to draw overlapping images, we can use hold on
commands, for example:
>> x=linspace(0,2*pi);
plot(x,cos(x))
>> hold on
>> plot(x,sin(x))
>> axis([0 2*pi -1.5 1.5]) %调整坐标轴以让我们的图像更为好看
In this way, overlapping cosx and sinx images can be drawn on one picture.