Image overlay and linspace commands
If we draw an image of a function and then decide to draw another image on the same graph, we can achieve this goal by telling MATLABhold on:
In the following example, we will draw sin (x) and cos (x ) On the same picture.
First let's learn the linspace command:
x = linspace(a,b)
MATLAB will take 100 points (vectors) evenly distributed from a to
b.If written as:
x = linspace(a,b,n)
Then MATLAB will take n points distributed evenly between a and b. Then we use this tool to draw cos (x) and sin (x) images, first let us take 100 points evenly between 0 ~ 2 * pi:
x = linspace(0,2*pi);
Then we draw cos (x):
plot(x,cos(x))
As shown:
If we enter:
plot(x,sin(x))
MATLAB will wash away the previous output, and the current graphics window becomes the following:
At this moment, it should be noted that
although we define the value range of x to be between 0 and 2 * pi, the graphics ratio calculated by MATLAB is calculated by the function The image is a bit wider, we can use the axis () command to adjust when calling plot (x, sin (x)):
>> plot(x,sin(x)),axis([0 2*pi -1 1])
Image:
Back to our previous question, we want to draw sin (x) and cos (x) two images on an image, which can be done by the following command:
>> x = linspace(0,2*pi);
>> y1 = sin(x);
>> y2 = cos(x);
>> plot(x,y1),axis([0 2*pi -1 1])
>> hold on
>> plot(x,y2),axis([0 2*pi -1 1])
image: