Đồ họa ứng dụng - Vẽ đồ thị trong Matlab

ppt 49 trang vanle 2990
Bạn đang xem 20 trang mẫu của tài liệu "Đồ họa ứng dụng - Vẽ đồ thị trong Matlab", để tải tài liệu gốc về máy bạn click vào nút DOWNLOAD ở trên

Tài liệu đính kèm:

  • pptdo_hoa_ung_dung_ve_do_thi_trong_matlab.ppt

Nội dung text: Đồ họa ứng dụng - Vẽ đồ thị trong Matlab

  1. Vẽ đồ thị trong Matlab
  2. TRONG KHÔNG GIAN 2 CHIỀU ◼ Lệ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 f
  3. Ví dụ ◼ Vẽ đồ thị của hàm y = sin(x) x = 0:pi/100:2*pi; y = sin(x); plot(x, y)
  4. ◼ Các tham số plot(x,y,’linestye_marker_color’) linestye ‘-’ ‘ ‘ ‘:’ ‘-.’
  5. marker ‘+’ + ‘h’/‘hexagram’  ‘o’ ^ ‘*’ * v ‘.’ . > ‘s’/‘square’  < ‘d’/‘diamond’  ‘p’/‘pentagram’ 
  6. color ‘r’ (red) ‘k’(black) ‘w’(white) ‘y’(yellow) ‘c’(cyan) ‘b’(blue) ‘g’(green) ‘m’(magenta)
  7. Ví dụ x = 0:pi/20:2*pi; plot(x, sin(x),’-.*r’); hold on plot(x, sin(x – pi/2),’ om’); plot(x, sin(x – pi), ‘:bs’); hold off
  8. 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.
  9. Ví dụ x = -pi:pi/10:pi; y = tan(sin(x)) - sin(tan(x)); plot(x,y,'—rs','LineWidth', 2,'MarkerEdgecolor', 'k','MarkerFacecolor', 'g', 'Markersize',10)
  10. Lệnh hold on Khi muốn vẽ thêm đồ thị trên đồ thị hiện có,ta sử dụng lệnh hold on. Để tắt chế độ này,sử dụng hold off. ◼ Ví dụ x = 0:pi/10:2*pi; plot(x, sin(x),'->r') hold on t =0:1:7; plot(t, 2*t,'-*b');
  11. Xác định tọa độ ◼ Lệnh axis axis([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/off
  12. 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]);
  13. Xác định tọa độ • xticks và yticks ◼ Ví 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'})
  14. 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’])
  15. Chú thích trên đồ thị ➢Các lệnh ◼ xlabel; ylabel ◼ title ◼ legend ◼ text; gtext;
  16. 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 on plot(t, sin(t),’or’)
  17. plotyy - Vẽ đồ thị trên 2 trục tọa độ khác nhau t=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')
  18. 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; subplot(2,2,3) [x,y] = meshgrid(t); z = sin(x).*cos(y); subplot(2,2,1) plot(t,z) plot(sin(t),cos(t)) axis([0 2*pi -1 1]) subplot(2,2,4) axis equal z = (sin(x).^2)-(cos(y).^2); subplot(2,2,2) plot(t,z) z = sin(x)+cos(y); axis([0 2*pi -1 1]) plot(t,z) axis([0 2*pi -2 2])
  19. TRONG KHÔNG GIAN 3 CHIỀU ➢ plot3(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ệnh meshgrid(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);
  20. ◼ Vẽ mặt: z ( x , y ) = x 2 ye −− xy 22 với − 44 x và − 44 y . [x,y]=meshgrid([-4:0.1:4]); z=x.*x.*y.*exp(-x.^2-y.^2); plot3(x,y,z)
  21. Dùng lệnh mesh(z)
  22. ◼ Ví dụ Vẽ mặt z=sin( y22 − x ) − c os( y − x ) với xy,  0,  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 off
  23. Một số lệnh vẽ đồ thị trong 3 - D ◼ plot3 ◼ contour / contourf / contour3 ◼ mesh / meshc / meshz ◼ surf / surfc ◼ waterfall ◼ bar3 / bar3h ◼ pie3 / fill3 ◼ comet3 / scatter3 / stem3
  24. In và xuất đồ thị ◼ Dùng lệnh print -dtiff -r200 mygraph.tiff print –deps2 mygraph.eps
  25. ◼ Sử dụng Plotting Tools
  26. Cá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’
  27. 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)')
  28. ◼ 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 pie3
  29. Ví 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])
  30. ◼ Đồ 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');
  31. 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 on area(x,profits,'FaceColor',[.9.85.7],'EdgeColor','y', 'LineWidth',2) hold off set(gca,'XTick',[90:94]) set(gca,'Layer','top') gtext('\leftarrow Sales') gtext('Profits') gtext('Expenses') xlabel('Years','FontSize',14) ylabel('Expenses + Profits = Sales in 1,000''s','FontSize',14)
  32. ◼ Đồ 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 thang ◼ Ví 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'')')
  33. ◼ Trong 3-D t = 0:.1:10; s = 0.1+i; y = exp(-s*t); stem3(real(y), imag(y),t) hold on plot3(real(y), imag(y),t,'r') hold off view(-39.5,62) xlabel('Real') ylabel('Imaginary') zlabel('Magnitude')
  34. ◼ Dạng bậc thang alpha = 0.01; beta = 0.5;t = 0:10; f = exp(-alpha*t).*sin(beta*t); stairs(t,f) hold on; plot(t,f,' *'); hold off label = '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])