天天看点

matlab loglog_第3讲 MATLAB作图——1020

第3讲 MATLAB作图——10-20

论文组成:文字、公式、表格、图形

matlab loglog_第3讲 MATLAB作图——1020
matlab loglog_第3讲 MATLAB作图——1020
matlab loglog_第3讲 MATLAB作图——1020

横坐标X一样

matlab loglog_第3讲 MATLAB作图——1020
x=linspace(0,2*pi,30); % 0,2π等间隔分成30个点y=sin(x);z=cos(x);plot(x,y,'r',x,z,'go') % 红线,绿圈
           
matlab loglog_第3讲 MATLAB作图——1020
matlab loglog_第3讲 MATLAB作图——1020
ezplot('cos(x)',[0,pi])ezplot('cos(t)^3','sin(t)^3',[0,2*pi])ezplot('exp(x)+sin(x*y)',[-2,0.5,0,2])
           
matlab loglog_第3讲 MATLAB作图——1020
matlab loglog_第3讲 MATLAB作图——1020
% myfun1.mfunction y=myfun1(x)y=exp(2*x)+sin(3*x^2)% liti43fplot('myfun1',[-1,2]) % fplot默认分割50个点。描点,连线。% liti28fplot('tanh',[-2,2])% liti42fplot('[tanh(x),sin(x),cos(x)]',2*pi*[-1 1 -1 1])
           

可以另存为figure,在figure中设置图形的线条、颜色等。

matlab loglog_第3讲 MATLAB作图——1020
matlab loglog_第3讲 MATLAB作图——1020

用途1:表示某个区间的大的范围。

用途2:为了大的范围内,小的区间进行对比。

%liti37x=logspace(-1,2);%x从10^-1到10^2,等间隔loglog(x,exp(x),'-s')grid on % 加栅格% liti38x=0:.1:10;semilogy(x,10.^x)%liti22x=[1:1:100];subplot(2,3,1);plot(x,x.^3);grid on;title 'plot-y=x^3';subplot(2,3,2);loglog(x,x.^3);grid on;title 'loglog-logy=3logx';subplot(2,3,3);plotyy(x,x.^3,x,x);grid on;title 'plotyy-y=x^3,logy=3logx';subplot(2,3,4);semilogx(x,x.^3);grid on;title 'semilogx-y=3logx';subplot(2,3,5);semilogy(x,x.^3);grid on;title 'semilogy-logy=x^3';
           
matlab loglog_第3讲 MATLAB作图——1020

空间曲线也是描点、连线。

matlab loglog_第3讲 MATLAB作图——1020
t=0:pi/50:10*pi;plot3(sin(t),cos(t),t)rotate3d
           
matlab loglog_第3讲 MATLAB作图——1020

其中X每一行都相同,Y的每一列都相同(将y转置后,不断重复)。

plot3的过程就是固定为某x一个值(一列),让y不同值(一列)进行遍历。

从而画出一条条曲线。

% plot的过程就是固定为某x一个值(一列),让y(一列)进行遍历。x=-3:0.1:3;y=1:0.1:5;[X,Y]=meshgrid(x,y);Z=(X+Y).^2;plot3(X,Y,Z)
           

区别:

plot3是多条曲线

surf是网格(填充)

mesh也是网格(没有填充)

meshz有参考平面

matlab loglog_第3讲 MATLAB作图——1020
x=-3:0.1:3;y=1:0.1:5;[X,Y]=meshgrid(x,y);Z=(X+Y).^2;surf(X,Y,Z)shading flatrotate3d
           
matlab loglog_第3讲 MATLAB作图——1020
x=-3:0.1:3;   y=1:0.1:5;[X,Y]=meshgrid(x,y);Z=(X+Y).^2;mesh(X,Y,Z)
           
matlab loglog_第3讲 MATLAB作图——1020
[X,Y]=meshgrid(-3:.125:3);Z=peaks(X,Y);meshz(X,Y,Z)
           
matlab loglog_第3讲 MATLAB作图——1020
matlab loglog_第3讲 MATLAB作图——1020
matlab loglog_第3讲 MATLAB作图——1020
x=linspace(0,2*pi,30);  %x离散化 y=sin(x); plot(x,y) xlabel('自变量X') ylabel('函数Y') title('示意图') grid on
           
matlab loglog_第3讲 MATLAB作图——1020
x=linspace(0,2*pi,30);y=sin(x);z=cos(x);plot(x,y,x,z)gtext('sin(x)');gtext('cos(x)');
           
matlab loglog_第3讲 MATLAB作图——1020
x=linspace(0.0001,0.01,1000);y=sin(1./x);  % x是向量,因此操作每个元素要用./plot(x,y)axis([0.005 0.01 -1 1])% 展示效果更好
           
matlab loglog_第3讲 MATLAB作图——1020

如果不用hold on,那么用两个plot,只会显示最后一个plot的图。

x=linspace(0,2*pi,30);z=cos(x);y=sin(x);plot(x,z)hold onplot(x,y)zoom  on
           
matlab loglog_第3讲 MATLAB作图——1020
x=linspace(0,2*pi,100);y=sin(x);z=cos(x);plot(x,y);title('sin(x)');pause% 需要在命令行按enter键figure(2);%如果不用figure2,那么就会覆盖之前的sin(x)plot(x,z);title('cos(x)');pause% 需要按enter键
           
matlab loglog_第3讲 MATLAB作图——1020
matlab loglog_第3讲 MATLAB作图——1020
x=linspace(0,2*pi,100); y=sin(x); z=cos(x); % six(x)是向量,每个相乘就需要.*    a=sin(x).*cos(x);b=sin(x)./(cos(x)+eps)% 避免cosx为0,因此加上最小值eps subplot(2,2,1);plot(x,y),title('sin(x)') subplot(2,2,2);plot(x,z),title('cos(x)') subplot(2,2,3);plot(x,a),title('sin(x)cos(x)') subplot(2,2,4);plot(x,b),title('sin(x)/cos(x)')  % 我们画多窗口图需要是以下步骤: figure(3)%选画布 subplot(2,2,1)%激活窗口 plot%画
           
matlab loglog_第3讲 MATLAB作图——1020
x=linspace(0,2*pi,30);y=sin(x);plot(x,y)zoom  on
           

缺省的意思就是默认

matlab loglog_第3讲 MATLAB作图——1020
x=-3:0.1:3;   y=1:0.1:5;[X,Y]=meshgrid(x,y);Z=(X+Y).^2;subplot(2,2,1); mesh(X,Y,Z)subplot(2,2,2);mesh(X,Y,Z);view(50,-34)subplot(2,2,3);mesh(X,Y,Z);view(-60,70) subplot(2,2,4);mesh(X,Y,Z);view(1,1)
           
matlab loglog_第3讲 MATLAB作图——1020
[x,y,z]=peaks(30);surf(x,y,z)axis([-3 3 -3 3 -10 10])%axis off%shading interp%colormap(hot)m=moviein(15);15帧for i=1:15 % 15张图片   view(-37.5+24*(i-1),30)   m(:,i)=getframe;%快照存到第i列endmovie(m)
           
matlab loglog_第3讲 MATLAB作图——1020
matlab loglog_第3讲 MATLAB作图——1020
 theta=linspace(0,2*pi),                         rho=sin(2*theta).*cos(2*theta);polar(theta,rho,'g')title('Polar plot of sin(2*theta).*cos(2*theta)');
           
matlab loglog_第3讲 MATLAB作图——1020

为了找出最小值、最大值,可以通过等值线进行分析。

load seamount %内置scatter(x,y,5,z) % 5是大小,z是不同的颜色的矩阵[X,Y]=meshgrid(-2:.2:2,-2:.2:3);Z=X.*exp(-X.^2-Y.^2);[C,h]=contour(X,Y,Z);clabel(C,h) %标注等值线的值colormap cool
           
matlab loglog_第3讲 MATLAB作图——1020
[x,y,z]=peaks;subplot(1,2,1)       contour3(x,y,z,16,'s')  % 空间等值线 grid,   xlabel('x-axis'),  ylabel('y-axis')zlabel('z-axis')title('contour3 of peaks'); subplot(1,2,2)contour(x,y,z,16,'s') % 平面等值线grid,   xlabel('x-axis'),    ylabel('y-axis')title('contour of peaks');
           
matlab loglog_第3讲 MATLAB作图——1020
[x,y,z]=sphere(16);% x是17x17的矩阵--289个点X=[x(:)*.5 x(:)*.75 x(:)];%x缩小为原来的0.5;0.75;不变Y=[y(:)*.5 y(:)*.75 y(:)];Z=[z(:)*.5 z(:)*.75 z(:)];%repmat:将向量[10 7.5 5]复制289遍  289x3的矩阵;1代表行方向复制。2代表列方向上复制S=repmat([1 .75 .5]*10,prod(size(x)),1);%圈的大小分别是1*10C=repmat([1 2 3],prod(size(x)),1);% 1颜色,2颜色 3颜色scatter3(X(:),Y(:),Z(:),S(:),C(:),'filled'),view(-60,60) %filled实心圈
           
matlab loglog_第3讲 MATLAB作图——1020
x=0:400:5600;y=0:400:4800;z=[370 470 550 600 670 690 670 620 580 450 400 300 100 150 250;...      510 620 730 800 850 870 850 780 720 650 500 200 300 350 320;...      650 760 880 970 1020 1050 1020 830 900 700 300 500 550 480 350;...      740 880 1080 1130 1250 1280 1230 1040 900 500 700 780 750 650 550;...      830 980 1180 1320 1450 1420 1400 1300 700 900 850 840 380 780 750;...      880 1060 1230 1390 1500 1500 1400 900 1100 1060 950 870 900 930 950;...      910 1090 1270 1500 1200 1100 1350 1450 1200 1150 1010 880 1000 1050 1100;...      950 1190 1370 1500 1200 1100 1550 1600 1550 1380 1070 900 1050 1150 1200;...      1430 1430 1460 1500 1550 1600 1550 1600 1600 1600 1550 1500 1500 1550 1550;...      1420 1430 1450 1480 1500 1550 1510 1430 1300 1200 980 850 750 550 500;...      1380 1410 1430 1450 1470 1320 1280 1200 1080 940 780 620 460 370 350;...      1370 1390 1410 1430 1440 1140 1110 1050 950 820 690 540 380 300 210;...      1350 1370 1390 1400 1410 960 940 880 800 690 570 430 290 210 150];meshz(x,y,z),rotate3dxlabel('X'),ylabel('Y'),zlabel('Z')%pausefigure(2)contour(x,y,z)%pausefigure(3)contour3(x,y,z)
           
matlab loglog_第3讲 MATLAB作图——1020
matlab loglog_第3讲 MATLAB作图——1020

牵涉图像的两个命令

imread()

imshow()

继续阅读