matlab drawing (3) draw three-dimensional image

Table of contents

1. Draw a 3D curve

 2. Drawing three-dimensional surfaces

1. meshgrid function

 2.mesh and surf functions


1. Draw a 3D curve

1. The most basic function for drawing three-dimensional curves —plot3

plot3(x1, y1, z1, option 1, x2, y2, z2, option 2, ..., xn, yn, zn , option n)
Among them, each set of x , y , z constitutes a set of coordinate parameters of the curve, and the definition of the options is the same as that of the plot function. When x , y , z are vectors of the same dimension, then the corresponding elements of x , y , z form a three-dimensional curve; when x , y , z are matrices of the same dimension, then the three-dimensional curve is drawn with the corresponding column elements of x , y , z , the number of curve bars is equal to the number of matrix columns. (For detailed usage of plot, please refer to my blog: matlab drawing (1)

Example 1. Draw a 3D curve

 code show as below:

t=0:pi/10:10*pi;%取点
x=sin(t)+t.*cos(t);
y=cos(t)-t.*sin(t);
z=t;%计算x,y,z坐标
plot3(x,y,z,'Color','#4F4F2F','Linewidth',1.5);
axis([-30 30 -30 30 0 35])%设置坐标范围
title('Line in 3-D Space');%标题
xlabel('X');ylabel('Y');zlabel('Z');%x,y,z轴标签
grid on;%开启网格线

Draw the graph as follows:

 2. Easy to draw 3D curves

For the three-dimensional curve expressed in parametric form, it can also be drawn with the simple drawing function ezplot3 .

Call format: ezplot3(x,y,z,[a,b])

Plots the three-dimensional curve determined by the parametric equations xx( ) , = y( t, and = z( tover the interval a < b . When no interval is specified, the graph is drawn on the interval 0 < t < 2 pi by default .    

Example 2. Draw a 3D curve

syms t
x=sin(t)+t.*cos(t);
y=cos(t)-t.*sin(t);
z=t;%计算x,y,z坐标
ezplot3(x,y,z,[0,10*pi])

operation result;

 2. Drawing three-dimensional surfaces

The basic method of drawing a three-dimensional surface in Matlab : (according to a certain rule) take mxn grid points Qij on the surface , and set their x , y , z coordinates to form a matrix X , Y , Z respectively , then use mesh(X, Y, Z) or surf(X, Y, Z) can draw the surface.

1. meshgrid function

For the functional surface, that is, the surface represented by z=f( x,y ) , a<x<b, c<y<d , usually, first take n points in the interval [ a , b ] of the x- axis , and set The x- coordinates of these points form a vector x; then m points are taken in the interval [ c , ] of the y- axis , and the y- coordinates of these points form a vector y; a straight line parallel to the two coordinate axes is drawn from each division point, Then mxn grid points P ij in the two-dimensional rectangular area a<x<b, c<y<d are obtained . The x coordinates of these mxn grid points form an mxn matrix X , and their The y coordinates constitute the matrix Y of mxn , that is, the coordinates of the ( i,j ) th grid point P ij are (X( i,j ),Y( i,j )) . How to get matrix X, Y from vector x, y . Matlab specially provides a function meshgrid , its function is to generate the coordinate matrix X, Y of the two-dimensional grid point P ij from the division point of the x, y axis (vector x, y ) .
Format : [X,Y]= meshgrid ( x,y ); when y=x , it can be simplified to [X,Y]= meshgrid (x) .
Map the mxn grid points P ij in the two-dimensional rectangular area a<x<b, c<y<d to the surface through z=f( x, y ) , and then get the mxn points Q ij on the surface , The coordinates of the ( i,j ) th point Q ij are (X( i,j ),Y( i,j ), Z( i,j )), the x,y coordinates of Q ij are the same as those of P ij , so , the matrix composed of the x and y coordinates of Q ij is the X, Y. The matrix Z composed of the z coordinates generated by the meshgrid function can be calculated according to Z( i, j   )=f(X( i,j ),Y( i,j )) is obtained by point operation. After generating the x, y, z coordinate matrix X , Y , Z of mxn grid points Qij on the surface , use functions such as mesh(X,Y,Z) or surf(X,Y,Z) to convert this Draw the surface.

 2.mesh and surf functions

The mesh function is used to draw a three-dimensional grid map, surf is used to draw a three-dimensional surface map, and the complementary surfaces between the lines are filled with colors. The usage format of mesh is introduced below (the usage format of surf and mesh is the same).

mesh(X,Y,Z,C) : Among them, X,Y,Z are the mxn matrix composed of the x coordinates, y coordinates and z coordinates of mxn points on the surface , namely: (X( i,j ) ,Y( i,j ),Z( i,j )) is the coordinate of the ( i,j ) th grid point Pij , and C(i,j) is the color value of Pij.

mesh(X,Y,Z) : use C=Z (color value proportional to height value)

mesh( x,y,Z ) or mesh( x,y,Z,C ): use two vectors instead of two matrices, require: length(x)=n, length(y)=m , and [ m,n ]=size(Z) , the coordinates of the ( i,j ) th grid point Pij are (x(j),y( i ),Z( i,j ))

mesh(Z): Equivalent to mesh(1:n,1:m,Z).

mesh(Z,C): equivalent to mesh(1:n,1:m,Z,C).

Example 3. Draw the graph of the function z=x*exp(-x^2-y^2) in [-2,2]x[-3,3] . The following two pieces of code have exactly the same effect.

%% figure1
figure(1)
x=-2:0.1:2;   y=-3:0.1:3; 
[X,Y]=meshgrid(x,y);
Z=X.*exp(-X.^2-Y.^2);
mesh(X,Y,Z);
title('figure 1')
colorbar
%% figure2
figure(2)
x=-2:0.1:2;m=size(x,2);
y=-3:0.1:3; n=size(y,2);
Z=zeros(n,m);
for i=1:m
    for j=1:n
        Z(j,i)=x(i)*exp(-x(i)^2-y(j)^2);
    end
end
mesh(x,y,Z);
colorbar
title('figure 2')

 The result display:

 Surface map drawing (just change mesh to surf), these ten pictures reflect different color mapping styles, and the titles are the corresponding colormap values.

code show as below:

%% figure 1
figure(1)
x=-2:0.1:2;   y=-3:0.1:3; 
[X,Y]=meshgrid(x,y);
Z=X.*exp(-X.^2-Y.^2);
surf(X,Y,Z);
title('hsv')
colormap('hsv')
shading flat
%% figure 2
figure(2)
x=-2:0.1:2;   y=-3:0.1:3; 
[X,Y]=meshgrid(x,y);
Z=X.*exp(-X.^2-Y.^2);
surf(X,Y,Z);
title('jet')
colormap('jet')
shading interp
%% figure 3
figure(3)
x=-2:0.1:2;   y=-3:0.1:3; 
[X,Y]=meshgrid(x,y);
Z=X.*exp(-X.^2-Y.^2);
surf(X,Y,Z);
title('copper')
colormap('copper')
shading interp
%% figure 4
figure(4)
x=-2:0.1:2;   y=-3:0.1:3; 
[X,Y]=meshgrid(x,y);
Z=X.*exp(-X.^2-Y.^2);
surf(X,Y,Z);
title('bone')
colormap('bone')
shading interp
%% figure 5
figure(5)
x=-2:0.1:2;   y=-3:0.1:3; 
[X,Y]=meshgrid(x,y);
Z=X.*exp(-X.^2-Y.^2);
surf(X,Y,Z);
title('flag')
colormap('flag')
shading interp
%% figure 6
figure(6)
x=-2:0.1:2;   y=-3:0.1:3; 
[X,Y]=meshgrid(x,y);
Z=X.*exp(-X.^2-Y.^2);
surf(X,Y,Z);
title('pink')
colormap('pink')
shading interp
%% figure 7
figure(7)
x=-2:0.1:2;   y=-3:0.1:3; 
[X,Y]=meshgrid(x,y);
Z=X.*exp(-X.^2-Y.^2);
surf(X,Y,Z);
title('spirng')
colormap('spring')
shading interp

%% figure 8
figure(8)
x=-2:0.1:2;   y=-3:0.1:3; 
[X,Y]=meshgrid(x,y);
Z=X.*exp(-X.^2-Y.^2);
surf(X,Y,Z);
title('summer')
colormap('summer')
shading interp

%% figure 9
figure(9)
x=-2:0.1:2;   y=-3:0.1:3; 
[X,Y]=meshgrid(x,y);
Z=X.*exp(-X.^2-Y.^2);
surf(X,Y,Z);
title('autumn')
colormap('autumn')
shading interp

%% figure 10
figure(10)
x=-2:0.1:2;   y=-3:0.1:3; 
[X,Y]=meshgrid(x,y);
Z=X.*exp(-X.^2-Y.^2);
surf(X,Y,Z);
title('winter')
colormap('winter')
shading interp

Image result display:

 

 

 

 

 Example 4. Draw a three-dimensional surface graph = sin x ^2 + cos y^ 2 , x : [0 , pi ] , y: [0 , pi /2] .

code:

[x,y]=meshgrid(0:pi/100:pi, 0:pi/100:pi/2);
z=sin(x.^2)+cos(y.^2);
surf(x,y,z);
axis([0 4 0 1.8 -1.5 1.5]);
colormap('prism')
shading interp
colorbar

 operation result:

So much to say in this section, and I will continue to share in the next issue!

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/higerwy/article/details/129435292