Skip to content Skip to sidebar Skip to footer

draw 3d shapes in matlab

0 Preface

This article isScientific Computing and MATLAB LanguageThe fifth department of the topic four summarizes the notes, and combines a little understanding of my ain. After reading this article, you can easily utilize MATLAB related functions to draw beautiful iii-dimensional surfaces.

1 Generation of plane grid data

Before drawing a 3D surface, you demand to store the airplane coordinates corresponding to the 3D surface. How to store it? There are two simple methods.
Employ matrices Ten and Y to store the ten-coordinate and y-coordinate of each small-scale rectangle vertex respectively. The matrices X and Y are the xy plane grid coordinate matrix of the rectangular expanse.
For example, generate a grid coordinate surrounded by (2,6), (3,eight) on the X-Y coordinate plane.
In MATLAB, there are ii methods to generate the grid coordinate matrix in the plane area.
Generated using matrix operations

          ten=2:6;  y=(3:viii)';  Ten=ones(size(y))*10; Y=y*ones(size(ten));                  

Elements at the same position of 10 and Y, such every bit Ten 32 X_{32} Y 32 Y_{32} Is the coordinate (3, 5) of the filigree point in the third row and 2d cavalcade of the area.
Generated using the meshgrid role
This method is what we need! concise!

[X,Y]=meshgrid(10,y);
where
The parameters x and y are vectors, and the X and Y storing the coordinates of the filigree points are matrices.

          x=2:ane:6; y=(three:1:8)'; [X,Y]=meshgrid(10,y);                  

The grid coordinate matrix X and Y generated past the command in line iii are the same as those obtained in the first i.
If the function value z is calculated according to the ten and y coordinates of each filigree point, the function value matrix Z will exist obtained. Each column vector in the matrix X, Y, Z corresponds to the coordinate of a bend data point.
Instance i Draw a space curve.
plot3.7

          x = 2:6;  y = (3:8)'; [10, Y] = meshgrid(x, y); Z = randn(size(Ten)); plot3(10,Y,Z) grid on;                  

ii Functions for drawing three-dimensional surfaces

ii.ane The general call method of mesh part and surf office

mesh(x,y,z,c)
surf(x,y,z,c)
where
x and y are filigree coordinate matrix
z is the height matrix on the grid points
c is used to specify the color of the surface at different heights
c When omitted, the color setting is proportional to the meridian of the graphic.
Case two Draw 3D surface graph z = 10 e ten 2 y 2 z=xe^{-x^2-y^two}
plot3.8

          t = -2:0.ii:2;  [10, Y] = meshgrid(t); Z = X .* exp(-X.^2 - Y.^2); subplot(one,3,1) mesh(Ten,Y,Z);  subplot(ane,3,2) surf(10,Y,Z);  subplot(1,3,three) plot3(X,Y,Z);  grid on                  

ii.two Other ways to summon

mesh(z,c)
surf(z,c)
When 10 and y are omitted, the second-dimensional subscript of the z matrix is ​​regarded every bit the x-axis coordinate, and the first-dimensional subscript of the z matrix is ​​regarded as the y-centrality coordinate.
For example, draw a surface.
plot3.9

          t=1:5;  z=[0.5*t;ii*t;3*t]; mesh(z);                  

Note: z is a matrix with 3 rows and 5 columns, so the Ten-axis coordinates are 1, 2, 3, 4, and v, and the Y-axis coordinates are 1, 2, 3.

2.3 Relatives and friends of mesh and surf functions

Function name Function description
meshc bandcontour lineof3D mesh surface
meshz ringBaseof3D mesh surface
surfc bandcontour lineofCurved surface
surfl ringLighting effectofCurved surface

Example iii Use 4 ways to depict a function z = ( ten 1 ) 2 + ( y 2 ) two 1 z=(x−1)^2+(y−ii)^2−ane Surface map. among them, x [ 0 , 2 ] y [ 1 , 3 ] x∈[0,2],y∈[1,three]。
plot3.10

          [x,y]=meshgrid(0:0.one:ii,1:0.1:3); z=(x-1).^ii+(y-ii).^two-ane; subplot(2,two,one); meshc(x,y,z);title('meshc(x,y,z)') subplot(2,2,2); meshz(10,y,z);title('meshz(x,y,z)') subplot(ii,ii,3); surfc(ten,y,z);title('surfc(x,y,z)') subplot(2,2,4); surfl(x,y,z); title('surfl(x,y,z)')                  

3. Standard iii-dimensional surface

3.1 sphere role-draw a ball

[x,y,z]=sphere(north)
Generate 3 (due north+one)-order square matrices. Using these 3 matrices, a unit sphere with the center at the origin and a radius of 1 can be drawn.

3.two cylinder function

[x,y,z]=cylinder(R,n)
Among them, the parameter R is a vector that stores the radius of each equal interval height of the cylinder. n means that there are north interval points on the circumference of the cylinder, and at that place are 20 interval points by default.
Example iv Use cylinder function to draw cylinder, vase and cone respectively.
plot3.11

          subplot(1,iii,1); [ten,y,z]=cylinder; surf(x,y,z); subplot(1,3,2); t=linspace(0,2*pi,40); [x,y,z]= cylinder(ii+cos(t),30); surf(x,y,z); subplot(i,3,3); [x,y,z]= cylinder(0:0.two:2,30); surf(x,y,z);                  

Instance 5 Utilise the cylinder function to draw the intersection of two mutually perpendicular cylindrical surfaces with equal diameters.
plot3.11-12

          [x,y,z]= cylinder(one,sixty); z=[-1*z(2,:);z(2,:)]; surf(ten,y,z) concur on surf(y,z,x) centrality equal                  

3.3 peaks function-acme surface plot

The calling method of peaks role:
peaks(n) returns an n×n matrix
peaks(V) returns an n×northward matrix, where northward = length(V)
peaks(x,y) Calculate peaks at the given 10 and Y (must be the same size) and return a matrix of the same size
peaks
Reference for specific usageOfficial certificate。
Multimodal function: z = iii ( 1 x ) ii e ( ten 2 ) ( y + one ) two 10 ( x 5 x three y 5 ) e ( x 2 y 2 ) i 3 eastward ( x + one ) 2 y ii z = 3(1-x)^2e^{-(10^ii) - (y+ane)^2 }-10(\frac{ten}{v} - x^3 - y^v)e^{(-x^2-y^2})-\frac{1}{3}eastward^{-(x+1)^2 - y^2}
plot3.12

          [ten,y]=peaks(ten); z =  three*(1-x).^2.*exp(-(x.^2) - (y+i).^2) ...     - 10*(ten/v - ten.^3 - y.^v).*exp(-x.^2-y.^ii) ...     - i/3*exp(-(10+1).^ii - y.^2) ; surf(z)                  

You can also draw this graph through the following command.

          figure peaks(ten)                  

plot3.13
Is it the aforementioned? It seems non, the coordinate axis interval and labeling are dissimilar, hahaha. At the moment I don't know what this part is useful for, but it seems very powerful!

three.iv fmesh function and fsurf function-three equations and two independent variables

If a graph is determined past three parameter equations and has two independent variables, so the fmesh function and fsurf function tin can be used.
fmesh(funx,funy,funz,uvlims)
fsurf(funx,funy,funz,uvlims)
Amid them, funx, funy, and funz represent functions that ascertain the x, y, and z coordinates of the surface, ordinarily in the class of function handles.
uwlims is the value range of the independent variables of funx, funy and funz, described past a 4-element vector (umin, umax, vmin, vmaxd, and the default is [-5, 5, -5, five].
Example 6 Draw a spiral surface { x = u s i n v y = u c o southward five , ( v u five , 5 v 2 ) z = v \left\{ \begin{aligned} x&=usinv\\ y&=-ucosv,(-5 \leq u\leq5,-5\leq v\leq2)\\ z&=v \end{aligned}\right.

          funx = @(u,v) u.*sin(5); funy = @(u,v) -u.*cos(v); funz = @(u,v) v; fsurf(funx,funy,funz,[-v 5 -v -ii])% draw the bottom half hold on  fmesh(funx,funy,funz,[-v five -2 two])% draw the upper office concur off                  

4 Decision

Accept you learned information technology? If this commodity is helpful to y'all, you can give it a thumbs upward. If you lot have any questions, please signal it out.

hulltagoink39.blogspot.com

Source: https://programmersought.com/article/20484922928/

Post a Comment for "draw 3d shapes in matlab"