在 MATLAB 中绘制 3D 轮廓
等高线图用于通过在 2D 表面上绘制 z 幻灯片来显示 3D 表面。等高线图也称为线图。在轮廓中,我们有 3 个变量 x、y、z。 x, y 变量用于给出 z 的值,( z=f(x, y))。 x 和 y 变量通常位于称为 meshgrid 的网格中。
MATLAB 中有各种等高线图,如 contour、contourf、contour3、contourc、countourslice、clabel 和 fcontour。在本文中,我们将了解如何在 MATLAB 中绘制 3D 轮廓。为了绘制 3D 轮廓,我们将使用countour3()绘制不同类型的 3D 模块。
句法:
- contour3(X,Y,Z): Specifies the x and y coordinates for the values in Z.
- contour3(Z): Creates a 3-D contour plot containing the isolines of matrix Z, where Z contains height values on the x-y plane.
- contour3(___,levels): Specify levels as a scalar value n to display the contour lines at n automatically chosen levels (heights).
- contour3(___,LineSpec): Specifies the style and color of the contour line.
循序渐进的方法:
- 在 meshgrid函数输入 x 和 y 的输入。
Matlab
% plot the points for x and y
[x,y]=meshgrid(-5 : 0.1 :5);
Matlab
% give any equation for z variable
z = x.^2 + y.^2 ;
% disp is used to display the
% value for z
disp(z);
Matlab
% to plot the 3D surface use contour3()
% contour3(z ,levels), levels is used to
% display the height for the contourlines
contour3(z,25);
% to label x ,y ,z axis and title.
xlabel('x-axis');
ylabel('y-axis');
zlabel('z-axis');
title('3D contour');
Matlab
% code for 3D plot
% plot the points for x and y
[x,y]=meshgrid(-5 : 0.1 :5);
% give any equation for z variable
z = x.^2 + y.^2 ;
disp(z);
% to plot the 3D surface use contour3()
% contour3(z ,levels), levels is used
% to display the height for the contour
% lines
contour3(z,25);
% to label x ,y ,z axis and title.
xlabel('x-axis');
ylabel('y-axis');
zlabel('z-axis');
title('3D contour');
Matlab
% code for 3D plot,
% plot the points for x and y
[x,y]=meshgrid(-5 : 0.1 :5);
% give any equation for z variable
z = x.^2 - y.^2 ;
disp(z);
% to plot the 3D surface use contour3()
% contour3(z ,levels), levels is used to
% display the height for the contourlines
contour3(z,25);
% to label x ,y , z axis and title
xlabel('x-axis');
ylabel('y-axis');
zlabel('z-axis');
title('3D contour');
Matlab
% code for 3D plot,
% plot the points for x and y
[x,y]=meshgrid(-7 : 0.1 : 7);
% give any equation for z variable
z = sin(x)+ cos(y) ;
disp(z);
% to plot the 3D surface use contour3()
% contour3(z ,levels), levels is used to
% display the height for the contour
% lines
contour3(z,25);
% to label x ,y and z axis
xlabel('x-axis');
ylabel('y-axis');
zlabel('z-axis');
title('3D contour');
- 在方程中使用 x 和 y 传递 z 的值。
MATLAB
% give any equation for z variable
z = x.^2 + y.^2 ;
% disp is used to display the
% value for z
disp(z);
- 使用 contour3 绘制 3d 等高线并使用 label() 方法标记轴。
MATLAB
% to plot the 3D surface use contour3()
% contour3(z ,levels), levels is used to
% display the height for the contourlines
contour3(z,25);
% to label x ,y ,z axis and title.
xlabel('x-axis');
ylabel('y-axis');
zlabel('z-axis');
title('3D contour');
下面是完整的程序:
MATLAB
% code for 3D plot
% plot the points for x and y
[x,y]=meshgrid(-5 : 0.1 :5);
% give any equation for z variable
z = x.^2 + y.^2 ;
disp(z);
% to plot the 3D surface use contour3()
% contour3(z ,levels), levels is used
% to display the height for the contour
% lines
contour3(z,25);
% to label x ,y ,z axis and title.
xlabel('x-axis');
ylabel('y-axis');
zlabel('z-axis');
title('3D contour');
输出:
示例 1:
这是另一个与上一个类似的示例。
MATLAB
% code for 3D plot,
% plot the points for x and y
[x,y]=meshgrid(-5 : 0.1 :5);
% give any equation for z variable
z = x.^2 - y.^2 ;
disp(z);
% to plot the 3D surface use contour3()
% contour3(z ,levels), levels is used to
% display the height for the contourlines
contour3(z,25);
% to label x ,y , z axis and title
xlabel('x-axis');
ylabel('y-axis');
zlabel('z-axis');
title('3D contour');
输出:
示例 2:
生成 3D 轮廓的 MATLAB 程序:
MATLAB
% code for 3D plot,
% plot the points for x and y
[x,y]=meshgrid(-7 : 0.1 : 7);
% give any equation for z variable
z = sin(x)+ cos(y) ;
disp(z);
% to plot the 3D surface use contour3()
% contour3(z ,levels), levels is used to
% display the height for the contour
% lines
contour3(z,25);
% to label x ,y and z axis
xlabel('x-axis');
ylabel('y-axis');
zlabel('z-axis');
title('3D contour');
输出: