Powerful graphics is Matlab one of the characteristics, Matlab provides a set of drawing functions, we can enter the search interface in the upper right corner of Matlab “Types of MATLAB Plots”, then open the “Types of MATLAB Plots” document, we You can see that Matlab can draw various graphs as shown in the figure below.
Matlab can not only draw two-dimensional images but also three-dimensional images, and even animations, making the drawing in your study, research and work simple and beautiful. The following three different types of images are drawn by Matlab.
When deciding what graphics to draw, initially do not need to think too much about the details in the image, just choose the drawing function we need. After determining the drawing function, we can get the graph we want by giving or importing the basic data and parameters. Of course, Matlab can directly operate a series of objects such as coordinate axes, curves (color, thickness), text, etc. directly on each element of the graph. After setting the presentation mode of the image (through the drawing function) and continuously adjusting the various elements in the graphic, you can save your own drawing code, and you can draw you with one click after importing the data. The desired image. Here I use the three-dimensional surface animation as an example to explain how to draw in Matlab.
The decomposition of the three-dimensional surface animation seems to be obtained from the continuous change of the three-dimensional surface map one by one, then we must first make a separate three-dimensional surface map. By viewing the Matlab graphics type document mentioned above, we can find surf and mesh functions to draw 3D surfaces in the “Surface and Mesh Plot” column. Here we choose surf to draw, so how is the function surf used? Click to open the surf documentation, which has detailed instructions for using surf. Use Matlab’s own instructions, examples, or check the methods used by others on the Internet. We can basically clarify whether Z is the most critical whether to use surf (Z) or surf (X, Y, Z) for three-dimensional drawing. If Z is a matrix of m rows✖n columns, then surf(Z) is equivalent to projecting each element of matrix Z into a three-dimensional space to form a point. The height of the point (z, which also represents color) is The specific value of the element, the x and y coordinates correspond to the index position of the element in the matrix. Finally, connect adjacent points with lines to form a small three-dimensional surface, and finally draw a smooth three-dimensional surface.
For example, we want to draw a three-dimensional graph of f(x, y) = x^2+y^2, where x and y range from -5 to 5. We can achieve this through the following code:
x=-5:0.25:5;% sets the value range and precision of x
y=x; %y has the same range and precision as x
[X,Y]=meshgrid(x,y);% returns the grid matrix related to x and y
Z=X.^2+Y.^2;% generates all values of f(x,y)=x^2+y^2 within the interval
surf(X,Y,Z);% draw 3D surface graph
Finally we got the following picture
So in the end, let’s draw a three-dimensional surface animation based on this picture! The animation effect is that the surface flattens downward, continues to bend downward to the same degree as when bending upward, and then returns to the initial state. The actual change in this process is the height, that is, the Z value, then we take N different Z values in sequence and draw a picture, and then connect each image together. In Matlab, the above functions can be realized by generating animated pictures in gif format. The specific code and renderings are as follows:
x=-5:0.25:5;% sets the value range and precision of x
y=x; %y has the same range and precision as x
[X,Y]=meshgrid(x,y);% returns the grid matrix related to x and y
Z=X.^2+Y.^2;% generates all values of f(x,y)=x^2+y^2 within the interval
fact=2*sin(0:0.1*pi:2*pi);% sets the change factor of Z, here the periodic function sin is used, and the value of a whole period is taken
for k=1:length(fact)
surf(X,Y,fact(k)*Z);
axis([-5 5 -5 5 -100 100])% Set the coordinate axis range
caxis([-120 120])% Set the coordinate color range
axis off% hidden axis
title(‘3D animation’);% set the image name
frame=getframe(gcf); %Get the current frame
im=frame2im(frame);% make a gif file, the image must be an index index image
[I,map]=rgb2ind(im,256);% converted to gif pictures, only 256 colors can be used
if k==1
% The first picture is directly saved to the directory
imwrite(I,map,strcat(‘C:\toutiao\20180718\’,’3D-Animation2′,’.gif’),’gif’,’Loopcount’,inf,’DelayTime’,0.1);
else% The remaining pictures continue to the previous picture, each picture interval is 0.1 seconds
imwrite(I,map,strcat(‘C:\toutiao\20180718\’,’3D-Animation2′,’.gif’),’gif’,’WriteMode’,’Append’,’DelayTime’,0.1);
end
end
The above is the drawing function of Matlab introduced today. Although only one drawing function is involved, how to use the function has been introduced in the article (the code also wrote a note), and all other functions are also the same. I hope this article is helpful to you. If you want to learn something, you can leave a message below. As long as I know it, I will try my best to answer it.