MATLAB's basic functions for drawing 3D graphics, 3D surfaces and other 3D graphics

  • Three-dimensional graphics have stronger data representation capabilities, so MATLAB provides a wealth of functions to draw three-dimensional graphics. The method of drawing 3D graphics is very similar to that of 2D graphics, and many of them are extended on the basis of 2D graphics.

1. Basic functions for drawing three-dimensional curves

  • The basic three-dimensional graphics function is that plot3it extends the related functions plotof to the three-dimensional space, and is used to draw the three-dimensional curve.
  • plot3The usage of the function is very similar to that of plotthe function , and its calling format is as follows:
plot3(x1,y1,z1,选项1,x2,y2,z2,选项2,,xn,yn,zn,选项n) 
  • Among them, each group x , y , zx, y, zx , y , z form a group of coordinate parameters of the curve, and the definition of plotthethe basic functions and auxiliary operations of MATLAB's two-dimensional graphics drawing).
  • x 、 y 、 z x、y、z When x , y , z are vectors of the same length, thenx , y , zx, y, zThe corresponding elements of x , y , z form a three-dimensional curve.
  • x 、 y 、 z x、y、z When x , y , z are matrices of the same type, usex , y , zx, y, zx , y , z correspond to column elements to draw a three-dimensional curve, and the number of curves is equal to the number of matrix columns.
  • For example, we plot the space curve: { x 2 + y 2 + z 2 = 64 y + z = 0 \left\{\begin{matrix}x^{2}+y^{2}+z^{2}= 64 \\y+z=0 \end{matrix}\right.{ x2+y2+z2=64y+z=0
  • The parametric equation corresponding to the curve is { x = 8 cos ⁡ ty = 4 2 sin ⁡ tz = − 4 2 sin ⁡ t , 0 ≤ t ≤ 2 π \left\{\begin{matrix}x=8\cos t \\ y=4\sqrt{2} \sin t \\z=-4\sqrt{2} \sin t \end{matrix}\right.\begin{matrix},0\le t\le 2\pi \end {matrix} x=8costy=42 sintz=42 sint,0t2 p.m
  • The procedure is as follows:
t=0:pi/50:2*pi;
x=8*cos(t);
y=4*sqrt(2)*sin(t);
z=-4*sqrt(2)*sin(t);
plot3(x,y,z,'p');
title('Line in 3-D Space');
text(0,0,0,'origin');
axis ([-10,10,-10,10,-6,6]);
xlabel('X');
ylabel('Y');
zlabel('Z');
grid;
  • The program running result is shown in the figure below.

insert image description here

2. Three-dimensional surface

1. Generation of plane grid coordinate matrix

  • 绘制 z = f ( x , y ) z=f(x,y) z=f(x,y ) represents the three-dimensional surface graph, first inxy xySelect a matrix area on the xy plane, assume that the rectangular areaD = [ a , b ] × [ c , d ] D=[a,b]×[c,d]D=[a,b]×[c,d ],Latest[ a , b ] [a,b][a,b ] atxxThe x direction is divided intommm parts,[ c , d ] [c,d][c,d ]yyThe y direction is divided intonnn copies.
  • Draw straight lines parallel to the two coordinate axes from each dividing point, and divide the area DDD is divided intom × nm × nm×n small rectangles, generate a plane grid coordinate matrix representing the vertex coordinates of each small rectangle, and finally use related functions to draw.
  • There are two methods to generate the grid coordinate matrix in the plane area.
  • (1) Generated by matrix operations.
x=a:dx:b;
y=(c:dy:d)';
X=ones(size(y))*x;
Y=y*ones(size(x));
  • In the above program segment, the matrix X of XEach row of X is the vector xxx , number of rows equal to vectoryyThe number of elements of y , matrix YYEach column of Y is the vector yyy with the number of columns equal to the vectorxxThe number of elements of x .
  • So XXXYYElements at the same position of Y ( X ( i , j ) , Y ( i , j ) (X(i, j), Y(i, j)(X(i,j)Y(i,j ) is exactly the regionDDD ( i , j ) (i, j) (i,j ) The coordinates of the grid points. If according to each grid pointx, yx, yFind the function valuezz of x and y coordinatesz , then the function value matrixZZZ
  • 显然, X 、 Y 、 Z X、Y、Z The coordinates corresponding to each column or row of X , Y , Z correspond to a space curve, and a set of space curves forms a space surface.
  • (2) Use meshgridthe function to generate.
x=a:dx:b;
y=c:dy:d;
[X,Y]=meshgrid(x,y);
  • After the program segment runs, the obtained grid coordinate matrix X, YX, YX , Y are the same as those obtained by method (1). whenx = y x = yx=y ,meshgridthe function can be written as meshgrid(x).
  • In order to illustrate the use of the grid coordinate matrix, an example is given below, which cleverly uses the grid coordinate matrix to solve the indeterminate equation.
  • 例如,已知 6 < x < 30 , 15 < y < 36 6<x<30,15<y<36 6<x<3015<y<36 , we find the indeterminate equation2 x + 5 y = 126 2x+5y=1262x _+5 y=Integer solution of 126 .
  • The procedure is as follows:
x=7:29;
y=16:35;
[x,y]=meshgrid(x,y);  %[7,29]*[16,35]区域生成网格坐标
z=2*x+5*y;
k=find(z==126);  %找出解的位置
x(k)'  %输出对应位置的x即方程的解
y(k)'  %输出对应位置的y即方程的解
  • The result of the program running is as follows:
ans =

     8    13    18    23


ans =

    22    20    18    16

  • That is to say, the equation has a total of 4 solutions: (8,22), (13,20), (18,18), (23,16).

2. Functions for drawing three-dimensional surfaces

  • MATLAB provides meshthe and surffunctions to plot 3D surface plots. meshThe function is used to draw a three-dimensional grid plot. When it is not necessary to draw a particularly fine three-dimensional surface diagram, the three-dimensional surface can be represented by a three-dimensional mesh diagram. surfThe function is used to draw a three-dimensional surface graph, and the complement surface between each line is filled with color.
  • meshThe calling format of function and surffunction is as follows:
mesh(x,y,z,c)
surf(x,y,z,c)
  • In general, x , y , zx, y, zx , y , z are matrices of the same type. x, yx, yx , y are the grid coordinate matrix,zzz is the height matrix over the grid points,ccc is called the color scale (color scale) matrix, which is used to specify the color of the surface.
  • By default, the system according to ccThe proportional relationship of the size of the elements in c transforms the color scale data into the corresponding color in the color map matrix.
  • when ccWhen c is omitted, MATLAB thinks thatc = zc=zc=z , that is, the setting of the color is proportional to the height of the graph, so that a three-dimensional graph with distinct layers can be obtained.
  • x、yx、yWhen x and y are omitted, putzzThe column subscript of the z matrix is ​​treated as xxx- axis coordinates, putzzThe row subscript of the z matrix is ​​treated as yyy- axis coordinates, and then draw a three-dimensional surface plot.
  • x、yx、yWhen x and y are vectors, xxThe length of x is equal to zzthe number of columns of the z matrix, yyThe length of y is equal to zznumber of rows of the z matrix, x, yx, yThe combination of x , y vector elements constitutesthe x , yx , yx , y coordinates,zzThe z coordinate is taken fromzzz matrix, and then plot the 3D surface plot.
  • For example, we plot the 3D surface z = sin ⁡ y cos ⁡ xz=\sin y\cos xz=sinycosx
  • In order to facilitate the analysis of the characteristics of various three-dimensional surfaces, three different forms of surfaces are drawn below.
  • Program 1 is as follows:
x=0:0.1:2*pi;
[x,y]=meshgrid(x);
z=sin(y).*cos(x);
mesh(x,y,z);
xlabel('x-axis');
ylabel('y-axis');
zlabel('z-axis');
title('mesh');
  • The result of running program 1 is shown in the figure below.

insert image description here

  • Program 2 is as follows:
x=0:0.1:2*pi;
[x,y]=meshgrid(x);
z=sin(y).*cos(x);
surf(x,y,z);
xlabel('x-axis');
ylabel('y-axis');
zlabel('z-axis');
title('surf');
  • The running result of program 2 is shown in the figure below.

insert image description here

  • Program 3 is as follows:
x=0:0.1:2*pi;
[x, y]=meshgrid(x);
z=sin(y).*cos(x);
plot3(x,y,z);
xlabel('x-axis');
ylabel('y-axis');
zlabel('z-axis');
title('plot3');
grid;
  • The running result of program 3 is shown in the figure below.

insert image description here

  • The lines in the mesh map (mesh) are colored, but the complements between the lines are not colored. The lines of the surface plot (surf) are black, and the complements of the lines are colored. The color of the complement surface of the surface graph and the line color of the grid graph are all along the zzThe z axis changes. A 3D surface drawn with plot3 is actually composed of 3D curves.
  • For example, we draw the intersection of two mutually perpendicular cylinders of equal diameter.
  • The procedure is as follows:
m=30;
z=1.2*(0:m)/m; 
r=ones(size(z));
theta=(0:m)/m*2*pi;
x1=r'*cos(theta);  %生成第一个圆柱体的坐标矩阵
y1=r'*sin(theta);
z1=z'*ones(1,m+1);
x=(-m:2:m)/m;
x2=x'*ones(1,m+1);  %生成第二个圆柱体的坐标矩阵
y2=r'*cos(theta);
z2=r'*sin(theta);
surf(x1,y1,z1);  %绘制垂直的圆柱体
axis equal;
axis off;
hold on;
surf(x2,y2,z2);  %绘制水平的圆柱体
axis equal;
axis off;
title('两个圆柱体的相交图形');
hold off;
  • The program running result is shown in the figure below.

insert image description here

  • For example, we analyze z = x 2 − 2 y 2 z=x^{2} -2y^{2}z=x22 y2 The shape of the curved surface and the planez = az=az=the intersection line of a .
  • The procedure is as follows:
[x,y]=meshgrid(-10:0.2:10);
z1=(x.^2-2*y.^2)+eps;  %第一个曲面坐标
a=input('a= ');
z2=a*ones(size(x));  %第二个曲面坐标
subplot(1,2,1);
mesh(x,y,z1);
hold on;
mesh(x,y,z2);  %分别画出两个曲面
v=[-10,10,-10,10,-100,100];  %第一子图的坐标设置
axis(v);
grid;
hold off;
r0=abs(z1-z2)<=1;  %求两曲面z坐标差小于1的点
xx=r0.*x;
yy=r0.*y;
zz=r0.*z2;  %求这些点上的x、y、z坐标,即交线坐标
subplot(1,2,2);
plot3(xx(r0~=0),yy(r0~=0),zz(r0~=0),'*');  %在第二子图画出交线
axis(v);  %第二子图的坐标设置
grid;
  • When the program is running, if we input a = − 25 a=-25a=25 , the resulting three-dimensional surface graph and the intersection line of the surface are shown in the figure below. When we enteraaWhen a is different, the intersection line of the surface will change.

insert image description here

  • In addition, there are two functions similar to meshthe function , that is, the 3D mesh surface function with contour meshclines and the 3D mesh surface function with base meshz. Its usage is meshsimilar to , the difference is that mesheit is still in xy xyDraw the surface on the xy plane at zzThe contour line in the z- axis directionmeshzis stillxy xyThe base of the drawn surface on the xy plane .
  • The function surfalso has two similar functions, Surface with Contours surfcand Surface with Lighting surfl.
  • For example, in xy xySelection area in the xy plane [ − 8 , 8 ] × [ − 8 , 8 ] [-8,8]×[-8,8][8,8]×[8,8 ] , we plot the function z = sin ⁡ x 2 + y 2 x 2 + y 2 z=\frac{\sin \sqrt{x^{2} +y^{2} } }{\sqrt{x^{ 2} +y^{2}}}z=x2+y2 sinx2+y2 4 3D surface plots (Mexican hat graphics) for .
  • The procedure is as follows:
[x,y]=meshgrid(-8:0.5:8);
z=sin(sqrt(x.^2+y.^2))./sqrt(x.^2+y.^2+eps);
subplot(2,2,1);
meshc(x,y,z) ;
title('meshc(x,y,z)');
subplot(2,2,2);
meshz(x,y,z);
title('meshz(x,y,z)');
subplot(2,2,3);
surfc(x,y,z);
title('surfc(x,y,z)');
subplot(2,2,4);
surfl(x,y,z);
title('surfl(x,y,z)');
  • The program running result is shown in the figure below.

insert image description here

3. Standard 3D surface

  • MATLAB provides some numbers for drawing standard three-dimensional surfaces, and these numbers can also be used to generate corresponding drawing data, which are often used in the demonstration of three-dimensional graphics. For example, spherethe function and cylinderfunction are used to draw a three-dimensional sphere and cylinder, respectively.
  • sphereThe function call format is as follows:
    [x,y,z]=sphere(n)
  • The function will produce ( n + 1 ) × ( n + 1 ) (n+1)×(n+1)(n+1)×(n+1 ) matrixx、y、zx、y、zx , y , z , using these three matrices can draw a unit sphere whose center is at the origin and radius is 1. If there is no output parameter when calling this function, the desired spherical surface will be drawn directly. nn determines the smoothness of the sphere, and its default value is 20. IfnnFor smaller values ​​of n , a polyhedral surface map will be drawn.
  • cylinderThe function call format is as follows:
    [x,y,z]=cylinder(R,n)
  • Among them, RRR is a vector, storing the radius at each equal interval height of the cylinder,nnn means that there are nnon the circumference of the cylindern interval points, there are 20 interval points by default. For example:
>> cylinder(3)
  • A cylinder will be generated. Another example:
>> cylinder([10,0])
  • A cone will be generated, and the following commands are executed:
>> t=0:pi/100:4*pi;
>> R=sin(t);
>> cylinder(R,30);
  • A sinusoidal cylinder will be generated. In addition, the size of the generating matrix is ​​related to RRThe length of the R vector andnnn related. The rest of the usage is the same as spherethe function
  • MATLAB also has a peaksfunction called the multimodal function, which is often used in the demonstration of three-dimensional surfaces. This function can be used to generate a plotting data matrix. The matrix elements are defined by the following function in the rectangular area [ − 3 , 3 ] × [ − 3 , 3 ] [-3,3]×[-3,3][3,3]×[3,3 ] The function value on the equally divided grid points is determined. f ( x , y ) = 3 ( 1 − x 2 ) e − x 2 − ( y + 1 ) 2 − 10 ( x 5 − x 3 − y 5 ) e − x 2 − y 2 − 1 3 e − ( x + 1 ) 2 − y 2 f(x,y)=3(1-x^{2})e^{-x^{2}-(y+1)^{2}}-10(\frac {x}{5}-x^{3}-y^{5})e^{-x^{2}-y^{2}}-\frac{1}{3}e^{-(x +1)^{2}-y^{2}}f(x,y)=3(1x2)ex2(y+1)210(5xx3y5)ex2y231e(x+1)2y2
  • For example:
    z=peaks(30);
  • will generate a 30x30 30x3030×Matrixzz of 30z , that is, alongthe xxxyyIn the y direction, the interval[ − 3 , 3 ] [-3,3][3,3 ] into 29 equal parts, and calculate the function value on these grid points. The default equal fraction is 48, ie p=peaks will generate a49 × 49 49 × 4949×Matrixpp of 49p . It can also be based on the grid coordinate matrixx, yx, yx , y recalculates the function matrix. For example:
>> [x,y]=meshgrid(-5:0.1:5);
>> z=peaks(x,y);
  • The generated numerical matrix can be used as a parameter of functions such as mesh, surfetc. to draw a multi-peak function surface graph. In addition, if there is no output parameter when calling peaksthe function , the multi-peak function surface graph will be drawn directly.
  • For example, we draw standard 3D surface graphics.
  • The procedure is as follows:
t=0:pi/20:2*pi;
[x,y,z]=cylinder(2+sin(t),30);
subplot(1,3,1);
surf(x,y,z);  %生成一个正弦型柱面
axis([-5,5,-5,5,0,1]);
[x,y,z]=sphere;
subplot(1,3,2);
surf(x,y,z);  %生成一个球面
axis equal;
[x,y,z]=peaks(30);
subplot(1,3,3);
meshz(x,y,z);  %生成一个多峰曲面
axis([-5,5,-5,5,-10,10]);
  • The program running result is shown in the figure below.

insert image description here

3. Other three-dimensional graphics

  • When introducing two-dimensional graphics, various special graphics have been mentioned, some of which can also appear in three-dimensional form, and the functions used include bar3, bar3h, pie3, fill3, scatter3, stem3and quiver3.

1. 3D Bar Chart

  • bar3The function draws a three-dimensional bar chart, and the common format is as follows:
    bar3 (y)
    bar3(x,y)
  • In the first format, yyEach element of y corresponds to a bar. The second format is inxxDraw yyat the position specified by xA bar plot of the elements in y .
  • bar3his used in bar3the same way as .

2. 3D pie chart

  • pie3The function draws a three-dimensional pie chart, and the common format is as follows:
    pie3(x,explode)
  • where xxx is a vector, usexxThe data in x draws a three-dimensional pie chart, and explode sets whether the corresponding sector deviates from the overall graph.

3. Three-dimensional solid map

  • fill3The function can draw a filled polygon in a three-dimensional pie chart, and the common format is as follows:
    fill3(x,y,z,c)
  • where x , y , zx, y, z are usedx , y , z are the vertices of the polygon, andccc specifies the fill color.

4. Three-dimensional scatterplot

  • scatter3The function can draw a scatter plot in three-dimensional space, and the common format is as follows:
    scatter3(x,y,z,c)
  • Among them , x , y , zx, y, zx , y , z must be vectors of equal length, andccc specifies the fill color.

5. 3D Bar Diagram

  • stem3The function draws a three-dimensional bar graph of discrete sequence data, and the common format is as follows:
    stem3(z)
    stem3(x,y,z)
  • The first type will be the data sequence zzz is expressed as fromxy xyA pole diagram extending upwards in the x y plane, xxxyyy is automatically generated. The second format is inxxxyyDraw the data series zzat the position specified by yThe rod diagram of z .

6. 3D Arrow Chart

  • quiver3The function draws a vector diagram in a three-dimensional space, and the commonly used formats are as follows:
    quiver3(x,y,z,u,v,w)
  • Among them , x , y , z , u , v , wx, y, z, u, v, wx , y , z , u , v , w must have the same length to draw a three-dimensional vector diagram. Vector by( u , v , w ) (u,v,w)(u,v,w ) , the location is determined by( x , y , z ) (x,y,z)(x,y,z ) decide. For example, quiver3(1,2,3,4,5,6) draws a vector starting from (1,2,3), that is, a vector pointing from (1,2,3) to (4,5,6) arrow.
  • For example, we draw the following three-dimensional graphics.
  • (1) Draw a three-dimensional bar graph of the magic square.
  • (2) Known x = [ 2347 , 1827 , 2043 , 3025 ] x=[2347,1827,2043,3025]x=[2347,1827,2043,3025 ] , Draw a three-dimensional pie chart.
  • (3) Draw 5 yellow triangles with random vertex coordinates.
  • (4) Draw the curve y = sin ⁡ xy=\sin x in the form of three-position bar diagramy=sinx
  • The overall procedure is as follows:
subplot(2,2,1);
bar3(magic(4));
title('(1)bar3');
subplot(2,2,2);
pie3([2347,1827,2043,3025]);
title('(2)pie3');
a=rand(3,5);
b=rand(3,5);
c=rand(3,5);
subplot(2,2,3);
fill3(a,b,c,'y');
title('(3)fill3');
y=2*sin(0:pi/10:2*pi);
subplot(2,2,4);
stem3(y);
title('(4)stem3');
  • The overall program running result is shown in the figure below.

insert image description here

  • In addition to the three-dimensional graphics discussed above, commonly used graphics include waterfall diagrams and contour diagrams of three-dimensional surfaces.
  • watrallThe function is used to draw the waterfall chart . Its usage and graphic effect meshzare similar to the function, except that its grid lines are at xxAppears in the x- axis direction, with a waterfall effect. The contour map is divided into two-dimensional and three-dimensional forms, which contourarecontour3drawn by the functions and respectively.
  • For example, we plot waterfall and contour plots of multimodal functions.
  • The procedure is as follows:
subplot(1,2,1);
[X,Y,Z]=peaks(30);
waterfall(X,Y,Z)
xlabel('X-axis');
ylabel('Y-axis');
zlabel('Z-axis');
subplot(1,2,2);
contour3(X,Y,Z,12,'k');  %其中12代表高度的等级数
xlabel('X-axis');
ylabel('Y-axis');
zlabel('Z-axis');
  • The program running result is shown in the figure below.

insert image description here

Guess you like

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