1.基本画图
程序如下:
x=0:pi/1000:2*pi; y1=sin(2*x); y2=2*cos(2*x); %输出图像 plot(x,y1,'k-',x,y2,'b--'); title(' Plot of f(x)=sin(2x) and its derivative'); %设置X坐标和Y坐标的标签 xlabel('x'); ylabel('y'); %制作图例 legend('f(x)=sin(2x)','d/dx f(x)') %显示网格 grid on;
显示结果:
2.利用有限个点画出平滑曲线
x=1:1:10; y=[1,4,8,10,11,11.5,12,7,5,1]; xx=1:0.01:10; %使用了函数interp1 yy=interp1(x,y,xx,'cublic'); plot(xx,yy,x,y,'.'); grid on;
3.绘制三维曲线
t=0:pi/100:20*pi; x=sin(t); y=cos(t); z=t.*sin(t).*cos(t); plot3(x,y,z); title('Line in 3-D Space'); xlabel('X');ylabel('Y');zlabel('Z'); grid on;
4.绘制三维曲面
[x,y]=meshgrid(-8:0.5:8); z=sin(sqrt(x.^2+y.^2))./sqrt(x.^2+y.^2+eps); subplot(2,2,1); mesh(x,y,z); title('mesh(x,y,z)') subplot(2,2,2); meshc(x,y,z); title('meshc(x,y,z)') subplot(2,2,3); meshz(x,y,z) title('meshz(x,y,z)') subplot(2,2,4); surf(x,y,z); title('surf(x,y,z)')
标准三维曲面:
t=0:pi/20:2*pi; [x,y,z]= cylinder(2+sin(t),30); subplot(2,2,1); surf(x,y,z); subplot(2,2,2); [x,y,z]=sphere; surf(x,y,z); subplot(2,1,2); [x,y,z]=peaks(30); surf(x,y,z);
其他函数:
subplot(2,2,1); bar3(magic(4)) subplot(2,2,2); y=2*sin(0:pi/10:2*pi); stem3(y); subplot(2,2,3); pie3([2347,1827,2043,3025]); subplot(2,2,4); fill3(rand(3,5),rand(3,5),rand(3,5), 'y' )
绘制多峰函数的瀑布图和等高线图:
subplot(1,2,1); [X,Y,Z]=peaks(30); waterfall(X,Y,Z) xlabel('X-axis'),ylabel('Y-axis'),zlabel('Z-axis'); subplot(1,2,2); contour3(X,Y,Z,12,'k'); %其中12代表高度的等级数 xlabel('X-axis'),ylabel('Y-axis'),zlabel('Z-axis');
5.图形修饰处理
三种图像着色方式效果显示:
[x,y,z]=sphere(20); colormap(copper); subplot(1,3,1); surf(x,y,z); axis equal subplot(1,3,2); surf(x,y,z);shading flat; axis equal subplot(1,3,3); surf(x,y,z);shading interp; axis equal
光照处理后的球面:
[x,y,z]=sphere(20); subplot(1,2,1); surf(x,y,z);axis equal; light('Posi',[0,1,1]); shading interp; hold on; plot3(0,1,1,'p');text(0,1,1,' light'); subplot(1,2,2); surf(x,y,z);axis equal; light('Posi',[1,0,1]); shading interp; hold on; plot3(1,0,1,'p');text(1,0,1,' light');
图形的裁剪处理:
[x,y]=meshgrid(-5:0.1:5); z=cos(x).*cos(y).*exp(-sqrt(x.^2+y.^2)/4); subplot(1,2,1); surf(x,y,z);shading interp; title('裁剪之前'); i=find(x<=0&y<=0); z1=z;z1(i)=NaN; subplot(1,2,2); surf(x,y,z1);shading interp; title('裁剪之后');
从文件载入图像:
[x,cmap]=imread('flower.jpg'); %读取图像的数据阵和色图阵 image(x);colormap(cmap); axis image off %保持宽高比并取消坐标轴
制作动画:
[X,Y,Z]=peaks(30); surf(X,Y,Z) axis([-3,3,-3,3,-10,10]) axis off; shading interp; colormap(hot); m=moviein(20); %建立一个20列大矩阵 for i=1:20 view(-37.5+24*(i-1),30) %改变视点 m(:,i)=getframe; %将图形保存到m矩阵 end movie(m,2); %播放画面2次