Bài giảng Vẽ đồ thị trong Matlab (Bản đẹp)

Tùy chỉnh màu sắc và độ lớn nét vẽ

LineWidth : độ rộng của nét vẽ, tính bằng pt.

MarkerEdgecolor: màu của đường viền marker.

MarkerFacecolor: màu bên trong marker.

Markersize: độ lớn của marker, tính bằng pt.

 

ppt49 trang | Chuyên mục: MATLAB | Chia sẻ: yen2110 | Lượt xem: 500 | Lượt tải: 0download
Tóm tắt nội dung Bài giảng Vẽ đồ thị trong Matlab (Bản đẹp), để xem tài liệu hoàn chỉnh bạn click vào nút "TẢI VỀ" ở trên
Vẽ đồ thị trong MatlabTRONG KHÔNG GIAN 2 CHIỀULệnh plot	plot(x,f(x))Với:	f(x) - hàm số cần vẽ	x – vectơ miền giá trị của hàm fVí dụVẽ đồ thị của hàm y = sin(x)x = 0:pi/100:2*pi;y = sin(x);plot(x, y)Các tham số plot(x,y,’linestye_marker_color’)linestye‘-’‘--‘‘:’‘-.’marker‘+’+‘h’/‘hexagram’‘o’^‘*’*v‘.’.>‘s’/‘square’r')hold ont =0:1:7;plot(t, 2*t,'-*b');Xác định tọa độLệnh axisaxis([xmin xmax ymin ymax])Tùy chỉnh các kiểu trục tọa độ	axis on/off/auto	axis normal/square/equal/tight axis ij/xy	grid on/offVí dụXác định tọa độxlim([xmin xmax])ylim([ymin ymax])Xác định giới hạn của trục Ox và Oy.Ví dụx = linspace(0,3,500);y = 1./(x - 1).^2 + 3./(x - 2).^2;plot(x,y); grid on;ylim([0 50]);Xác định tọa độxticks và yticksVí dụx = -pi:.1:pi;y = sin(x);plot(x,y)set(gca,'XTick',-pi:pi/2:pi)set(gca,'XTickLabel',	{'-pi','-pi/2','0','pi/2','pi'})y = [0 31 6 5 9];area(y)str = 'Jan|Feb|Mar|April|May|June';set(gca,'xtick',1:6,...'xticklabel',str,'xgrid','on','layer','top')set(gca,’XTickLabel’,{’1’;’10’;’100’}set(gca,’XTickLabel’,’1|10|100’)set(gca,’XTickLabel’,[1;10;100])set(gca,’XTickLabel’,0:2)set(gca,’XTickLabel’,[’1 ’;’10 ’;’100’])Chú thích trên đồ thịCác lệnhxlabel; ylabeltitle legendtext; gtext;Ví dụx = -pi:.1:pi;t = -pi/4;y = sin(x);plot(x,y)set(gca,'XTick',-pi:pi/2:pi)set(gca,'XTickLabel',{'-pi','pi/2','0','pi/2','pi'})xlabel('-\pi \leq \Theta \leq \pi')ylabel('sin(\Theta)')title('Plot of sin(\Theta)')text(-pi/4,sin(-pi/4),'\leftarrow sin(\pi\div4)',...'HorizontalAlignment','left')legend('sin \theta')hold onplot(t, sin(t),’or’)plotyy - Vẽ đồ thị trên 2 trục tọa độ khác nhaut=0:900; A=1000; a=0.005; b=0.005;y1 = A*exp(-a*t);y2 = sin(b*t);plotyy(t,y1,t,y2,'semilogy','plot') subplot – vẽ nhiều đồ thị trong cùng một cửa sổsubplot(m,n,p)Ví dụt = 0:pi/20:2*pi;[x,y] = meshgrid(t);subplot(2,2,1)plot(sin(t),cos(t))axis equalsubplot(2,2,2)z = sin(x)+cos(y);plot(t,z)axis([0 2*pi -2 2])subplot(2,2,3)z = sin(x).*cos(y);plot(t,z)axis([0 2*pi -1 1])subplot(2,2,4)z = (sin(x).^2)-(cos(y).^2);plot(t,z)axis([0 2*pi -1 1]) TRONG KHÔNG GIAN 3 CHIỀUplot3(x,y,z)Ta cần xác định các vectơ x, y, z. Để vẽ mặt (x, y, z=f(x,y)), lệnhmeshgrid(x,y) sẽ tạo ra mảng X, Y từ miền giá trị của x, y.Ví dụt = 0:0.02*pi:25*pi;x = sin(t); y = cos(t); z = t; plot3(x,y,z);Vẽ mặt: với và .[x,y]=meshgrid([-4:0.1:4]);z=x.*x.*y.*exp(-x.^2-y.^2);plot3(x,y,z)Dùng lệnh mesh(z)Ví dụ Vẽ mặt với x=0:0.1:pi;y=0:0.1:pi; [X,Y]=meshgrid(x,y);Z=sin(Y.^2+X)-cos(Y-X.^2); subplot(221);mesh(Z);subplot(222);meshc(Z); subplot(223);mesh(x,y,Z);axis([0 pi 0 pi -5 5]);subplot(224);mesh(Z);hidden offMột số lệnh vẽ đồ thị trong 3 - Dplot3contour / contourf / contour3mesh / meshc / meshzsurf / surfcwaterfallbar3 / bar3hpie3 / fill3comet3 / scatter3 / stem3In và xuất đồ thịDùng lệnhprint -dtiff -r200 mygraph.tiffprint –deps2 mygraph.eps Sử dụng Plotting ToolsCác dạng đồ thị đặc biệt khácĐồ thị dạng cột	bar(X,Y)	X = 1:M	Y : Ma trận cỡ MxN	Tùy chọn khác	barh / bar3 / bar3h	‘stacked’, ‘grouped’,’detached’Ví dụY =[7 6 5;6 8 1;4 5 9;2 3 4;9 7 2]subplot(221);bar(Y);title('bar(Y,''grouped'')')subplot(222);bar(0:5:20,Y);title('bar(0:5:20,Y,''grouped'')')subplot(223);bar(Y,'stacked');title('bar(Y,''stacked'')')subplot(224);barh(Y);title('barh(Y)')subplot(221);bar3(Y,'detached');title('bar3(Y,''detached'')')subplot(222);bar3(Y,'grouped');title('bar3(Y,''grouped'')')subplot(223);bar3(Y,'stacked');title('bar3(Y,''stacked'')')subplot(224);bar3h(Y);title('bar3h(Y)')Biểu đồ quạt	pie(X,str)	X: vectơ dữ liệu	str: chuỗi chứa nhãn của đồ thị	Tùy chọn khác	pie3Ví dụY = [12 20 15 25 78]pie(Y)pie(Y,{‘Giai tich’,’Dai so’,’Xac suat’,’Co hoc’,’Tin hoc’})pie3(Y)pie(Y,[0 0 1 0 1])Đồ thị dạng vùng	area(x) 	area(x,y)Ví dụx =[0:9];y = 2*sin(x);area(x,y,'Facecolor','blue','Edgecolor','red');sales = [51.6 82.4 90.8 59.1 47.0];x = 90:94;profits = [19.3 34.2 61.4 50.5 29.4];area(x,sales,'FaceColor',[.5 .9 .6],'EdgeColor','b',...'LineWidth',2)hold onarea(x,profits,'FaceColor',[.9.85.7],'EdgeColor','y',...'LineWidth',2)hold offset(gca,'XTick',[90:94])set(gca,'Layer','top')gtext('\leftarrow Sales')gtext('Profits')gtext('Expenses')xlabel('Years','FontSize',14)ylabel('Expenses + Profits = Sales in1,000''s','FontSize',14) Đồ thị của dữ liệu rời rạc	stem / stem3 : đồ thị dạng rời rạc trong 2D / 3D	stair : đồ thị bậc thangVí dụalpha = .02; beta = .5; t = 0:4:200;y = exp(-alpha*t).*cos(beta*t);subplot(2,1,1);stem(t,y);title('stem(t,y)')subplot(2,1,2);stem(t,y,'—sr','fill');title('stem(t,y,''--sr'',''fill'')')Trong 3-Dt = 0:.1:10; s = 0.1+i; y = exp(-s*t); stem3(real(y),imag(y),t)hold onplot3(real(y),imag(y),t,'r')hold offview(-39.5,62)xlabel('Real')ylabel('Imaginary')zlabel('Magnitude')Dạng bậc thangalpha = 0.01;beta = 0.5;t = 0:10;f = exp(-alpha*t).*sin(beta*t);stairs(t,f)hold on;plot(t,f,'--*');hold offlabel = 'Stairstep plot of e^{-(\alpha*t)}sin\beta*t';text(0.5,-0.2,label,'FontSize',14);xlabel('t = 0:10','FontSize',14);axis([0 10 -1.2 1.2])

File đính kèm:

  • pptbai_giang_ve_do_thi_trong_matlab_ban_dep.ppt
Tài liệu liên quan