当前位置:网站首页>[matlab] 3D curve and 3D surface
[matlab] 3D curve and 3D surface
2022-06-12 23:55:00 【No card, no change】
3D curve and 3D surface
Three dimensional curve
plot3 function
plot3(x, y, z):
- x、y、z When it is an isomorphic vector , At every location (x, y, z) The coordinates that make up a point .
- x、y、z When the matrix is isomorphic , Each column corresponds to a curve , therefore ,x( or y or z) The number of columns is the number of curves drawn .
- x、y、z When there are matrices and vectors in , If the vector is a row vector , It should be the same as the number of columns of the matrix ; If the vector is a column vector , It should be the same as the number of rows of the matrix .
plot3(x1, y1, z1, Options , x2, y2, z2, Options , ……): Draw multiple curves at the same time .
t=0:0.01:2*pi;
t=t';
x=[t t t t t];
y=[sin(t) sin(t)+1 sin(t)+2 sin(t)+3 sin(t)+4];
z=[t t t t t];
subplot(121)
plot3(x, y, z)
grid on
t=0:0.01:2*pi;
x=t;
y=[sin(t); sin(t)+1; sin(t)+2; sin(t)+3; sin(t)+4];
z=t;
subplot(122)
plot3 (x, y, z)
grid on
fplot3 function
fplot3(funx, funy, funz, tlims, Options ):funx、funy、funz Represents the defining curve x、y、z A function of coordinates , Usually in the form of a function handle .tlims Is the value range of the argument of the parameter function , Use binary vectors [tmin,tmax] describe , The default is [-5,5]. Used to draw parametric equation curve .
xt = @(t) exp(-t/ 10).*sin(5*t);
yt = @(t) exp(-t/ 10).*cos(5*t);
zt = @(t) t;
fplot3(xt, yt, zt, [-12 12], 'r-.')
Three dimensional surface
General steps :
- Generation of plane grid data : In general use meshgrid function .
- Select appropriate function to draw 3D surface .
Generation of plane grid data
First, understand what grid data is ?
The figure below , The lower left corner is (2, 3), The upper right corner is (6, 8) The rectangular area of , contain 30 A little bit , Put these points x、y The coordinates are assigned to two matrices according to the relationship between relative positions X、Y in , You can get 6 × 5 6×5 6×5 Matrix X, Store... For each point x coordinate , and 6 × 5 6×5 6×5 Matrix Y, Store... For each point y coordinate .
To get the plane mesh data is equivalent to getting the lower left corner is (2, 3), The upper right corner is (6, 8) The coordinates of all the points in the rectangular area of , For each point, we know the corresponding z coordinate , obtain (x, y, z) Three dimensional coordinates , It can be marked in the coordinate system , When enough points of coordinates are known, the surface can be drawn . This is the principle of drawing all-round functions .
For the image above , Its X The matrix of the :
X =
2 3 4 5 6
2 3 4 5 6
2 3 4 5 6
2 3 4 5 6
2 3 4 5 6
2 3 4 5 6
Its Y The matrix of the :
Y =
3 3 3 3 3
4 4 4 4 4
5 5 5 5 5
6 6 6 6 6
7 7 7 7 7
8 8 8 8 8
Generate... Manually
x= 2:6;
y = (3:8)';
X = ones(size(y))*x
Y = y*ones(size(x))
The same as above .
meshgrid function
[X Y] = meshgrid(x, y):x、y Vector , They correspond to each other x Coordinates and y coordinate , After the test, the row vector and column vector are all OK .
[X Y] = meshgrid(x): At this time, it is equivalent to x=y.
[X Y] = meshgrid(2:6, 3:8)
The same as above .
Drawing 3D surfaces
mesh function
mesh(x, y, z, c):x、y For flat mesh data , That's what we got from above X and Y,z Is the height of the corresponding point , It's also mentioned above ,x、y、z Homomorphic matrix ,c Used to specify the color of the surface at different heights , Generally omit , When omitted c=z.
mesh(z, c): When x and y When omitted ,z The second subscript of a matrix is taken as x Axis coordinates ,z The first dimensional subscript of a matrix is taken as y Axis coordinates .
Be careful : And mesh Functions with similar usage , If grid data is required as a function of parameters , When the grid data corresponds to z When the coordinate information is known , That is, the title has been given , be x、y The parameter can be x Coordinates and y Vector of coordinates , No need to use meshgrid Function to generate grid data ! The reason for generating grid data , Because most of the topics are xy Corresponding z The coordinates are unknown , Therefore, it is necessary to generate grid data and solve the corresponding equation z Coordinate before drawing and other operations .
t = 1:5;
z=[0.5*t; 2*t; 3*t];mesh(z);
surf function
surf(x, y, z, c): ditto .
surf(z, c): ditto .
mesh Function and surf Difference of function
mesh Function is used to draw a 3D surface mesh that is not particularly fine , Lines of the same layer are represented by the same color .
surf Function is used to draw a smooth three-dimensional surface mesh , The patch between the lines is filled with color .
t = -2:0.2:2;
[X,Y] = meshgrid(t);
Z= X .* exp(-X.^2- Y.^2);
subplot(131)
mesh(X,Y,Z);
subplot(132)
surf(X,Y,Z);
subplot(133)
plot3(X,Y,Z);
grid on % Write it under the drawn image to take effect
plot3 The call form of the function at this time is that the three parameters are isomorphic matrices , Then each column represents a curve .
fsurf function
fsurf(funx, funy, funz, uvlims):funx、funy、funz Represents the defining surface x、y、z A function of coordinates , Usually in the form of a function handle .uvlims by funx、funy and funz The value range of the argument of , use 4 Metavector [umin, umax, vmin, vmax] describe ﹐ The default is [-5,5,-5,5]. It is generally used in parametric equation, which contains two parameters .
fmesh function
fmesh(funx, funy, funz, uvlims): The meaning of the parameter is the same as above . It is generally used in parametric equation, which contains two parameters .
funx = @(u, v) u.*sin(v);
funy = @(u, v) -u.*cos(v);
funz = @(u, v) v;
fsurf(funx, funy, funz,[-5,5,-5,-2])
hold on
fmesh(funx, funy, funz,[-5,5,-2,2])
hold off
Special function for drawing 3D surface
meshc function
Three dimensional mesh surface functions with contours .
Use the same mesh.
meshz function
Three dimensional mesh surface function with base .
Use the same mesh.
surfc function
Surface functions with contours .
Use the same mesh.
surfl function
Surface function with illumination effect .
Use the same mesh.
[x,y]=meshgrid(0:0.1:2, 1:0.1:3);
z=(x-1).^2+(y-2).^2-1;
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)')
sphere function
[x y z] = sphere(n): Draw the unit ball ,n How many polygon planes should the ball be surrounded by ,n The bigger, the smoother , Return plane mesh data x and y, And their corresponding heights , Got x、y、z Can be called directly mesh Class function or surf Class function to draw a ball .
!!! Draw any ball center , A ball of arbitrary radius :
%% Draw ball :(x-10)²+(y-5)²+(z-3)²=10²
a = 10; b = 5; c = 3; r = 10; % User defined or entered spherical coordinates and radius
[x y z] = sphere(30);
X = x*r + a;
Y = y*r + b;
Z = z*r + c;
surf(X, Y, Z)
title('(x-10)²+(y-5)²+(z-3)²=10² ')
grid on
cylinder function
[x,y,z]=cylinder: Function returns a radius and height of 1 The cylinder of x,y,z Coordinate value of the axis , The cylinder has... Along its circumference 20 Equidistant points
[x,y,z]=cylinder: The function has a radius of r、 The height is 1 Of a cylinder x,y,z Coordinate value of the axis , The cylinder has... Along its circumference 20 Equidistant points
[x,y,z]=cylinder(r,n): The function has a radius of r、 The height is 1 Of a cylinder x,y,z Coordinate value of the axis , The cylinder has... Along its circumference n Equidistant points
Generated at this time x、y、z Is a two row matrix .x The first line of is equal to the second line , Which respectively represent the points on the circle x coordinate ;y Empathy x;z The first line of is the bottom surface z coordinate ,z The second line of is the top and bottom surface z coordinate , namely 0 and 1.
Essentially , The cylinder is drawn from the scattered points on two circles .
!!! Draw the center of the bottom circle at any , A cylinder of arbitrary height :
a = -1; b = 2; up = 5; down = -2; % Center of bottom surface (a, b), The height is [down, up]
[x y z] = cylinder(2, 30);
surf(x+a, y+b, repmat([-2;5], 1, length(x))) % Guarantee z And x Same type , And each column is -2 5
grid on
subplot(1,3,1);
[x y z]=cylinder; % Cylinder
surf(x, y, z);
subplot(1,3,2);
t=linspace(0,2*pi,40);
[x y z]=cylinder(2+cos(t),30); % vase
surf(x, y, z);
subplot(1,3,3);
[x y z]=cylinder(0:0.2:2,30); % Cone
surf(x, y, z);
[x, y, z]= cylinder(1, 60);
z=[-1*z(2,:); z(2,:)]; % high [-1, 1]
surf(x, y, z);
hold on
surf(y, z, x);
xlabel('x')
ylabel('y')
zlabel('z')
axis equal
Graphic decoration processing
Viewpoint processing
viewpoint , That is to see the position of the object , Look at the position of the object , The appearance of objects is different ; The viewpoint position can be represented by azimuth and elevation .
Azimuth is also called rotation angle , The line between the viewpoint and the origin is xoy The projection of a face is related to y The included angle in the negative direction of the axis is the azimuth angle . A positive value means counterclockwise , A negative value means clockwise .
Elevation angle is also called angle of view , The line between the viewpoint and the origin xoy The included angle of the face . A positive value indicates that in xoy Above face , A negative value means that in xoy Below face .
view function
view(az, el):az Is the azimuth angle ,el For elevation , All units are degrees ; The default azimuth is -37.5°, The elevation is 30°.
view(x, y, z): Directly set the viewpoint position to... In the rectangular coordinate system (x, y, z)
view(2): Observe the two-dimensional pattern of the object , Top view , Equivalent to view(0, 90)
view(3): Observe the three-dimensional pattern of the object , Default , Equivalent to view(-37.5, 30)
[x, y]=meshgrid(0:0.1:2, 1:0.1:3);
z=(x- 1).^2+(y-2).^2-1;
subplot(2,2,1);
mesh(x, y, z) % Equivalent to view(3)
title(' azimuth =-37.5{\circ}, Elevation =30{\circ}')
subplot(2,2,2);
mesh(x, y,z)
view(0,90); % Equivalent to view(2)
title(' azimuth =0{\circ}, Elevation =90{\circ}')
subplot(2,2,3);
mesh(x, y, z)
view(90,0);
title(' azimuth =90{circ}, Elevation =0{\circ}')
subplot(2,2,4);
mesh(x, y,z)
view(-45,-60);
title(' azimuth =-45{\circ} , Elevation =-60{\circ}')
Color treatment
Chromatic graph matrix is a matrix with several rows and three columns , Each element range [0, 1],0 Indicates that the brightness is 0,1 Indicates maximum brightness , The three columns represent RGB Color brightness , The change in the brightness of each line of color .
Built in color map ( Own color chart ):
colormap function
colormap cmapname:cmapname The name of the built-in color map , Set image as color map .
colormap(cmap):cmap Is a chromatic graph matrix , Set the image as the color map represented by this color map matrix .
surf(peaks)
colormap hot
% colormap(hot(64)) % The same thing as above , The built-in color map is 64 Matrix of rows ,hot(n) To generate 64 Row heat chromatic graph matrix , The generation methods of other chromatic graph matrices are the same .
Custom color map matrix
c = 0:0.2:1;
cmap = [c' c' c'];
surf(peaks)
colormap(cmap) % Equivalent to colormap(gray(6))
%% One of my favorite colors
mycolorpoint=[[0 0 16];
[8 69 99];
[57 174 156];
[198 243 99];
[222 251 123];
[239 255 190]];
mycolorposition=[1 11 33 50 57 64];
mycolormap_r=interp1(mycolorposition,mycolorpoint(:,1),1:64,'linear','extrap');
mycolormap_g=interp1(mycolorposition,mycolorpoint(:,2),1:64,'linear','extrap');
mycolormap_b=interp1(mycolorposition,mycolorpoint(:,3),1:64,'linear','extrap');
mycolor=[mycolormap_r',mycolormap_g',mycolormap_b']/255;
mycolor=round(mycolor*10^4)/10^4;% Retain 4 Decimal place
peaks(20);
colormap(mycolor)
[ Colors come from ]( utilize matlab Build your own colormap( Color matching )_hyhhyh21 The blog of -CSDN Blog _colormap hsv)
Shading of 3D graphics surface
shading faceted: Color each mesh slice with the color corresponding to its height , The gridlines are black .
shading flat: Shade each mesh slice with the same color , And the grid lines also use the corresponding color .
shading interp: Color interpolation is used in the mesh .
[x, y, z]= cylinder(pi:-pi/5:0, 10);
colormap(lines);
subplot(1,3,1);
surf(x, y, z);
shading flat
subplot(1,3,2);
surf(x, y, z);
shading interp
subplot(1,3,3);
surf (x, y, z);
% By default shading faceted
Graphics clipping
Set the function value corresponding to the part to be cropped in the drawing to NaN, In this way, when drawing graphics , Function value is NaN The part of will not be displayed , So as to achieve the purpose of cutting the graphics .
t = linspace(0,2*pi,100);
x= sin(t);
y = cos(t);
p = y > 0.5;
y(p)= NaN; % The logic matrix is divided into 1 The corresponding position element of is set to NaN, That is, it is set to nonexistent , but y Its length is still 100
plot(x, y)
axis([-1.1, 1.1, -1.1, 1.1])
axis square
grid on
[X,Y,Z] = sphere(60);
p = Z>0.5;
Z(p)= NaN;
surf(X,Y,Z)
axis([-1,1,-1,1,-1,1])
axis equal
view(-45,20)
Be careful :A(B): among A For matrix ,B Is a logical matrix , It is equivalent to obtaining B In the logic matrix is 1 The position of is A The element of the corresponding position in the matrix .
边栏推荐
- SAP UI5 如何通过 manifest.json 文件定义第三方库依赖关系
- What occupation is suitable for PMP?
- Leetcode 2164. 对奇偶下标分别排序(可以,一次过)
- Day 3 of jsbom and DOM learning
- 2022-06-13日报: 图灵奖得主:想要在学术生涯中获得成功,需要注意哪些问题?
- 3、 Storage system
- 【Matlab】三维曲线与三维曲面
- 如何利用华为云容灾解决方案替代灾备一体机
- 【HCIE论述】组播IGMP-A
- Introduction to business rules service on SAP Business Technology Platform (BTP)
猜你喜欢
Running of NCF dapr application instance
Learn to divide subnets in an article
M_8:设计消息队列存储消息数据的 MySQL 表格
leaflet如何加载10万条数据
How to load 100000 pieces of data in leaflet
MySql索引
2022 electrician (elementary) operation certificate examination question bank and online simulation examination
PLC peut également faire des jeux - - codesys écrit des jeux de devinettes numériques
dict和set的基本操作
SAP QM qp03 displays an inspection plan with multiple specs inspection features
随机推荐
21 Chundong University blasting safety online peacetime operation 123 [standard answer]
leaflet如何优雅的展示重叠点位的气泡窗口
Test platform series (97) perfect the case part
Real time preview of PHP in browser by vscade
[matlab] basic operation
Software development tools [3] theoretical basis of software development tools
【Matlab】矩阵
Divicon est toujours utilisé dans le leaflet de l'ère H5?
数组
Day 3 of jsbom and DOM learning
Buuctf-[ciscn 2019 preliminary]love math
Novice must see! How rust beginners write gear smart contracts (1)
Initial experience of Huawei cloud Conference [Huawei cloud to jianzhiyuan]
同花顺开证券账户怎么样?到底安不安全呢
[hcie discussion] multicast igmp-a
Actual combat | UI automation test framework design and pageobject transformation
2022年危险化学品经营单位安全管理人员考试试题及在线模拟考试
PLC peut également faire des jeux - - codesys écrit des jeux de devinettes numériques
How does idea switch the interface to Chinese
How to gracefully solve the offset problem of Baidu and Gaode maps in leaflet