MATLAB---绘制三维图形函数实例介绍

286 篇文章 32 订阅
236 篇文章 15 订阅

例:用plot3函数绘制三维螺旋线。

>> t = linspace(0, 10*pi, 300);    
>> plot3(20*sin(t), 20*cos(t), t, 'r', 'linewidth', 2);    
>> hold on    %图形保持
>> quiver3(0,0,0,1,0,0,25,'k','filled','LineWidth',2); 
>> quiver3(0,0,0,0,1,0,25,'k','filled','LineWidth',2); 
>> quiver3(0,0,0,0,0,1,40,'k','filled','LineWidth',2); 
>> grid on
>> xlabel('X'); ylabel('Y'); zlabel('Z');    
>> axis([-25 25 -25 25 0 40]);    
>> view(-210,30);    

在这里插入图片描述
例:用meshgrid函数生成网格矩阵,并用plot函数画出平面网格图形。

[x,y] = meshgrid(1:4, 2:5)
x =
     1     2     3     4
     1     2     3     4
     1     2     3     4
     1     2     3     4
y =
     2     2     2     2
     3     3     3     3
     4     4     4     4
     5     5     5     5
>> plot(x, y, 'r',x', y', 'r', x, y, 'k.','markersize',18);
>> axis([0 5 1 6]);    
>> xlabel('X');  ylabel('Y');

在这里插入图片描述
例:绘制三维曲面在这里插入图片描述
的等高线图和梯度场。

 >> [X,Y] = meshgrid(-2:.2:2);        
>> Z = X.*exp(-X.^2 - Y.^2);         
>> [DX,DY] = gradient(Z,0.2,0.2);    
>> contour(X,Y,Z) ;                  
>> hold on ;                         
>> quiver(X,Y,DX,DY) ;               
>> h = get(gca,'Children');          
>> set(h, 'Color','k');   

在这里插入图片描述
例:用mesh,surf,surfl,surfc函数绘制二元正态分布的密度函数图。

>> x = linspace(-3,3,20); 
>> y = linspace(-9,9,20); 
>> [X, Y] = meshgrid(x,y); 
>> Z = mvnpdf([X(:) Y(:)], [0 0], [1 2;2 9]);    
>> Z = reshape(Z, size(X));    
>> subplot(2, 2, 1);
>> mesh(X, Y, Z);    
>> title('mesh');    
>> subplot(2, 2, 2);
>> surf(X, Y, Z);    
>> alpha(0.5);    
>> title('surf');
>> subplot(2, 2, 3);
>> surfl(X, Y, Z);    
>> title('surfl');
>> subplot(2, 2, 4);
>> surfc(X, Y, Z);    
>> title('surfc');

>
例:绘制三维曲面图z=sin(x+sin(y))-x/10。

>> [x,y]=meshgrid(0:0.25:4*pi);
>> z=sin(x+sin(y))-x/10;  mesh(x,y,z);
>> axis([0 4*pi 0 4*pi -2.5 1]);

在这里插入图片描述
例:调用函数绘制柱面,球面,椭球面。
% 绘制圆柱面

>> subplot(2,2,1);
>> [x,y,z] = cylinder;
>> surf(x,y,z); 
% 绘制哑铃面
>> subplot(2,2,2);
>> t = 0:pi/10:2*pi;
>> [X,Y,Z] = cylinder(2+cos(t));
>> surf(X,Y,Z);
% 绘制球面,半径为10,球心 (1,1,1)
>> subplot(2,2,3);
>> [x,y,z] = sphere;
>> surf(10*x+1,10*y+1,10*z+1);  axis equal;
% 绘制椭球面
>> subplot(2,2,4);
>> a=4; b=3;
>> t = -b:b/10:b;
>> [x,y,z] = cylinder(a*sqrt(1-t.^2/b^2),30);
>> surf(x,y,z);

在这里插入图片描述
例:绘制三维饼图、三维柱状图、三维火柴杆图、三维填充图、三维向量场图和立体切片图(四维图).

% 饼图
>> subplot(2,3,1); 
>> pie3([2347,1827,2043,3025]); 
>> title('三维饼图');               

% 柱状图
>> subplot(2,3,2); 
>> bar3(magic(4));
>> title('三维柱状图'); 

% 火柴杆图

>> subplot(2,3,3); 
>> y=2*sin(0:pi/10:2*pi); 
>> stem3(y);                       
>> title('三维火柴杆图'); 

% 填充图
>> subplot(2,3,4); 
>> fill3(rand(3,5),rand(3,5),rand(3,5), 'y' );    
>> title('三维填充图'); 
% 三维向量场图
>> subplot(2,3,5); 
>> [X,Y] = meshgrid(0:0.25:4,-2:0.25:2);  
>> Z = sin(X).*cos(Y); 
>> [Nx,Ny,Nz] = surfnorm(X,Y,Z); 
>> surf(X,Y,Z); 
>> hold on; 
>> quiver3(X,Y,Z,Nx,Ny,Nz,0.5); 
>> title('三维向量场图'); 
>> axis([0 4 -2 2 -1 1]); 
% 立体切片图(四维图)
>> subplot(2,3,6); 
>> t = linspace(-2,2,20); 
>> [X,Y,Z] = meshgrid(t,t,t); 
>> V = X.*exp(-X.^2-Y.^2-Z.^2);    
>> xslice = [-1.2,.8,2]; 
>> yslice = 2; 
>> zslice = [-2,0]; 
>> slice(X,Y,Z,V,xslice,yslice,zslice); 
>> title('立体切片图(四维图)');

在这里插入图片描述

  • 4
    点赞
  • 60
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值