当前位置:网站首页>[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 .
边栏推荐
- 一篇文章学会子网划分
- 2022年3月11日记:王老师的春天,奇异的模板模式
- Start of u-boot_ Armboot analysis (I)
- SAP 业务技术平台(BTP) Workflow(工作流)功能介绍
- [leetcode] understanding and usage of map[key]+
- What occupation is suitable for PMP?
- [hcie discussion] STP-A
- Leaflet that supports canvas Path. Dashflow dynamic flow direction line
- 最全预告!华为云精彩议程火速收藏
- scala中的隐式转换和隐式参数讲解与实践
猜你喜欢
Day 3 of jsbom and DOM learning
How leaflet gracefully displays the bubble window of overlapping points
Software development tools [3] theoretical basis of software development tools
Is divicon still used in leaflet in H5 era?
How does idea switch the interface to Chinese
Is the revised PMP worth testing?
Running of NCF dapr application instance
dict和set的基本操作
Matlab [path planning] - UAV drug distribution route optimization
[hcie discussion] multicast igmp-a
随机推荐
[matlab] two dimensional curve
Will the salary increase after obtaining PMP certification?
PLC peut également faire des jeux - - codesys écrit des jeux de devinettes numériques
Summary of individual NLP internship experience
Matlab【路径规划】—— 无人机药品配送路线最优化
[hcie discussion] STP-A
[SciPy optimization tutorial] v. quick solution of univariate function optimization
线上真实排队系统重构案例分享——实战篇
Explanation and practice of implicit transformation and implicit parameters in Scala
2022 operation of simulated examination platform for hoisting machinery command certificate
Novice must see! How rust beginners write gear smart contracts (1)
你真的会用PostGIS中的buffer缓冲吗?
SAP UI5 如何通过 manifest.json 文件定义第三方库依赖关系
How to publish OSM maps locally and customize the mapping
KConfig
How to get Matplotlib figure size
How SAP ui5 uses manifest JSON file defines third-party library dependencies
scala中的隐式转换和隐式参数讲解与实践
Examination questions and online simulation examination for safety management personnel of hazardous chemical business units in 2022
TCP与UDP