分享兴趣,传播快乐,增长见闻,留下美好。
亲爱的您,
这里是LearningYard学苑!
今天小编给大家带来Matlab三维绘图(a),
欢迎您的用心访问!
本期推文阅读时长大约8分钟,请您耐心阅读。
Share interest, spread happiness, increase knowledge, and leave beautiful.
Dear you,
This is the LearningYard Academy!
Today,the editor brings you Matlab three-dimensional drawing (a),
Welcome your visit!
This tweet usually takes about 8 minutes to read. Please be patient and read.
在上次分享中,我们学习了如何利用Matlab绘制二维曲线,但在我们实际生活中,往往需要我们绘制出三维图像来解决问题,下面让我们一起学习在Matlab中如何绘制出三维图像吧。
In the last sharing, we learned how to use Matlab to draw two-dimensional curves, but in our real life, we often need to draw three-dimensional images to solve problems. Let's learn how to draw three-dimensional images in Matlab.
Part.1 绘制三维曲线图
最基本的三维图像函数为plot3,它将二维绘图函数plot的有关功能扩展到三维空间。其调用格式为:plot3(x1,y1,z1,选项1,x2,y2,z2,选项2,......)在Matlab命令窗口中输入代码,绘制出图像。
The most basic three-dimensional image function is plot3, which extends the related functions of the two-dimensional drawing function plot to three-dimensional space. The calling format is: plot3 (x1, y1, z1, option 1, x2, y2, z2, option 2,...) Enter the code in the Matlab command window to draw an image.
- plot3命令以x1,y1,z1所给出的数据分别为x,y,z坐标值,option1为选项参数,以逐点连折线的方式绘制1个三维折线图形;同时,以x2,y2,z2所给出的数据分别为x,y,z坐标值,option2为选项参数,以逐点折线的方式绘制另一个三维折线图形。
- The plot3 command uses the data given by x1, y1, and z1 as the x, y, and z coordinate values, and option1 as the option parameter, and draws a three-dimensional polyline graph by connecting a polyline point by point; at the same time, using x2, y2, z2 The given data are x, y, z coordinate values respectively, option2 is the option parameter, and another three-dimensional polyline graph is drawn in the way of point-by-point polyline.
- plot3命令的功能及使用方法与plot命令的功能及使用方法相类似,它们的区别在于前者绘制出的是三维图形。
- The function and usage of the plot3 command is similar to the function and usage of the plot command. The difference between them is that the former draws three-dimensional graphics.
- plot3命令参数的含义与plot命令的参数含义相类似,它们的区别在于前者多了一个Z方向上的参数。同样,各个参数的取值情况及其操作效果也与plot命令相同。
- The meaning of the parameters of the plot3 command is similar to the meaning of the parameters of the plot command. The difference between them is that the former has an additional parameter in the Z direction. Similarly, the value of each parameter and its operation effect are the same as the plot command.
- plot3命令使用的是以逐点连线的方法来绘制三维折线的,当各个数据点的间距较小时,我们也可利用它来绘制三维曲线。
- The plot3 command uses a point-by-point method to draw a three-dimensional polyline. When the distance between each data point is small, we can also use it to draw a three-dimensional curve.
示例1:利用plot3绘制三维线条图形。
clear,clc
[X,Y]=meshgrid([-4:0.1:4]);
Z=3*X.*(-X.^3-2*Y.^2);
plot3(X,Y,Z,'b')
执行程序后,输出如图下图所示的图形:
After executing the program, the output is as shown in the figure below:
Part.2 绘制三维曲面图
在Matlab中,可用函数surf、surfc来绘制三维曲面图,其调用格式如下:
In Matlab, the functions surf and surfc can be used to draw 3D surface graphs. The calling format is as follows:
- surf(Z)
- %以矩阵Z指定的参数创建一渐变的三维曲面,坐标x = 1:n,y = 1:m,其中[m,n] = size(Z)
- % Create a gradient three-dimensional surface with the parameters specified by the matrix Z, the coordinates x = 1:n, y = 1:m, where [m,n] = size(Z)
- surf(X,Y,Z)
- %以Z确定的曲面高度和颜色,按照X、Y形成的格点矩阵,创建一渐变的三维曲面。X、Y可以为向量或矩阵,若X、Y为向量,则必须满足m= size(X),n =size(Y),[m,n] = size(Z)
- % According to the height and color of the surface determined by Z, according to the lattice matrix formed by X and Y, create a gradient three-dimensional surface. X and Y can be vectors or matrices. If X and Y are vectors, they must satisfy m= size(X), n = size(Y), [m,n] = size(Z)
- surf(X,Y,Z,C)
- %以Z确定的曲面高度,C确定的曲面颜色,按照X、Y形成的格点矩阵,创建一渐变的三维曲面
- % Use the height of the surface determined by Z, the color of the surface determined by C, and create a gradient three-dimensional surface according to the lattice matrix formed by X and Y
- surf(...,'PropertyName',PropertyValue)
- %设置曲面的属性
- % set the properties of the surface
- surfc(...)
- %采用surfc函数的格式同surf,同时在曲面下绘制曲面的等高线
- % The format of the surfc function is the same as surf, and the contour of the surface is drawn under the surface at the same time
示例2:绘制球体的三维图形。
Example 2: Drawing a 3D figure of a sphere.
clear,clc
figure
[X,Y,Z]=sphere(40); %计算球体的三维坐标
surf (X,Y,Z); %绘制球体的三维图形
xlabel('x'),ylabel('y'),zlabel('z');
title(' shading faceted ');
执行程序后,输出如图下图所示的图形:
After executing the program, the output is as shown in the figure below:
Part.3 三维绘图的各种样式
Matlab绘制三维图,最常用的是surf、mesh这两个函数及其衍生函数,以peaks函数为例,以各种不同方法进行绘图。
Matlab draws three-dimensional graphs. The two most commonly used functions are surf and mesh and their derivative functions. Taking the peaks function as an example, draw in various methods.
1. meshz可将曲面加上围裙:
meshz can add an apron to a surface:
[x,y,z]=peaks;
meshz(x,y,z);
axis([-inf inf -inf inf -inf inf]);
2. waterfall可在x方向或y方向产生水流效果:
waterfall can produce water flow in the x-direction or y-direction:
[x,y,z]=peaks;
waterfall(x,y,z);
axis([-inf inf -inf inf -inf inf]);
3. y方向的水流效果:
The water flow effect in the y direction:
[x,y,z]=peaks;
waterfall(x',y',z');
axis([-inf inf -inf inf -inf inf]);
4. meshc可以同时画出网状图与等高线:
Meshc can draw mesh diagrams and contour lines at the same time:
[x,y,z]=peaks;
meshc(x,y,z);
axis([-inf inf -inf inf -inf inf]);
5. surfc同时画出曲面图与等高线:
surfc draws surface graphs and contour lines at the same time:
[x,y,z]=peaks;
surfc(x,y,z);
axis([-inf inf -inf inf -inf inf]);
6. 其他图形的绘制
Drawing of other graphics
举一个例子,使用同一色图,以不同着色方式绘制圆锥体。
As an example, use the same colormap to draw cones in different shades.
[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)
今天的分享就到这里了。
如果您对今天的文章有独特的想法,
欢迎给我们留言,
让我们相约明天,
祝您今天过得开心快乐!
That's it for today's sharing.
If you have a unique idea about today’s article,
Welcome to leave us a message,
Let us meet tomorrow,
I wish you a happy day today!
参考资料:谷歌翻译、哔哩哔哩
本文由LearningYard学苑原创,如有侵权请在后台留言!
文案 |Yuan
排版 |Yuan
审核 |Qian
,