当前位置:网站首页>[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
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-TJle48yE-1655004061083)(C:\Users\23343\AppData\Roaming\Typora\typora-user-images\image-20210828150449565.png)]](/img/f4/c1750e6333b345c53697f6b59d5e79.png)
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-.')
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-hYSDnHxT-1655004061085)(C:\Users\23343\AppData\Roaming\Typora\typora-user-images\image-20210828151013225.png)]](/img/b6/eb02181c99c4374f01749fdc2b3b9a.png)
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 .
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-buNC96mn-1655004061085)(C:\Users\23343\AppData\Roaming\Typora\typora-user-images\image-20210828155724626.png)]](/img/6f/ce84c31591bd279c6c00051ff285ba.png)
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);
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-OEg3xpxw-1655004061086)(C:\Users\23343\AppData\Roaming\Typora\typora-user-images\image-20210828162302099.png)]](/img/9d/190f0c039f35d1f895068b95ef7339.png)
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
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-AC7teLm4-1655004061087)(C:\Users\23343\AppData\Roaming\Typora\typora-user-images\image-20210828161839896.png)]](/img/8a/dae9d85a5709ff6efb47dee38eafb3.png)
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
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-zuwPPykY-1655004061087)(C:\Users\23343\AppData\Roaming\Typora\typora-user-images\image-20210828185130765.png)]](/img/5e/8ccafa1c9c12364d79311fb96a699d.png)
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)')
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-MyDww8nL-1655004061088)(C:\Users\23343\AppData\Roaming\Typora\typora-user-images\image-20210828162540861.png)]](/img/e5/79e3b4c06a68d50820ffcbcb89f6c6.png)
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
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-uuVGbLr1-1655004061088)(C:\Users\23343\AppData\Roaming\Typora\typora-user-images\image-20210828164606737.png)]](/img/ec/fec285210def03760dea83cc47a628.png)
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
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-jtxyhGYr-1655004061089)(C:\Users\23343\AppData\Roaming\Typora\typora-user-images\image-20210828165940540.png)]](/img/8d/8de548d0d6dc4dfe0c825c8f926229.png)
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);
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-eEjpioDT-1655004061089)(C:\Users\23343\AppData\Roaming\Typora\typora-user-images\image-20210828170126408.png)]](/img/1d/fbeaa6d06cf876dc1f16fd0625266d.png)
[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
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-F1g8QDix-1655004061090)(C:\Users\23343\AppData\Roaming\Typora\typora-user-images\image-20210828172323168.png)]](/img/6f/b95669cbb86bc70c09463cd28e1e87.png)
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 .
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-jswH4HUi-1655004061090)(C:\Users\23343\AppData\Roaming\Typora\typora-user-images\image-20210830143401617.png)]](/img/57/d182860e08b793a92aace1f8f6aad4.png)
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 .
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-ZfzTztrk-1655004061090)(C:\Users\23343\AppData\Roaming\Typora\typora-user-images\image-20210830143348104.png)]](/img/76/a9a914a51d00094f5c94f02d683e1e.png)
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}')
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-eDgfICuZ-1655004061091)(C:\Users\23343\AppData\Roaming\Typora\typora-user-images\image-20210830144424293.png)]](/img/ed/ac139a0135a4520a4d5018f0b7c1d0.png)
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 ):
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-EeAJ3hEW-1655004061091)(C:\Users\23343\AppData\Roaming\Typora\typora-user-images\image-20210830145842507.png)]](/img/74/20e35757968378e6b50160672f47a4.png)
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 .
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-IbIsrqOI-1655004061091)(C:\Users\23343\AppData\Roaming\Typora\typora-user-images\image-20210830150411440.png)]](/img/aa/07a932d3de48b64e48271d3da0f590.png)
Custom color map matrix
c = 0:0.2:1;
cmap = [c' c' c'];
surf(peaks)
colormap(cmap) % Equivalent to colormap(gray(6))
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-9RA7hSMa-1655004061092)(C:\Users\23343\AppData\Roaming\Typora\typora-user-images\image-20210830150805398.png)]](/img/50/44fec1cae6e2bbab1d9e000578f281.png)
%% 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
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-7g2geshI-1655004061092)(C:\Users\23343\AppData\Roaming\Typora\typora-user-images\image-20210830151231780.png)]](/img/14/1fd6aa09dd515f7017ca81eb6da757.png)
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 .
边栏推荐
- The PMP examination time in March 2022 is set -- "March 27"
- 一篇文章学会子网划分
- 2022 questions d'examen pour le personnel de gestion de la sécurité de l'unit é de gestion des produits chimiques dangereux et examen de simulation en ligne
- Start blogging
- Accelerating with Dali modules
- 2022 electrician (elementary) operation certificate examination question bank and online simulation examination
- 21 Chundong University blasting safety online peacetime operation 123 [standard answer]
- Restrictions on MySQL function creation
- Start of u-boot_ Armboot analysis (II)
- leaflet如何优雅的展示重叠点位的气泡窗口
猜你喜欢
![[leetcode] understanding and usage of map[key]+](/img/f5/9718f1020bddf913c09592697eb903.jpg)
[leetcode] understanding and usage of map[key]+

PLC也能制作小遊戲----Codesys編寫猜數字小遊戲

leaflet中如何通过透明度控制layerGroup的显示隐藏

妙才周刊 - 5

基于Three.js海上风电数字孪生三维效果

线上真实排队系统重构案例分享——实战篇

Basic operations of dict and set

Talent Weekly - 5

VS2015 DLIB 1916 USER_ ERROR__ inconsistent_ build_ configuration__ see_ dlib_ faq_ 1 USER_ ERROR__ inconsiste

Is the PMP training organization an actual training?
随机推荐
2022年G3锅炉水处理考题模拟考试平台操作
设计消息队列存储信息数据的MySQL表结构
Pytorch loading model error resolution
Leetcode 2200. Find all k nearest neighbor subscripts in the array (yes, one pass)
Summary of individual NLP internship experience
Xi'an Jiaotong 22nd autumn e-commerce technology online expansion resources (IV) [standard answer]
How to use Huawei cloud disaster tolerance solution to replace disaster recovery all-in-one machine
PMP test experience
Learn to divide subnets in an article
Is the revised PMP worth testing?
CV—BaseLine总结(从AlexNet到SENet的发展历程)
Basic operations of dict and set
Examination questions and online simulation examination for safety management personnel of hazardous chemical business units in 2022
Leetcode 2164. Sort odd and even subscripts separately (yes, once)
What can PMP bring to you
Video tracker error troubleshooting
Distributed lock implementation
【HCIE论述】RR-A
Lower interest rates lead to higher bond prices
2022年R2移动式压力容器充装考试题及在线模拟考试