Matlab và ứng dụng - Chú thích đồ thị và vẽ biểu đồ trong Matlab

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’)

pdf16 trang | Chuyên mục: MATLAB | Chia sẻ: dkS00TYs | Lượt xem: 9318 | Lượt tải: 1download
Tóm tắt nội dung Matlab và ứng dụng - Chú thích đồ thị và vẽ biểu đồ trong Matlab, để xem tài liệu hoàn chỉnh bạn click vào nút "TẢI VỀ" ở trên
2/9/2010
1
Chú thích đồ thị và vẽ 
biểu đồ trong Matlab
Chú thích trên đồ thị
Các lệnh
 xlabel; ylabel
 title 
 legend
 text; gtext;
2/9/2010
2
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’)
xlabel
ylabel
title
text
legend
2/9/2010
3
plotyy - Vẽ đồ thị trên 2 tung độ 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) 
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 equal
subplot(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])
2/9/2010
4
Biểu đồ
 Biểu đồ dạng cột
bar(X,Y)
X = 1:M
Y : Ma trận có N hàng
Tùy chọn khác
barh / bar3 / bar3h
‘stacked’, ‘grouped’,’detached’
2/9/2010
5
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)')
2/9/2010
6
 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
2/9/2010
7
Ví dụ biểu đồ quạt
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])
2/9/2010
8
 Đồ 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 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) 
2/9/2010
9
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’])
2/9/2010
10
 Đồ 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'')')
2/9/2010
11
 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')
 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])
2/9/2010
12
ĐỒ THỊ 3D
 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);
2/9/2010
13
 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)
( )
2 22, x yz x y x ye− −= 4 4x− ≤ ≤ 4 4y− ≤ ≤
Dùng lệnh mesh(z)
2/9/2010
14
 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 off
2 2sin( ) os( ) z y x c y x= − − − [ ], 0,x y π∈
2/9/2010
15
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
In và xuất đồ thị
 Dùng lệnh
print -dtiff -r200 mygraph.tiff
print –deps2 mygraph.eps
…
2/9/2010
16
 Sử dụng Plotting Tools

File đính kèm:

  • pdfMatlab và ứng dụng - Chú thích đồ thị và vẽ biểu đồ trong Matlab.pdf