MATLAB's implicit function drawing, graphics modification, image processing and animation production, and interactive drawing tools

1. Implicit function drawing

  • If an explicit expression of the function is given, the argument vector can be set first, and then the function vector can be calculated according to the expression, so that the functions such as plot, , plot3etc. can be used to draw graphs.
  • However, if the function is given in the form of an implicit function, it is difficult to draw a graph with the above method. For this, MATLAB provides some functions to draw implicit function graphs.

1. Implicit function two-dimensional drawing

  • Implicit function 2D drawing adopts ezplotthe function , which has various deformations, and the common usage methods are introduced below.
  • (1) For the function y = f ( x ) y=f(x)y=f ( x ) , the calling format of the ezplot function is as follows.
  • ezplot(f): In the default interval − 2 π < x < 2 π -2\pi<x< 2\pi2 p<x<2 π ploty = f ( x ) y=f(x)y=Graph of f ( x ) . Among them,fff can be a function file name or a string of function expressions, or an anonymous function expression or function name.
  • ezplot(f,[a,b]): In the interval a < x < b a<x<ba<x<bplot y = f(x) y= f(x)y=Graph of f ( x ) .
  • (2) For the implicit function f ( x , y ) = 0 f(x,y)=0f(x,y)=0 , the calling format of the ezplot function is as follows.
  • ezplot(f): In the default interval − 2 π < x < 2 π -2\pi<x< 2\pi2 p<x<2 π− 2 π < y < 2 π -2\pi<y< 2\pi2 p<y<2 π plotf ( x , y ) = 0 f(x,y)=0f(x,y)=0 graphics.
  • ezplot(f,[a,b]): In the interval a < x < b a<x<ba<x<b a < y < b a<y<b a<y<b drawf ( x , y ) = 0 f(x,y)=0f(x,y)=0 graphics.
  • eplot(f,[xmin,xmax.ymin,ymax]): In the interval xmin < r < xmax xmin<r<xmaxx min<r<xmax y m i n < y < y m a x ymin<y<ymax y min<y<y ma x drawf ( x , y ) = 0 f(x,y)=0f(x,y)=0 graphics.
  • (3) For the parametric equation x = x ( t ) x=x(t)x=x(t) y = y ( t ) y=y(t) y=y ( t ) , the calling format of the ezplot function is as follows.
  • eplot(x,y): In the default interval 0 < x < 2 π 0<x<2\pi0<x<2 π plottingx = x ( t ) x = x(t)x=x(t) y = y ( t ) y=y(t) y=Graph of y ( t ) .
  • ezplot(x,y [tmin,tmax]):在区间 t m i n t < t < t m a x tmint<t<tmax t who t<t<t max x drawx = x ( t ) x=x(t)x=x(t) y = y ( t ) y=y(t) y=Graph of y ( t ) .
  • For example, we conduct the exercise of drawing implicit functions as follows.
  • The procedure is as follows:
subplot(2,2,1);
ezplot('x^2+y^2-9');
axis equal;
subplot(2,2,2);
ezplot(@(x,y) x^3+y^3-5*x*y+1/5);
subplot(2,2,3);
ezplot('cos(tan(pi*x))',[0,1]);
subplot(2,2,4) ;
ezplot('8*cos(t)','4*sqrt(2)*sin(t)',[0,2*pi]);
  • The program running result is shown in the figure below.

insert image description here

3. Implicit function 3D drawing

  • The implicit function 3D drawing functions include ezcontour, ezcontourf, ezmesh, ezmeshc, ezplot3, ezpolar, ezsurf, ezsurfe, and their calling formats are basically the same, just refer to the help information when necessary.
  • Below, we take ezsurfthe function as an example to introduce common usage methods. ezsurfThe function call is the function of surfthe function , and its calling format is as follows.
  • (1) ezsurf(f): Draw surface z = f ( x , y ) z=f(x,y)z=f(x,y ) , whereffThe representation of f is the same as the ezplot function. x, yx, yThe default range of x and y is − 2 π < x < 2 π -2π<x<2π2 p<x<2 π− 2 π < y < 2 π -2π<y<2π2 p<y<2 p .
  • (2) ezsurf(f,[xmin,xmax,ymin,ymax])or : draw a surface z = f ( x , y ) z=f(x,y)ezsurf(f[min,max]) in the specified intervalz=f(x,y)
  • (3) ezsuf(x,y,z): In the default region − 2 π < s < 2 π -2π<s<2π2 p<s<2π − 2 π < t < 2 π -2π<t<2π 2 p<t<Plot the parametric equationx = x ( s , t ) x=x(s,t) on 2 πx=x(s,t) y = y ( s , t ) y=y(s,t) y=and ( s ,t) z = z ( s , t ) z=z(s,t) z=z(s,t ) surface.
  • (4) ezsurf(x,y,z,[smin,smax,tmin,tmax])or ezsurf(x,y,[min,max]): Use the specified area to draw a parametric equation surface.
  • For example, we plot the following surface: { x = e − s cos ⁡ ty = e − s sin ⁡ tz = t , 0 ≤ s ≤ 8 , 0 ≤ t ≤ 5 π \left\{\begin{matrix}x=e ^{-s}\cos t \\y=e^{-s}\sin t \\z=t \end{matrix}\right.\begin{matrix},0\le s\le 8,0\ le t\le 5\pi \end{matrix} x=escosty=essintz=t,0s8,0t5 p.m
  • The procedure is as follows:
>> ezsurf('exp(-s)*cos(t)','exp(-s)*sin(t)','t',[0,8,0,5*pi])
  • The program running result is shown in the figure below.

insert image description here

2. Graphic modification

  • Graphics retouching can make the graphics more realistic and convey richer information by means of rendering and rendering. Graphics modification processing includes methods such as viewpoint processing, color processing and cropping processing.

1. Viewpoint processing

  • Observing objects from different viewpoints, the shapes of objects seen are different. Similarly, graphics drawn from different viewpoints have different shapes. The viewpoint position may be represented by azimuth and elevation.
  • The azimuth angle is also called the rotation angle, which is the line between the viewpoint and the origin in xy xyProjection on x y plane and yyThe angle formed by the negative direction of the y- axis, a positive value means counterclockwise, and a negative value means clockwise.
  • The elevation angle is also called the angle of view, which is the line between the viewpoint and the origin and xy xyThe included angle of the xy plane, a positive value indicates that the viewpoint is atxy xyAbove the xy plane, a negative value indicates that the viewpoint is at xy xybelow the xy plane. For example, the definition of the viewpoint in the coordinate system is shown in the figure below, and the direction of the arrow in the figure indicates the positive direction.

insert image description here

  • MATLAB provides a function to set the viewpoint view, and its calling format is as follows:
    view(az,el)
  • Among them, az is the azimuth angle, el is the elevation angle, and they are all in degrees. The default viewpoint of the system is defined as -37.5° in azimuth and 30° in elevation.
  • For example, we plot multimodal functional surfaces from different viewpoints.
  • The procedure is as follows:
subplot(2,2,1);
mesh(peaks);
view(-37.5,30);  %指定子图1的视点
title('azimuth=-37.5,elevation=30');
subplot (2,2,2);
mesh(peaks);
view(0,90);  %指定子图2的视点.
title('azimuth=0,elevation=90');
subplot(2,2,3);
mesh(peaks);
view(90,0);  %指定子图3的视点
title('azimuth=90,elevation=0');
subplot(2,2,4);
mesh(peaks);
view(-7,-10);  %指定子图4的视点
title('azimuth=-7,elevation=-10');
  • The program running result is shown in the figure below.

insert image description here

2. Color processing

2.1 Vector representation of color

  • In addition to using characters to represent colors, MATLAB can also use vectors containing 3 elements to represent colors. The vector elements take values ​​in the range [0,1], and the three elements respectively represent the relative brightness of the three colors of red, green, and blue, which are called RGB triplets. In the table below, we list the RGB values ​​of several common colors.
RGB value color character RGB value color character
[0 0 1] blue b [1 1 1] White w
[0 1 0] green g [0.5 0.5 0.5] grey
[1 0 0] red r [0.67 0 1] Purple
[0 1 1] blue c [1 0.5 0] orange
[1 0 1] Magenta m [1 0.62 0.40] copper
[1 1 0] yellow y [0.49 1 0.83] Sapphire blue
[0 0 0] black k

2.2 Colormap

  • Color Map (Color Map) is a concept introduced by the MATLAB system. In MATLAB, there can only be one colormap per graphics window. The colormap is m × 3 m × 3m×A numeric matrix of 3 whose rows are RGB triplets.
  • The colormap matrix can be generated artificially, or the function provided by MATLAB can be called to define the colormap matrix. In the table below we list the functions that define the colormap matrix. The dimension of the colormap matrix is ​​determined by the format of the function call. For example:
    M=hot;
  • Generate 64×3 64×364×3 color map matrixMMM , the color represented is from black, red, yellow to white, from thick to light. Another example:
    P=gray(100);
  • Generate 100×3 100×3100×3- color map matrixPPP , the color represented is gray from thick to light.
Function name meaning Function name meaning
autumn red, yellow light color jet Saturation value color with blue head and red tail
bone shades of blue lines Use plot to draw line color
colorcube Three thick and light colorful interlaced colors pink pale pink figure
cool Cyan, magenta shades prism Spectral cross color
copper Pure Copper Tone Linear Shades spring Blue, yellow thick light color
flag Red-white-blue-black interlaced color summer Green, yellow light color
gray gray tone linear shades winter blue, green shades
hot Black, red, yellow, white thick and light colors white all white
hsv Saturation value color with red ends
  • Except for plotand its derived functions, functions such as mesh, , surfetc. all use colormap coloring. To set and change the color map of the graphics window, use colormapthe function , and its calling format is as follows:
    colormap(M)
  • where MMM represents the colormap matrix.

2.3 Coloring of 3D Surface Graphics

  • The three-dimensional surface graphics are actually painting colors on each grid piece of the grid map. surfThe function shades the grid pieces using the default coloring method. In addition, you can also use shadingthe command to change the coloring method, the format is as follows.
  • (1) shading faceted: Color each grid piece with the color corresponding to its height, but the grid lines are still reserved, and their color is black. This is the system default coloring method.
  • (2) shading flat: Color each grid piece with the same color, and the grid lines also use the corresponding color, so that the graphic surface appears smoother.
  • (3) shading interp: The color interpolation process is adopted in the grid slice, and the surface map obtained is the smoothest.
  • For example, we show the effects of different coloring methods.
  • The procedure is as follows:
t=0:pi/20:2*pi;
z=peaks(20);
colormap(jet);
subplot(2,2,1);
surf(z);
subplot(2,2,2);
surf(z);
shading flat;
subplot(2,2,3);
surf(z);
shading interp;
subplot(2,2,4);
mesh(z);
shading interp;
  • The program running result is shown in the figure below.

insert image description here

3. Cutting processing of graphics

  • The NaN constant defined by MATLAB can be used to represent unusable data. Using this feature, the function value corresponding to the part that needs to be cropped in the graph can be set to NaN, so that when drawing the graph, the part whose function value is NaN will not be displayed out, so as to achieve the purpose of cutting the graphics.
  • For example, if we want to cut off the top or bottom of a sine wave greater than 0.5, we can use the following procedure:
x=0:pi/10:4*pi;
y=sin(x);
i=find(abs(y)>0.5);
x(i)=NaN;
plot(x,y);
  • For example, we draw two spheres, one of which is inside the other, and cut off a part of the outer ball so that the inner ball can be seen.
  • The procedure is as follows:
[x,y,z]=sphere(20);  %生成外面的大球
z1=z;
z1(:,1:4)=NaN;  
%将大球裁掉一部分
c1=ones(size(z1));
surf(3*x,3*y, 3*z1,c1);  
%生成里面的小球
hold on;
z2=z;
c2=2*ones(size(z2));
c2(:,1:4)=3*ones(size(c2(:,1:4)));
surf(1.5*x,1.5*y,1.5*z2,c2);
  • The program running result is shown in the figure below. The color chart uses 3 colors, the outer ball is blue and the inner ball is two different shades.

insert image description here

3. Image processing and animation production

  • Image processing and animation production can be regarded as a further expansion of the drawing function, and it is also a specific application field of MATLAB. When learning the basics of MATLAB, knowing some image processing and animation production methods will be helpful for future use.

1. Image processing

  • The MATLAB basic system provides several functions for simple image processing, which can be used to read/write and display images. In addition, MATLAB also has a more powerful image processing toolbox, which can perform more professional processing on images.

1.1 Image read/write

  • To use an image in a different format in MATLAB, you need to use imreadthe function to read the image, that is, to read the image file into the MATLAB workspace. imreadThe calling format and function of the function are as follows.
  • (1) x=imread(ilename,fmt): Where filename is the file name of the image, and fmt specifies the format of the image file. MATLAB supports a variety of image file formats, such as bmp, jpg, jpeg, tif, etc.
  • When we omit the fmt parameter, the format of the file is automatically determined from the content of the file. matrix xxx is the data read from the image file and converted into an image format recognizable by MATLAB.
  • In MATLAB, an image usually consists of a data matrix and a color matrix. If the image is a grayscale image, then xxx is two-dimensional. If the image is true color, thenxxx is a three-dimensional matrix, and the third dimension stores color data.
  • (2) [x,map]-imread(filename,fimt): Save the converted image data to the matrix xxIn x , read the relevant color map data into the matrix map at the same time.
  • In MATLAB, imwritethe function is used to write the image data and the colormap data together into the image file, and the calling format is as follows:
    imwrite(x,filename,fmt)
  • This function takes the image data xxx output to the file filename, the type of image is specified by fmt.

1.2 Display of images

  • MATLAB imageuses the function to display images, and the calling format is as follows:
    image (x)
  • where xxx is the data matrix of the graph.
  • imageThe function similar to the function is imagescthat its calling format and functions are the same as imagethe function , but the image coloring method is different.
  • In order to ensure the display effect of the image, generally colormapthe function to set the image colormap.
  • There is an image file logo.jpg, and the image is displayed in the graphics window. The procedure is as follows:
[x,cmap]=imread('logo.jpg');  %读取图像的数据阵和色图阵
image(x);
colormap(cmap);
axis image off  %保持宽高比并取消坐标轴

2. Animation production

  • MATLAB has the ability to make animations. It can store a series of various types of two-dimensional or three-dimensional images, and then play them in order like a movie, which is called frame-by-frame animation. For example, take a three-dimensional figure and slowly rotate it so that it can be viewed from different angles. Another type is the animation that depicts the trajectory of particle motion, called trajectory animation.

2.1 Make frame-by-frame animation

  • MATLAB provides getframe, movieinand moviefunctions for frame-by-frame animation production. The functions of the functions are as follows.
  • (1) getframeFunction: It can intercept a picture information (called a frame in the animation), and a picture information forms a large column vector. Obviously, saving nnA large matrix is ​​required for n pictures.
  • (2) moviein(n)Function: used to create a sufficiently large nnn- column matrix. This matrix is ​​used to holdnnn frames of data for playback. The reason for building a large matrix in advance is to improve the speed of the program.
  • (3) movie(m,n)Function: Play by matrix mmThe picture nndefined by mn times, play once by default.
  • For example, we play a sphere with a changing diameter.
[x,y,z]=sphere(50);
m=moviein(30);  %建立一个30列大矩阵
for i=1:30
    surf(i*x,i*y,i*z);  %绘制球面
    m(:,i)=getframe;  %将球面保存到m矩阵
end
movie(m,10);  %以每秒10幅的速度播放球面

2.2 Create trajectory animation

  • MATLAB provides cometand comet3functions to show the trajectory of particles in two-dimensional plane and three-dimensional space, this kind of trajectory curve is called comet trajectory curve. The function call format is as follows:
    comet(x,y,p)
    comet3(x,y,z,p)
  • Among them, each group x , y , zx, y, zx , y , z form a set of coordinate parameters of the curve, the plotusageplot3is the same as that of and functions. ppp is the parameter used to set the comet length, the default value is 0.1. In two-dimensional graphics, the coma length isyypp of length of y vectorp times. In 3D graphics, the coma length iszzpp of z- vector lengthp times.
  • For example, we generate a 3D motion graphics trajectory.
x=0:pi/250:10*pi;
y=sin(x);
z=cos(x);
comet3(x,y,z);
  • After running the program, a screen in the animation is shown in the figure below. The small circle in the figure represents the head of the comet, which tracks the data points on the screen. The comet track is the curve behind the small circle, and the change process of the curve dynamically shows the trajectory of the particle.
    insert image description here

4. Interactive drawing tools

  • MATLAB provides a variety of functions for plotting that can be called from the Command Window or from within a program. In addition, MATLAB provides interactive plotting tools.

1. Drawing tab

  • There is a plotting tab in MATLAB's ribbon, which provides basic tools for plotting.
  • There are 3 command groups in the toolbar of the drawing tab, the selection command group on the left is used to display the variables selected for drawing; the drawing command group in the middle provides commands for drawing various graphics; the option command group on the right It is used to set whether to create a new graphics window when drawing.
  • If no variable is selected, the commands in the Drawing command group are unavailable. If a variable is selected in the workspace, the drawing command group will automatically provide the corresponding drawing command according to the selected variable type. At this time, click a drawing command button, and the command will be automatically input in the command line window (the command is selected The variable is the parameter) and execute, and draw the graph in the graph window.
  • For example, to draw a sine curve with the tools in the drawing tab, first create xx in the command line windowxyyy , and then select x, yx, yin the workspace windowx , y , and click the plot button in the drawing tab, then the plot(x,y) command will appear in the command line window, and then the graphics window will pop up and the sine curve will be drawn.

2. Drawing Tools

  • When drawing graphics, if we need to modify the drawing parameters, we can use the drawing tools (Plot Tools) in the MATLAB graphics window.
  • In the shortcut toolbar of the graphics window, click the display plotting tools and docking graphics buttons on the far right, or enter the command plottools in the command line window of MATLAB to start the plotting tools.

insert image description here

  • The drawing tool consists of three parts. The left side of the image editing area is the graphics palette, the right side is the drawing browser, and the lower part is the attribute editor.

2.1 Graphics Palette

  • The Graph Palette is used to add and arrange subplots in the graph window, view and select plot data, and add graph annotations. To open the graphics palette, enter the following command in the command line window:
>> figurepalette
  • The graphics palette contains 3 panels, and their functions are as follows.
  • (1) New submap panel: used to add 2D and 3D submaps. For example, to split the graphics window into 2 × 2 2 × 22×2 for the 4 subgraphs used to draw two-dimensional graphics, click the expand button on the right of the two-dimensional coordinate axis option, and then click the second row and second column square.
  • (2) Variable panel: used to browse and select drawing data. If you double-click a variable, plotthe function . If you right-click on the selected variable, you can choose a drawing function to draw the graph from the pop-up shortcut menu.
  • (3) Annotation panel: used to add annotations to graphics. Select a label tool from the panel to draw graphics for labeling in the graphics window, such as straight lines, arrows, and label text boxes.

2.2 Drawing Browser

  • The drawing browser lists the elements in the drawing as a legend. Select an object in the drawing browser, a black mark appears on the object in the graphics window, and the property editor displays the properties of the object.

2.3 Property Editor

  • The attribute editor is used to observe and set parameters such as the name, color, and filling method of the selected object. The content in the property editor is different for different object types.

Guess you like

Origin blog.csdn.net/weixin_45891612/article/details/130805766