Lệnh vẽ cơ bản 2D trong Matlab

Lệnh vẽ cơ bản 2D trong Matlab

x=[-10:1:10];

y=x.^2;

plot(x,y)
title(‘Do thi ham so y=x^2’)% dat ten cho do thi
xlabel(‘trucx’)
ylabel(‘trucy’)
grid on % ve duong luoi trong do thi
grid off % tat duong luoi
grid % chuyen doi qua lai giua on va off
legend (‘y=x^2’)% ghi chu giai cho ham so
gtext(‘Day la duong Parabol’)
gtext(‘Day la duong Parabol’);%chon 1 diem tren do thi
plot(x,y,’r’)% ve mau do
plot(x,y,’–r’)% ve mau do net dut
plot(x,y,’or’)% ve mau do cham tron cac diem
plot(x,y,’–or’)% ve mau do cham tron +net dut cac diem
plot(x,y,’–or’,’LineWidth’,2)% ve mau do cham tron +net dut cac diem tang do dam cua net ve len 2
figure
figure %tao them 1 cua so do thi de ve
x=[0:pi/10:8*pi];
length(x)% ktra do dai chuoi x

ans =

81

y1=sin(x);

plot(x,y1);
y2=cos(x);
plot(x,y1,’–r’,x,y2,’-.k’);%ve 2 do thi tren cung 1 bang
legend(‘sin x’,’cos x’)
%Cach 2 : de ve nhieu do thi tren cung 1 Figure
plot(x,y1);
hold on % Giu lai nhung gi da co tren Figure
plot(x,y2,’–g’)
% Cach dung ham subplot
figure
subplot(2,2,1)% 2 hang 2 cot goc phan tu th nhat
plot(x,y1)
subplot(2,2,2)% 2 hang 2 cot goc phan tu thu 2
plot(x,y2,’–g’)
y3=sin(x).^2;
subplot(2,2,3)
plot(x,y3,’k’);
y4=sin(x)+cos(x);
plot(x,y4,’m’);
plot(x,y3,’k’);
subplot(2,2,4)
plot(x,y4,’m’);
subplot(2,2,2)
title(‘y=cos x’)
subplot(2,2,1)
hold on
plot(x,y2,’–r’)
legend(‘sin x’,’cos x’)