Thuc Hanh MatLab Tong Hop

You might also like

You are on page 1of 78

BAI TAP MATLAB

Trang

http://www.ebook.edu.vn

Bai 1 : chng trnh bang cu chng


% lap bang cuu chuong tu 2 den 9
clear
con = 'y' ;
while con =='y'
fact = input('Enter number i:10 ing character:','s');
switch fact
case'1'
for i = 1:10
fact = 1;
fact = fact*i;
disp(fact);
end
case'2'
for i = 1:10
fact = 2;
fact = fact*i;
disp(fact);
end
case'3'
for i = 1:10
fact = 3;
fact = fact*i;
disp(fact);
end
case'4'
for i = 1:10
fact = 4;
fact = fact*i;
disp(fact);
end
case'5'
for i = 1:10
fact = 5;
fact = fact*i;
disp(fact);
end
SVTH : VNG VAN HUNG

CLASS : T307.3

BAI TAP MATLAB

Trang

http://www.ebook.edu.vn

case'6'
for i = 1:10
fact = 6;
fact = fact*i;
disp(fact);
end
case'7'
for i = 1:10
fact = 7;
fact = fact*i;
disp(fact);
end
case'8'
for i = 1:10
fact = 8;
fact = fact*i;
disp(fact);
end
case'9'
for i = 1:10
fact = 9;
fact = fact*i;
disp(fact);
end
otherwise
disp('wrong number');
end
con = input('continue y or n:','s');
end
% end of program

Bai 2 : chng trnh dung lenh For e tnh gia tha:


% dung lenh for de tinh gia thua
n = input('Enter positive integer:')
fact = 1
SVTH : VNG VAN HUNG

CLASS : T307.3

BAI TAP MATLAB

Trang

http://www.ebook.edu.vn

for i = 1:n;
fact = fact*i
end
% end of program

Bai 3 : dung lenh for tinh tong day so s=1+3+5+..+n


% dung lenh for tinh tong day so s=1+3+5+..+n
n = input('positive integer:')
s=0
for i = 1:2:n
s=s+i
end
% end of program

Bai 4: dung lenh for tinh tong day so s=1+3+5+..+n


% dung lenh for tinh tong day so s=1+3+5+..+n
n = input('positive integer:')
s=0
for i = n : -2 : 1
s=s+i
end
% end of program

Bai 5: cac phep toan khong tiet tuc


%Beginning of program
%lenh switch tinh +,-,*,/
a = input('Enter a:');
SVTH : VNG VAN HUNG

CLASS : T307.3

BAI TAP MATLAB

Trang

http://www.ebook.edu.vn

b = input('Enter b:');
operator = input('Enter operator =,-,*,/:','s');
switch operator
case'+'
c = a+b;
disp(c);
case'-'
c = a-b;
disp(c);
case'*'
c = a*b;
disp(c);
case'/'
c = a/b;
disp(c);
otherwise
disp('wrong operator');
end
% end of program

Bai 6 : chng trnh cac phep toan co tiep tuc


%Beginning of program
%lenh switch continue while tinh +,-,*,/
con = 'y'
while con =='y'
a = input( 'Enter a :' );
b = input( 'Enter b :' );
operator = input('Enter operator +,-,*, / :' , 's ');
switch operator
case'+'
c = a+b;
disp(c);
case'-'
c = a-b;
disp(c);
SVTH : VNG VAN HUNG

CLASS : T307.3

BAI TAP MATLAB

Trang

http://www.ebook.edu.vn

case'*'
c = a*b;
disp(c);
case'/'
c = a/b;
disp(c);
otherwise
disp('wrong operator')
end
con = input('continue y or n:','s');
end
% end of program

Bai 7 : tnh tong cac so dng:


% chuong trinh nhap day so duong va tinh tong cua day so duong
% begining of program
con ='y';
sum = 0;
while con =='y'
nun = input('Enter number:');
if nun < 0 continue;
else
sum = sum + nun;
con = input('continue y/n:','s');
end
end
disp(sum)
% end of program

Bai 8 : Cach s dung lenh Breack :


% su dung lenh break
sum = 0
while sum >=0
SVTH : VNG VAN HUNG

CLASS : T307.3

BAI TAP MATLAB

Trang

http://www.ebook.edu.vn

nun = input('Enter number:');


if nun <0 break;
else
sum = sum + nun;
end
disp(sum)
end
% end of program

Bai 9 : tnh tong cac so dng . nhap cac so dng , neu ta nhap 1 so am bat ky th
chng trnh ket thuc va cho ket qua la tong cau cac so dng .
% su dung lenh break
sum = 0
while sum >=0
nun = input('Enter number:');
if nun <0 break;
else
sum = sum + nun;
end
end
disp(sum)
% end of program

Bai 10 : chng trnh ve ng tron co tam 3,2 trong m-file


% ve duong tron tam 3,2
t = 0:pi/100:2*pi;
x = 3 + 3*cos(t);
y = 2 + 3*sin(t);
plot(x,y,'m','linewidth',1.5 ,'linestyle' , '+' ),grid
xlabel('x')
ylabel('y')
title('ve duong tron tam 3 2')
% End of program .

SVTH : VNG VAN HUNG

CLASS : T307.3

BAI TAP MATLAB

Trang

http://www.ebook.edu.vn

Bai 11 : Ve o th hnh sin(x) bam ong :


% ve do thi cua ham sin
clear
x=0;
y=sin(x);
h=0.01;
p=plot(x,y,'k','linestyle','.','EraseMode','none','MarkerSize',5);
axis([0 16 -1 1]),grid
for x=0:pi/100:5*pi
y=sin(x);
set(p,'XData',x,'YData',y);
drawnow
pause(0.01)
xlabel('x'),ylabel('Y')
title('ve sin(x) bam dong')
end
% En of program.

Bai 12 : Toa o cc
Cach 1:
% toa do cuc su dung ham polar:
% beginning of program
theta=[0.0:0.1:10.0];
r1=theta;
r2=5*cos(theta)+5;
h(1)=polar(theta,r1,'m');

SVTH : VNG VAN HUNG

CLASS : T307.3

BAI TAP MATLAB

Trang

http://www.ebook.edu.vn

set(h(1),'markersize',2*6);
hold on
h(2)=polar(theta,r2,'r');
set(h(2),'Linewidth',5);
hold off
title('polar(theta,r...)''');
legend(h,' r = \theta','r = 5cos(\theta)+ 5',-1);
% End of program

Cach 2 : toa do cuc su dung ham plot:


% Beginning of program:
theta=[0.0:0.1:10.0];
r1=theta;
r2=5*cos(theta)+5;
[x1,y1] = pol2cart(theta,r1);
[x2,y2] = pol2cart(theta,r2);
plot(x1,y1,'r','Markersize',2*6);
hold on
plot(x2,y2,'b','linewidth',5);
axis('equal')
title('polar using plot(x,y,...)''');
legend(' r = \theta','r = 5cos(\theta)+ 5',-1);
% End of program.

Bai 13 : Ve khong gian 3 D


% ve khong gian 3 D
[x,y]=meshgrid(-2:0.1:2);
z = sqrt(x.^2+y.^2);
subplot(2,2,1);
surf(x,y,z,'Edgecolor','r'),title('hamsurf');
% ham ve do thi ba chieu z theo x va y .
SVTH : VNG VAN HUNG

CLASS : T307.3

BAI TAP MATLAB

Trang

http://www.ebook.edu.vn

subplot(2,2,2);
surfc(x,y,z,'Edgecolor','y'),title('hamsurfc');
% ham ve do thi ba chieu z theo x va y co hinh chieu tren mat phang x y .
subplot(2,2,3);
mesh(x,y,z,'Edgecolor','m'),title('hammesh');
% ham ve do thi ba chieu z theo x va y nhung khong co to mau nen.
subplot(2,2,4);
plot3(x,y,z,'linewidth',2.5,'linestyle','.','color','r'),title('hamplot3');
% ham ve do thi ba chieu
theo x va y co theo thay
doi duoc cac thuoc tinh.
% End of program.

Bai 14 : Ve o th sin va cos tren cung toa o


%begin
x=0:pi/100:5*pi;
y1=sin(x);
y2=cos(x);
hold on
p=plot(x,y1,'b',x,y2,'r','linestyle','.','erasemode','none','markersize',10),grid;

SVTH : VNG VAN HUNG

CLASS : T307.3

BAI TAP MATLAB

Trang

http://www.ebook.edu.vn

xlabel('X'),ylabel('Y')
legend('sin(x)','cos(x)',-1)
title('graph sin(x) and cos(x)')
axis([0 16.5 -1.2 1.2])
%drawnow
%pause(0,0.1)
% end of program ..

Bai 15 : Ve o th chay ham cos(x) bam ong :


% ve do thi cua ham cos(x):
clear
x = 0;
y = cos(x);
h = 0.01;
p = plot(x,y,'m','linestyle','.','EraseMode','none','MarkerSize',5);
axis([0 16 -1.2 1.2]),grid
hold on
for x = 0:pi/100:5*pi
y = cos(x);
set(p,'Xdata',x,'Ydata',y)
xlabel('x'),ylabel('Y')
title('ve cos(x) bam dong')
drawnow
pause(0.01)
end
% End of program.

Bai 16 : Ve ng tron elip


% ve duong tron nhap gia tri tu ban phim
SVTH : VNG VAN HUNG

CLASS : T307.3

10

BAI TAP MATLAB

Trang

http://www.ebook.edu.vn

a = input('nhap a:');
b = input('nhap b:');
r = input('nhap ban kinh r:');
r1 = input('nhap truc nho r1:');
r2 = input('nhap truc lon r2:');
t = 0:pi/100:2*pi;
x1 = a+r*cos(t);
y1 = b+r*sin(t);
x2 = a + r1*cos(t);
y2 = b + r2*sin(t);
set(gcf,'defaulttextcolor','r');
h = plot(x1,y1,'r',x2,y2,'b');
set(h,'linewidth',2.25);
axis('square');
xlabel('X');
h = get(gca,'xlabel');
set(gca,'fontsize',10);
set(gca,'xtick',-4:10);
ylabel('Y');
h = get(gca,'ylabel');
set(gca,'fontsize',16);
set(gca,'xtick',-12:2:12);
title('graphs of(x-a)^2 + (y-b)^2 = r^2 and (x-a)^2/(r1)^2 + (y-b)^2/(r2)^2 = 1');
h = get(gca,'title');
set(h,'fontsize',12);
grid
% End of pro gram

Bai 17 : Ve sin va cos co gi han


% ve sin cos
h=figure('menubar','none','numbertitle','off','name','LuanVanTotNghiep','position',[100
100 600 600],'color','c')
axes('parent',h,'position',[0.1 0.1 0.8 0.8])
x=0:pi/100:2*pi
plot(x,sin(x),'b',x,cos(x),'r'),grid
SVTH : VNG VAN HUNG

CLASS : T307.3

11

BAI TAP MATLAB

Trang

http://www.ebook.edu.vn

uicontrol('parent',h,'Style','pushbutton','string','START','position',[5 5 50
50],'callback','move4')
xlabel('X')
ylabel('Y')
h0=uimenu('parent',h,'label','FILE')
uimenu('parent',h0,'label','RUN','callback','move4')
uimenu('parent',h0,'label','CLOSE','callback','CLOSE')
h1=uimenu('parent',h,'label','EDIT')
uimenu('parent',h1,'label','CLEAR','callback','CLC')
uimenu('parent',h1,'label','CLOSE','callback','CLOSE')
% end of program.

Bai 18 : Ve o th dang song theo tng phan


% begining of program:
k = 0;
for n = 1:3:10
n10 = n*10;
x = linspace(-2,2,n10);
y = x./(1 + x.^2);
k = k + 1;
subplot(2,2,k)
plot(x,y,'g','LineWidth',2.0,'LineStyle','.')
xlabel('X'),ylabel('Y')
title('y = x/(1+x^2)')
axis([-2 2 -0.8 0.8])
grid
pause(3);
end
% End of program.

Bai 19 : Chng trnh cau truc bang iem :


SVTH : VNG VAN HUNG

CLASS : T307.3

12

BAI TAP MATLAB

Trang

http://www.ebook.edu.vn

%chuong trinh cau truc bang diem


clear
con = 'y';
i = 1;
while con == 'y'
n = i;
lop(i).STT = input('enter STT :','s');
lop(i).name = input('enter name :','s');
lop(i).diem = input('enter diem :');
if lop(i).diem > 8 lop(i).hang ='Gioi';
elseif lop(i).diem <=8 && lop(i).diem >7 lop(i).hang = 'Kha';
elseif lop(i).diem <=7 && lop(i).diem >5 lop(i).hang = 'Trung Binh';
else lop(i).hang = 'Yeu';
end
i = i + 1;
con = input('continue thanh vien khac y/n :','s');
end
for i = 1:n;
lop(i)
end
% end of program
V Du Nh Sau :
enter STT :1
enter name :Hung
enter diem :9
continue thanh vien khac y/n :y
enter STT :2
enter name :Binh
enter diem :8
continue thanh vien khac y/n :y
enter STT :3
enter name :Dat
enter diem :8
continue thanh vien khac y/n :n
ans =
SVTH : VNG VAN HUNG

CLASS : T307.3

13

BAI TAP MATLAB

Trang

http://www.ebook.edu.vn

STT: '1'
name: 'Hung'
diem: 9
hang: 'Gioi'
ans =
STT: '2'
name: 'Binh'
diem: 8
hang: 'Kha'
ans =
STT: '3'
name: 'Dat'
diem: 8
hang: 'Kha'

Bai 20 : ve o th sin(x) va cos(x) tren cung 1 he truc toa o


%Begining of program
x = -2*pi:pi/100:5*pi;
y1 = sin(x);
y2 = cos(x);
hold on
plot(x,y1,'linewidth',1.0,'linestyle','.','color','r')
plot(x,y2,'linewidth',1.0,'linestyle','.','color','b')
grid
xlabel('x')
ylabel('y')
title('y1 = sin(x) and y2=cos(x)')
legend ('sin(x)','cos(x)',-1)
axis([ -5 5 -1 1])% cho phep tao gio han cua he truc xoy
axis on % cho phep bo he truc toa do xoy.
% lenh grid : tao mang luoi , lenh xlabel(x) : gian nhan truc x, lenh
% title( y = sin(x)): gang nhan cho do thi.
% lenh hold on : dung de giu 2 do thi tren cung 1 he toa do.
% End or program.
SVTH : VNG VAN HUNG

CLASS : T307.3

14

BAI TAP MATLAB

SVTH : VNG VAN HUNG

Trang

http://www.ebook.edu.vn

CLASS : T307.3

15

BAI TAP MATLAB

Trang

http://www.ebook.edu.vn

Bai 21 : Ve sin(x) va cos(x) tren 2 he toa o xy nhng cung trong 1 khung


%Begining of program
x = -2*pi:pi/100:2*pi;
y1 = sin(x);
y2 = cos(x);
subplot(2,1,1) % lenh cho phet tao ra nhng khung cha
plot(x,y1,'linewidth',1.0,'linestyle','o','color','m')
grid
xlabel('x')
ylabel('y')
title('y1 = sin(x)')
legend ('sin(x)',-1) % lenh cho phep hien th ten va mau cua ham ve.
subplot(2,1,2) % lenh cho phet tao ra nhng khung cha
plot(x,y2,'linewidth',1.0,'linestyle','.','color','r')
grid
xlabel('x')
ylabel('y')
title('y2 = cos(x)')
legend ('cos(x)',-1) % lenh cho phep hien th ten va mau cua ham ve.
% End of program
% lenh grid : tao mang luoi , lenh xlabel(x) : gian nhan truc x, lenh
% title( y = sin(x)): gang nhan cho do thi.

SVTH : VNG VAN HUNG

CLASS : T307.3

16

BAI TAP MATLAB

Trang

http://www.ebook.edu.vn

17

Bai 22 : Ve sin(x) , cos(x), sinc(x) va 1 - sin(x) tren 4 he toa o xy nhng cung trong
1 khung :
%Begining of program
x = -2*pi:pi/100:2*pi;
y1 = sin(x);
y2 = cos(x);
y3 = sinc(x);
y4 = 1 - sin(x);
subplot(2,2,1)
plot(x,y1,'linewidth',1.0,'linestyle','.','color','m')
grid
xlabel('x')
ylabel('y')
title('y1 = sin(x)')
legend ('sin(x)',-1)
subplot(2,2,2)
plot(x,y2,'linewidth',1.0,'linestyle','.','color','b')
grid
xlabel('x')
ylabel('y')
title('y2 = cos(x)')
legend ('cos(x)',-1)
subplot(2,2,3)
plot(x,y3,'linewidth',1.0,'linestyle','.','color','r')
grid
xlabel('x')
ylabel('y')
title('y3 = sinc(x)')
legend ('sinc(x)',-1)
subplot(2,2,4)
plot(x,y4,'linewidth',1.0,'linestyle','.','color','g')
grid

SVTH : VNG VAN HUNG

CLASS : T307.3

BAI TAP MATLAB

Trang

http://www.ebook.edu.vn

xlabel('x')
ylabel('y')
title('y4 = 1 - sin(x)')
legend ('1-sin(x)',-1)
% End of program

Bai 23: Ve sin(x) , cos(x) tren 2 he toa o xoy nhng cung trong 1 khung, 1 - sin(x)
va
1 - cos(x) tren 2 he toa o xoy nhng cung trong 1 khung
%Begining of program
x = -2*pi:pi/100:2*pi;
y1 = sin(x);
y2 = cos(x);
y3 = 1 - sin(x);
y4 = 1 - cos(x);
figure
subplot(2,2,1)
plot(x,y1,'linewidth',1.0,'linestyle','.','color','m')
grid
xlabel('x')
ylabel('y')
title('y1 = sin(x)')
subplot(2,2,2)
plot(x,y2,'linewidth',1.0,'linestyle','.','color','b')
grid
xlabel('x')
SVTH : VNG VAN HUNG

CLASS : T307.3

18

BAI TAP MATLAB

Trang

http://www.ebook.edu.vn

ylabel('y')
title('y2 = cos(x)')
figure
subplot(2,2,3)
plot(x,y3,'linewidth',1.0,'linestyle','.','color','r')
grid
xlabel('x')
ylabel('y')
title('y3 = 1 - sin(x)')
subplot(2,2,4)
plot(x,y4,'linewidth',1.0,'linestyle','.','color','g')
grid
xlabel('x')
ylabel('y')
title('y4 = 1 - cos(x)')
% End of program

Bai 24 : dung lenh while e tnh tong chuoi : s = x^n/n!


Cach 1:
%dung lenh while and for tinh tong s = x^n/n!
n = input('Enter n :');
x = input('Enter x :');
i = 1;
s = 0;
while i <= n; % ( kiem tra I co < = n hay ko? Neu <= th bat au thc hien vong lap
mi).
fact = 1;
for k = 1:i;
fact = fact*k;
end
s = s + (x^i)/fact;
i = i + 1;
end
SVTH : VNG VAN HUNG

CLASS : T307.3

19

BAI TAP MATLAB

Trang

http://www.ebook.edu.vn

disp(s)
% end of program
Cach 2: ch dung lenh while e tnh :
%dung lenh while and for tinh tong s = x^n/n!
n = input('Enter n :');
x = input('Enter x :');
i = 1;
k =1;
s = 0;
fact = 1;
while k <= n;
fact = fact*k;
s = s + (x^i)/fact;
k = k + 1;
i = i + 1;
end
disp(s)
% end of program

Bai 25 : cach dung lenh if elseif else :


% cach dung lech if elseif else
con = 'y'
while con == 'y'
n = input('Enter any number:');
if n > 0
disp('positive') ;
elseif n < 0
disp('Negative') ;
else
disp('Zero') ;
end
con =input('continue y/n:','s');
SVTH : VNG VAN HUNG

CLASS : T307.3

20

BAI TAP MATLAB

Trang

http://www.ebook.edu.vn

end
% End of progam

Bai 26 : Ve o th hnh ellips :


%Begining of program _ve do thi hing ellips:
a = input('Enter a :'); % nhap a = 5
b = input('Enter b :'); % nhap b = 4
c = input('Enter c :'); % nhap c = 4
d =input('Enter d :'); % nhap d = 8
t = 0:pi/100:2*pi;
x = a + c*cos(t);
y = b + d*sin(t);
set(gcf,'DefaultTextColor','g'); % mac nh van ban hien th tren o th la mau xanh la
cay. .
plot(x,y,'b'),xlabel('X'),ylabel('Y'),grid;
title('Do Thi Hinh Ellips'); % tao ten cho o th
%End of program.

Bai 27: : Ve o th hnh ellips co the thay oi net ve


%Begining of program _ve do thi hinh ellips:
a = input('Enter a :'); % nhap a = 5
b = input('Enter b :'); % nhap b = 4
c = input('Enter c :'); % nhap c = 4
d =input('Enter d :'); % nhap d = 8
t = 0:pi/100:2*pi;
x = a + c*cos(t);
y = b + d*sin(t);
set(gcf,' DefaultTextColor ' ,'b' );
h = plot( x,y,'b' ),xlabel('X'),ylabel('Y'),grid;
set(h,' linewidth ',3);% thay doi do rong cua net ve .
h = get( gca,'xlabel' );% chon doi tuong la nhan cua truc x .
set(h,' FontSize ' ,18);% tang co chu cua nhan truc x.
SVTH : VNG VAN HUNG

CLASS : T307.3

21

BAI TAP MATLAB

Trang

http://www.ebook.edu.vn

h = get(gca,'ylabel');
set( h,' FontSize ' ,18 );
title( ' Do Thi Hinh Ellips ' );
%End of program

Bai 28: cac lenh viet chng trnh giao dien nut ieu khien:
%chuong trinh chay cos va sin theo nut dieu khien
h=figure('position',[100 100 850 550],'menubar','none','numbertitle','off','name','NUT
DIEU KHIEN')
h0=uimenu('parent',h,'label','FILE')
% lenh : uimenu : tao he thong thuc don trong cua so gio dien.
%lenh: lable,FILE : cho phep tao ten nhan la FILE tren thanh menubar.
uimenu('parent',h0,'label','RUN','callback','chaySIN')
% tao con cua FILE co ten danh nhan la RUN
uimenu('parent',h0,'label','CLOSE','callback','close')
% tao con cua FILE co ten danh nhan la CLOSE
uicontrol('parent',h,'style','pushbutton','position',[120 20 100 30],'string','chaySIN',....
'backgroundcolor','g','foregroundcolor','r','visible','on','callback','chaySIN')
% Tao nut dieu khien chay chuong trinh la do thi hinh SIN duoc luu trong m-file.
% lenh:'parent',h, : la thuoc tinh cha do la cua so figure.
% lenh: 'style','pushbutton' : thuoc tinh tao nut dieu khien push.
% lenh: 'position' : la thuoc tinh vi tri dinh vi cua nut dieu khien.
% lenh: 'BackgroundColor' : la thuoc tinh tao mau nen cho cac nut dieu khien.
% lenh: 'ForegroundColor' : la thuoc tinh tao mau van ban cho cac nua dk.
% lenh : 'callback': la thuoc tinh goi cac chuong trinh ung dung voi gia
% tri la ten cua chuong trinh duoc luu duoi dang m-file.
% lenh: 'Visibel': la thuoc tinh hien thi voi 2 gia tri 'on' of 'off' cac
% nut dieu khien khi chon lenh nay.
h1=uimenu('parent',h,'label','EDIT')
%lenh cho phep tao ten nhan la EDIT tren thanh menubar.
uimenu('parent',h1,'label','CLEAR','callback','clc')
% tao con cua EDIT co ten danh nhan la 'clc'khi nhan no se lam sach cua so Window
uimenu('parent',h1,'label','CLOSE','callback','close')
SVTH : VNG VAN HUNG

CLASS : T307.3

22

BAI TAP MATLAB

Trang

http://www.ebook.edu.vn

% tao con cua EDIT co ten danh nhan la 'close'


uicontrol('parent',h,'style','pushbutton','position',[630 20 100 30],'string','chayCOS',.....
'backgroundcolor','y','foregroundcolor','r','callback','chayCos')
% Tao nut dieu khien chay chuong trinh la do thi hinh COSIN duoc luu trong
% m-file.
axes('parent',h,'position',[0.1 0.2 0.35 0.7])
%cho phep tao he truc xy cua do thi sin(x) o vi tri [0.1 0.2 0.35 0.7]
axes('parent',h,'position',[0.55 0.2 0.35 0.7])
%cho phep tao he truc xy cua do thi cos(x) o vi tri [0.55 0.2 0.35 0.7]
% End of program.

Bai 29: cac lenh cua ca so giao dien :


%Beginning of program : giao dien
h = figure('Position',[200 200 450 350],'MenuBar','none',...
'NumberTitle','off','color','y');
% lenh : 'color' ,'y' : toa thuoc tinh mau nen cua cua so giao dien
%la mau vang
axes('Parent',h,'Position',[0.1 0.2 0.8 0.6]);
h0 = uimenu('Parent',h,'Label','File');
uimenu('Parent',h0,'Label','Run','Callback','chaySIN');
uimenu('Parent',h0,'Label','Close','Callback','close');
h1 = uimenu('Parent',h,'Label','Edit');
uimenu('Parent',h1,'Label','Clear','Callback','clc');
uimenu('Parent',h1,'Label','Close','Callback','close');

SVTH : VNG VAN HUNG

CLASS : T307.3

23

BAI TAP MATLAB

Trang

http://www.ebook.edu.vn

24

%End of program.

Bai 30: Tao giao dien ngi s dung ve o th co the thay oi net ve :
Tao doi tuong do thi hinh sin nguoi su dung co the thay doi net ve bang cach click chuot
phai tren net ve :
% beginning of program
clear
h = figure('Position',[100 100 450 350],'MenuBar','none',...
'NumberTitle','off');
axes('Parent',h,'Position',[0.1 0.2 0.8 0.6]);
Cmenu = uicontextmenu;
x = 0:pi/100:2*pi;
hline = plot(x,sin(x),'LineWidth',2.6,'UIContextMenu',Cmenu);
grid
cb1 = ['set(hline,''LineStyle'',''--'')'];
cb2 = ['set(hline,''LineStyle'','':'')'];
cb3 = ['set(hline,''LineStyle'',''-'')'];
item1 = uimenu(Cmenu,'Label','dashed','Callback',cb1);
item2 = uimenu(Cmenu,'Label','dotted','Callback',cb2);
item3 = uimenu(Cmenu,'Label','solid','Callback',cb3);
% End of program.

SVTH : VNG VAN HUNG

CLASS : T307.3

BAI TAP MATLAB

SVTH : VNG VAN HUNG

Trang

http://www.ebook.edu.vn

CLASS : T307.3

25

BAI TAP MATLAB

Trang

http://www.ebook.edu.vn

Bai 31: chng trnh ve o th bam ong theo iem cua ellips :
%Beginning of program : ve do thi hinh elips bam dong.
t = 0;
x = 3 + 6*cos(t);
y = -2 + 9*sin(t);
h = 0.01
p = plot(x,y,'r','linestyle','o','erasemode','none','markersize',5),grid
axis([-4 10 -12 8])
hold on
for t = 0:pi/100:5*pi;
x = 3 + 6*cos(t);
y = -2 + 9*sin(t);
set(p,'xdata',x,'ydata',y)
drawnow
pause(0.005) % toc o cham ong la 0,005 giay.
xlabel('X'),ylabel('Y')
title('Graphs of (x-3)^2/36 + (y+2)^2/81 = 1')
end
%End of program

Bai 32 chng trnh toa giao dien ngi s dung vi nut ieu khien chay chng
trnh ve o th cau 31
%Beginning of program : giao dien
h = figure('Position',[200 200 500 500],'MenuBar','none',...
'NumberTitle','off','color','y');
% lenh : 'color' ,'y' : toa thuoc tinh mau nen cua cua so giao dien
%la mau vang
axes('Parent',h,'Position',[0.1 0.2 0.8 0.6]);
uicontrol('parent',h,'style','pushbutton','position',[120 20 100 30],'string','Chay Elips',....
'backgroundcolor','g','foregroundcolor','r','visible','on','callback','elips7')
h0 = uimenu('Parent',h,'Label','File');
uimenu('Parent',h0,'Label','Run','Callback','elips7');
uimenu('Parent',h0,'Label','Close','Callback','close');
SVTH : VNG VAN HUNG

CLASS : T307.3

26

BAI TAP MATLAB

Trang

http://www.ebook.edu.vn

h1 = uimenu('Parent',h,'Label','Edit');
uimenu('Parent',h1,'Label','Clear','Callback','clc');
uimenu('Parent',h1,'Label','Close','Callback','close');
% End of program.

Bai 33: chng trnh toa giao dien ngi s dung vi nut ieu khien chay chng
trnh ve o th sin(x) va cos(x) :
% chuong trinh tao nut dieu khien chay sin(x) va cos(x):
h=figure('position',[100 100 450
450],'menubar','none','numbertitle','off','name','chaysin(x)-cos(x)');
h0=uimenu('parent',h,'label','File');
uimenu('parent',h0,'label','runsin(x)','callback','chaysine');
uimenu('Parent',h0,'Label','Close','Callback','close');
uicontrol('parent',h,'style','pushbutton','string','chaysin(x)','backgroundcolor','r',...
'foregroundcolor','k','position',[80 170 110 30],'callback','chaysine');
axes('parent',h,'position',[0.1 0.5 0.4 0.4]);
h1 = uimenu('parent',h,'label','Edit');
uimenu('parent',h1,'label','runcos(x)','callback','chaycos');
uimenu('Parent',h1,'Label','Close','Callback','close');
uimenu('parent',h1,'label','claer','callback','clc');
uicontrol('parent',h,'style','pushbutton','string','chaycos(x)','backgroundcolor','m',...
'foregroundcolor','k','position',[280 170 110 30],'callback','chaycos');
axes('parent',h,'position',[0.55 0.5 0.4 0.4]);
% End of program .

SVTH : VNG VAN HUNG

CLASS : T307.3

27

BAI TAP MATLAB

Trang

http://www.ebook.edu.vn

28

Bai 34: cac lenh cua ca so giao dien :


% cac lenh tao cua so giao dien :
% beginning of program
h = figure('position',[100 100 250 250],'menubar','none','numbertitle','off',...
'name','luan van tot nghiep','color','r')
% lenh: 'position',[100 100 250 250]: dinh vi tri cua cua so giao dien tren man hinh may
tinh.
% lenh:'menubar','none': tao thuoc tinh tat thanh thuc don menubar.
% lenh: 'numbertitle','off': tao thuoc tinh tat ten nhan number figure tren thanh menubar.
% lenh: 'h = figure': gan cua so giao dien figure cho bien h.
axes('parent',h,'box','off','linewidth',1.5,'TickDir','out','tickLength',[0.02 0.02],...
'Xgrid','on','Ygrid','on','visible','off','position',[0.1 0.4 0.5 0.4])
% lenh: axes: tao cua so he truc toa do xy trong cua so cha figure.
% lenh: 'Box' :mac dinh la mo khung cua he truc xy. Tat la off .
% lenh: 'linewidth',1.5 : tao do rong cua khung he truc xy.
% lenh: 'TickDir','out': toa thuoc tinh cac duong ganh so he truc xy nam phia ngoai.
% lenh: 'tickLength',[0.02 0.02]:tao thuoc tinh do dai net gach he truc toa do xy trong
khung cua figure.
% lenh: 'Xgrid','on','Ygrid','on': tao thuoc tinh duong ke luoi he truc xy.
% lenh: 'Visible','off' : la thuoc tinh lam tat cua so he truc toa do xy .
% lenh: 'posotion',[0.1 0.4 0.5 0.4]: da vi tri he truc xy trong khung giao dien figure.
% End of program.
SVTH : VNG VAN HUNG

CLASS : T307.3

BAI TAP MATLAB

Trang

http://www.ebook.edu.vn

Bai 35 : ve hnh tron va hnh elips tren cung 1 he truc toa o :


%begining
a1 = input('nhap a1 : ');% nhap a1 = 5.
b1 = input('nhap b1 : ');% nhap b1 = 4.
r = input('nhap r : ');% nhap r = 4.
a2 = input('nhap a2 : ');% nhap a2 = 3.
b2 = input('nhap b2 : ');% nhap b2 = -2.
r1 = input('nhap r1 : ');% nhap r1 = 7.
r2 = input('nhap r2 : ');% nhap r2 = 2.
t = 0:pi/100:2*pi ;
x1 = a1 + r*cos(t);
y1 = b1 + r*sin(t);
x2 = a2 + r1*cos(t);
y2 = b2 + r2*sin(t);
set(gcf,'defaulttextcolor','b')
% mac dimh van ban la mau xanh dam.
h = plot(x1,y1,'r',x2,y2,'b'),grid ;
set(h,'linewidth',3)
axis('square')
xlabel('X')
SVTH : VNG VAN HUNG

CLASS : T307.3

29

BAI TAP MATLAB

Trang

http://www.ebook.edu.vn

h1 = get(gca,'xlabel');
set(h1,'fontsize',10)
set(gca,'xtick',-8:10)
ylabel('Y')
h2 = get(gca,'ylabel');
set(h2,'fontsize',10)
set(gca,'ytick',-8:10)
title('ELIP & DUONG TRON')
h3 = get(gca,'title');
set(h3,'fontsize',15)
%end of program.

Bai 36 :
% beginning of program
k = 0;
for n = 1:3:10
n10 = n*10;
x = linspace(-2,2,n10);
y = x./(1 + x.^2);
k = k + 1;
subplot(2,2,k)
plot(x,y,'r','LineWidth',2.0,'LineStyle','.')
xlabel('X'),ylabel('Y')
title('y = x/(1+x^2)')
axis([-2 2 -0.8 0.8]),grid
pause(3);
end
%End of program

SVTH : VNG VAN HUNG

CLASS : T307.3

30

BAI TAP MATLAB

Trang

http://www.ebook.edu.vn

Bai 37 : Ve 2 hnh Elips tren cung 1 he truc toa o :


%Beginning of program
t = 0:pi/100:2*pi;
x1 = 3 + 6*cos(t);
y1 = -2 + 9*sin(t);
x2 = 7 + 2*cos(t);
y2 = 8 + 6*sin(t);
set(gcf,'DefaultTextColor','red') % tao mau mac dinh
h1 = plot(x1,y1, 'r',x2,y2,'b');
set(h1,'LineWidth',2.25);
axis('square');
xlabel('X')
h = get(gca,'xlabel');
set(h,'FontSize',16);
set(gca,'XTick',-4:10); % tao 15 diem tren truc hoanh x
ylabel('Y')
h = get(gca,'ylabel');
set(h,'FontSize',16)
set(gca,'YTICK',-12:2:12); % taqo 14 diem tren truc y
title('Graphs of (x-3)^2/36 + (y+2)^2/81 = 1 and (x-7)^2/4+(y-8)^2/36 = 1')
h = get(gca,'title');
set(h,'FontSize',12);
grid
%End of program

SVTH : VNG VAN HUNG

CLASS : T307.3

31

BAI TAP MATLAB

Trang

http://www.ebook.edu.vn

Bai 38: chng trnh toa giao dien ngi s dung vi nut ieu khien chay chng
trnh ve o th cau 33.
%chuong trinh chay cos va sin theo nut dieu khien
h=figure('position',[100 100 400 450],'menubar','none',...
'numbertitle','off','name','NUT DIEU KHIEN','color','y')
h0=uimenu('parent',h,'label','FILE')
%lenh cho phep tao ten nhan la FILE tren thanh menubar.
uimenu('parent',h0,'label','RUN','callback','elips7')
% tao con cua FILE co ten danh nhan la RUN
uimenu('parent',h0,'label','CLOSE','callback','close')
% tao con cua FILE co ten danh nhan la CLOSE
uicontrol('parent',h,'style','pushbutton','position',[120 20 100 30],'string','Chay Elips',....
'backgroundcolor','g','foregroundcolor','r','visible','on','callback','elips7')
% Tao nut dieu khien chay chuong trinh la do thi hinh SIN duoc luu trong m-file.
% lenh:'parent',h, : la thuoc tinh cha do la cua so figure.
% lenh: 'style','pushbutton' : thuoc tinh tao nut dieu khien push.
% lenh: 'position' : la thuoc tinh vi tri dinh vi cua nut dieu khien.
% lenh: 'BackgroundColor' : la thuoc tinh tao mau nen cho cac nut dieu khien.
% lenh: 'ForegroundColor' : la thuoc tinh tao mau van ban cho cac nua dk.
% lenh : 'callback': la thuoc tinh goi cac chuong trinh ung dung voi gia
% tri la ten cua chuong trinh duoc luu duoi dang m-file.
% lenh: 'Visibel': la thuoc tinh hien thi voi 2 gia tri 'on' of 'off' cac
% nut dieu khien khi chon lenh nay.
h1=uimenu('parent',h,'label','EDIT')
%lenh cho phep tao ten nhan la EDIT tren thanh menubar.
uimenu('parent',h1,'label','CLEAR','callback','clc')
% tao con cua EDIT co ten danh nhan la 'clc'khi nhan no se lam sach cua so Window
uimenu('parent',h1,'label','CLOSE','callback','close')
% tao con cua EDIT co ten danh nhan la 'close'
axes('parent',h,'position',[0.2 0.2 0.7 0.7])
SVTH : VNG VAN HUNG

CLASS : T307.3

32

BAI TAP MATLAB

Trang

http://www.ebook.edu.vn

%cho phep tao he truc xy cua do thi sin(x) o vi tri [0.2 0.2 0.7 0.7]
% End of program .

Bai 39 : Ve 3D bam ong cua ham : x = sin(2t) , y = cos(t) , z = t


% Beginning of program
clear
t = 0;
x = sin(2*t);
y = cos(t);
z=t
p = plot3(x,y,z,'.','EraseMode','none','MarkerSize',5);
axis([-1 1 -1 1 0 10]),grid
hold on
for t = 0:0.01:3*pi
x = sin(2*t);
y = cos(t);
z = t;
set(p,'XData',x,'YData',y,'Zdata',z)
drawnow
pause(0.01)
end
%end of program.
SVTH : VNG VAN HUNG

CLASS : T307.3

33

BAI TAP MATLAB

Trang

http://www.ebook.edu.vn

Bai 40 : Cho dy/dt = Ay, trong o A(y) = [-8/3 0 y(2);0 -10 10;-y(2) 28 -1].
Nghiem qu ao chuyen ong cua phng trnh vi phan nay co the c tm bang
phng phap Euler. Chng trnh sau la mot v du minh chng.
% beginning of program
clear
A = [-8/3 0 0;0 -10 10;0 28 -1];
y = [35 -10 -7]';
h = 0.01;
p = plot3(y(1),y(2),y(3),'.','EraseMode','none','MarkerSize',5);
axis([0 50 -25 25 -25 25]),grid
hold on
for i = 1:4000
A(1,3) = y(2);
A(3,1) = -y(2);
ydot = A*y;
y = y + h*ydot;
set(p,'XData',y(1),'YData',y(2),'ZData',y(3))
pause(0.01)
drawnow
i = i +1;
end
% End of program .

SVTH : VNG VAN HUNG

CLASS : T307.3

34

BAI TAP MATLAB

Trang

http://www.ebook.edu.vn

Bai 41: ve o th 3 chieu :


% Begining of program :
[x,y] = meshgrid(-1:0.05:1);
z = x.^2+y.^2;
subplot(2,2,1);
surf(x,y,z,'Edgecolor','r'),title('hamsurf');
subplot(2,2,2);
surfc(x,y,z,'Edgecolor','b'),title('hamsurfc');
subplot(2,2,3);
mesh(x,y,z,'Edgecolor','g'),title('hammesh');
subplot(2,2,4);
plot3(x,y,z,'linewidth',2.5,'linestyle','.','color','m'),title('hamplot3');
% End of program.

Bai 42 : ve o th 3 chieu bam ong theo iem :


% ve ba chieu bam dong theo diem
clear
z = 0;
x = sin(z);
y = cos(z);
SVTH : VNG VAN HUNG

CLASS : T307.3

35

BAI TAP MATLAB

Trang

http://www.ebook.edu.vn

h = 0.01
p = plot3(x,y,z,'o','EraseMode','none','MarkerSize',5,'color','m');
axis([-1 1 -1 1 0 40]),grid
hold on
for z = 0:pi/100:10*pi
x = sin(z);
y = cos(z);
set(p,'XData',x,'YData',y,'Zdata',z)
drawnow
pause(0.01)
end
% end of program

Bai 43 : Ve o th sin(x) va cos(x) chay bam ong tren cung he truc x y :


% ve do thi chay sin(x) va cos(x) tren cung he truc xy:
clear
x = 0;
y1 = sin(x);
y2 = cos(x);
hold on
p1 = plot(x,y1,'.','EraseMode','none','MarkerSize',5,'color','m');
p2 = plot(x,y2,'.','EraseMode','none','MarkerSize',5','color','g');
axis([0 16.5 -1.2 1.2]),grid
hold on
for x = 0:pi/100:5*pi;
y1 = sin(x);
y2 = cos(x);
set(p1,'XData',x,'YData',y1)
set(p2,'XData',x,'YData',y2)
xlabel('X'),ylabel('Y')
title('ve do thi sin(x) va cos(x)chay theo diem tren cung he truc xy')
drawnow
pause(0.01)
end
%end of program.
SVTH : VNG VAN HUNG

CLASS : T307.3

36

BAI TAP MATLAB

SVTH : VNG VAN HUNG

Trang

http://www.ebook.edu.vn

CLASS : T307.3

37

BAI TAP MATLAB

Trang

http://www.ebook.edu.vn

Bai 44: Ve dang song hnh sin cua ien 3 pha :


% ve do thi chay sin(x) va cos(x) tren cung he truc xy:
clear
x = 0;
y1 = sin(x);
y2 = sin(x-2*pi/3);
y3 = sin(x+2*pi/3);
hold on
p1 = plot(x,y1,'.','EraseMode','none','MarkerSize',5,'color','m');
p2 = plot(x,y2,'.','EraseMode','none','MarkerSize',5','color','g');
p3 = plot(x,y3,'.','EraseMode','none','MarkerSize',5','color','r');
axis([0 16.5 -1.2 1.2]),grid
hold on
for x = 0:pi/100:5*pi;
y1 = sin(x);
y2 = sin(x-2*pi/3);
y3 = sin(x+2*pi/3);
set(p1,'XData',x,'YData',y1)
set(p2,'XData',x,'YData',y2)
set(p3,'XData',x,'YData',y3)
xlabel('X'),ylabel('Y')
title('ve dang song hinh sin dien 3pha')
drawnow
pause(0.01)
end
%end of program.

SVTH : VNG VAN HUNG

CLASS : T307.3

38

BAI TAP MATLAB

Trang

http://www.ebook.edu.vn

Bai 45: Ve o th ng tron va elips cung tren 1 he truc xy va bam ong theo
iem ,co phng trnh sau: (x-1)^2/36 + (y+1)^2/25 = 1 and (x-6)^2 + (y+3)^2 = 16 .
%Beginning of program : ve do thi hinh elips bam dong.
clear
t = 0;
x1 = 6 + 4*cos(t);
y1 = -3 + 4*sin(t);
x2 = 1 + 6*cos(t);
y2 = -1 + 5*sin(t);
h = 0.01 ;
hold on
p1 = plot(x1,y1,'r','linestyle','.','erasemode','none','markersize',5)
p2 = plot(x2,y2,'b','linestyle','o','erasemode','none','markersize',5)
axis([-8 12 -12 8]),grid
hold on
for t = 0:pi/100:5*pi;
x1 = 6 + 4*cos(t);
y1 = -3 + 4*sin(t);
x2 = 1 + 6*cos(t);
y2 = -1 + 5*sin(t);
set(p1,'Xdata',x1,'Ydata',y1)
set(p2,'Xdata',x2,'Ydata',y2)
drawnow
pause(0.005)
xlabel('X'),ylabel('Y')
title('Ve (x-1)^2/36 + (y+1)^2/25 = 1 and (x-6)^2 + (y+3)^2 = 16')
end
%End of program

SVTH : VNG VAN HUNG

CLASS : T307.3

39

BAI TAP MATLAB

Trang

http://www.ebook.edu.vn

Bai 46: chng trnh toa giao dien ngi s dung vi nut ieu khien chay chng
trnh ve o th Elips tron cau 47:
%chuong trinh chay elips dtron theo nut dieu khien
h = figure('position',[100 100 400 450],'menubar','none',...
'numbertitle','off','name','NUT DIEU KHIEN','color','y')
h0 = uimenu('parent',h,'label','FILE')
%lenh cho phep tao ten nhan la FILE tren thanh menubar.
uimenu('parent',h0,'label','RUN','callback','elipsdtron2')
% tao con cua FILE co ten danh nhan la RUN
uimenu('parent',h0,'label','CLOSE','callback','close')
% tao con cua FILE co ten danh nhan la CLOSE
uicontrol('parent',h,'style','pushbutton','position',[120 20 100 30],'string','Chay ElipsDtron',....
'backgroundcolor','g','foregroundcolor','r','visible','on','callback','elipsdtron2')
% Tao nut dieu khien chay chuong trinh la do thi hinh Elipsdtron duoc luu trong m-file.
% lenh:'parent',h, : la thuoc tinh cha do la cua so figure.
% lenh: 'style','pushbutton' : thuoc tinh tao nut dieu khien push.
% lenh: 'position' : la thuoc tinh vi tri dinh vi cua nut dieu khien.
% lenh: 'BackgroundColor' : la thuoc tinh tao mau nen cho cac nut dieu khien.
% lenh: 'ForegroundColor' : la thuoc tinh tao mau van ban cho cac nua dk.
% lenh : 'callback': la thuoc tinh goi cac chuong trinh ung dung voi gia
% tri la ten cua chuong trinh duoc luu duoi dang m-file.
% lenh: 'Visibel': la thuoc tinh hien thi voi 2 gia tri 'on' of 'off' cac
% nut dieu khien khi chon lenh nay.
h1 = uimenu('parent',h,'label','EDIT')
%lenh cho phep tao ten nhan la EDIT tren thanh menubar.
uimenu('parent',h1,'label','CLEAR','callback','clc')
% tao con cua EDIT co ten danh nhan la 'clc'khi nhan no se lam sach cua so Window
uimenu('parent',h1,'label','CLOSE','callback','close')
% tao con cua EDIT co ten danh nhan la 'close'
axes('parent',h,'position',[0.2 0.2 0.7 0.7])
%cho phep tao he truc xy cua do thi sin(x) o vi tri [0.1 0.2 0.35 0.7]
% En of program.

SVTH : VNG VAN HUNG

CLASS : T307.3

40

BAI TAP MATLAB

Trang

http://www.ebook.edu.vn

Bai 47 : chuong trinh tinh tong day so : s = x/1 - x^2/2! - x^3/3! + x^4/4!...
% chuong trinh tinh tong day so : s = x/1 - x^2/2! - x^3/3! + x^4/4!...
clear
n = input('Enter n :');
x = input('Enter x :'); % cac gia tri cua x luon giong nhau.
i = 1;
count = 1;
sum = 0 ;
while i <= n
fact = 1;
for k = 1:i
fact = fact*k;
end
if count == 1
sum = sum + x^i/fact;
else
sum = sum - x^i/fact;
end
count = count + 1;
if count == 4
count = 1;
end
SVTH : VNG VAN HUNG

CLASS : T307.3

41

BAI TAP MATLAB

Trang

http://www.ebook.edu.vn

i = i + 1;
end
sum
%End of program .
V du nhap gia tr x(i) nh sau:
Enter n :4
Enter x :2
sum =
-0.6667

Bai 48 : cau truc thiet b : tnh tong cac cot : Sl , p , Umin , Imin, Cosphi, Ksd:
% chong trinh tao mang cau truc
i = 1;
sum(1) = 0;
sum(2) = 0;
sum(3) = 0;
sum(4) = 0;
sum(5) = 0;
sum(6) = 0;
while i <= 3
Tb(i).ten = input('Nhap ten thiet bi :','s');
Tb(i).kyhieu = input('Nhap ky hieu thiet bi :','s');
Tb(i).Sl = input('Nhap so luong :');
Tb(i).p = input('Nhap cong suat :');
Tb(i).Umin = input('Nhap dien ap min :');
Tb(i).Imin = input('Nhap dong dien min :');
Tb(i).Cosphi = input('Nhap he so cosphi:');
Tb(i).Ksd = input('Nhap Ksd :');
sum(1) = sum(1) + Tb(i).Sl;
sum(2) = sum(2) + Tb(i).p;
sum(3) = sum(3) + Tb(i).Umin;
sum(4) = sum(4) + Tb(i).Imin;
sum(5) = sum(5) + Tb(i).Cosphi;
sum(6) = sum(6) + Tb(i).Ksd;
SVTH : VNG VAN HUNG

CLASS : T307.3

42

BAI TAP MATLAB

Trang

http://www.ebook.edu.vn

i = i + 1;
end
sum(1)
sum(2)
sum(3)
sum(4)
sum(5)
sum(6)
% End of program .

Bai 49: tnh tong S = tong [ x(i) * i! ] :


Trng hp gia tr x(i) khac nhau:
% tinh s = tong[x(i)*i!]
n = input('Enter nhap n :');
i = 1;
s = 0;
fact = 1;
k = 1;
while i <= n
x(i) = input('Nhap gia tri x(i) :');
fact = fact*k;
s = s + x(i)*fact;
i = i +1;
k = k + 1;
end
s
% End of program.
V du nhap gia tr x(i) nh sau:
Enter nhap n :4
Nhap gia tri x(i) :2
Nhap gia tri x(i) :3
Nhap gia tri x(i) :4
Nhap gia tri x(i) :5
SVTH : VNG VAN HUNG

CLASS : T307.3

43

BAI TAP MATLAB

Trang

http://www.ebook.edu.vn

s=
152
Trng hp gia tr x(i) giong nhau :
% tinh tong[x*i!]
n = input('Enter nhap n :');
x = input('Nhap gia tri x :');
i = 1;
s = 0;
k = 1;
while i <= n
fact = 1;
for k = 1:i
fact = fact*k;
end
s = s + x*fact;
i = i +1;
end
s
% End of program.
V Du Nh Sau :
Enter nhap n :4
Nhap gia tri x :2
s=
66

Bai 50 : Xac nh tam cua phu tai : X = tong [ x(i) * p(i) ] / tong [p(i)]
va Y = tong [ y(i) * p(i) ] / tong [p(i)]
% xac dinh tam cua phu tai cung cap dien :
i = 1;
SVTH : VNG VAN HUNG

CLASS : T307.3

44

BAI TAP MATLAB

Trang

http://www.ebook.edu.vn

x1 = 0;
y1 = 0;
p1 = 0;
n = input('Nhap n :');
while i <= n
x(i)= input('Nhap gia tri x(i) :');
y(i)= input('Nhap gia tri y(i) :');
p(i)= input('Nhap cong suat p(i) :');
x1 = x1 + x(i)*p(i);
y1 = y1 + y(i)*p(i);
p1 = p1 + p(i);
i = i + 1;
end
x = x1/p1
y = y1/p1
% End of program .
V du nh sau :
Nhap n :4
Nhap gia tri x(i) :1
Nhap gia tri y(i) :2
Nhap cong suat p(i) :20
Nhap gia tri x(i) :2
Nhap gia tri y(i) :3
Nhap cong suat p(i) :30
Nhap gia tri x(i) :3
Nhap gia tri y(i) :4
Nhap cong suat p(i) :20
Nhap gia tri x(i) :4
Nhap gia tri y(i) :1
Nhap cong suat p(i) :20
x=
2.4444
y=
2.5556

SVTH : VNG VAN HUNG

CLASS : T307.3

45

BAI TAP MATLAB

Trang

http://www.ebook.edu.vn

Bai 52 : chng trnh ve ng tron bam ong theo iem :


%Beginning of program : ve do thi hinh tron bam dong.
a = input('nhap a :');
b = input('nhap b :');
r = input('nhap r :');
t = 0;
x = a + r*cos(t);
y = b + r*sin(t);
h = 0.01;
p = plot(x,y,'r','linestyle','o','erasemode','none','markersize',5),grid
axis([-4 10 -12 8]);
hold on
for t = 0:pi/100:5*pi;
x = a + r*cos(t);
y = b + r*sin(t);
set(p,'Xdata',x,'Ydata',y)
drawnow
pause(0.01)
xlabel('X'),ylabel('Y')
title('Graphs of (x - a)^2 + (y - b)^2 = r^2')
end
%End of program

Bai 53 : tnh tong S = tong [ x(i) / i! ] :


Trng hp gia tr x(i) khac nhau:
% tinh tong[x(i)/i!]
n = input('Enter nhap n :');
i = 1;
s = 0;
fact = 1;
SVTH : VNG VAN HUNG

CLASS : T307.3

46

BAI TAP MATLAB

Trang

http://www.ebook.edu.vn

k = 1;
while i <= n
x(i) = input('Nhap gia tri x(i) :');
fact = fact*k;
s = s + x(i)/fact;
i = i +1;
k = k + 1;
end
s
% End of program.
V Du Nh :
Enter nhap n :3
Nhap gia tri x(i) :2
Nhap gia tri x(i) :3
Nhap gia tri x(i) :4
s=
4.1667
Trng hp gia tr x(i) giong nhau :
% tinh tong[x/i!]
n = input('Enter nhap n :');
x = input('Nhap gia tri x :');% Gia tri cua x bang nhau
i = 1;
s = 0;
k = 1;
while i <= n
fact = 1;
for k = 1:i
fact = fact*k;
end
s = s + x/fact;
i = i +1;
end
s
% End of program.
SVTH : VNG VAN HUNG

CLASS : T307.3

47

BAI TAP MATLAB

Trang

http://www.ebook.edu.vn

V Du Cu The :
Enter nhap n :4
Nhap gia tri x :2
s=
3.4167

Bai 54 : chuong trinh tinh tong day so : s = x/1 + x^2/2! - x^3/3! - x^4/4! +
x^5/ 5! + x^6/6! x^7/7! x^8/8!
% chuong trinh tinh tong day so : s = x/1 + x^2/2! - x^3/3! - x^4/4!...
clear
n = input('Enter n :');
x = input('Enter x :');
i = 1;
count = 1;
sum = 0 ;
while i <= n
fact = 1;
for k = 1:i
fact = fact*k;
end
if count == 1
sum = sum + x^i/fact;
elseif count == 2
sum = sum + x^i/fact;
else
sum = sum - x^i/fact;
end
count = count + 1;
if count == 5
count = 1 ;
end
i = i + 1;
end
sum
% End of program .
SVTH : VNG VAN HUNG

CLASS : T307.3

48

BAI TAP MATLAB

Trang

http://www.ebook.edu.vn

V Du Nh :
Enter n :8
Enter x :2
sum =
2.3238
Hoac
Enter n :4
Enter x :2
sum =
2.0000

Bai 55 : Chng trnh tnh tong day so :


S = 2+x^3/3! + x^6/6! + x^9/9! +...+ x^n/n!
% Begining of program : s = 2+x^3/3! + x^6/6! + x^9/9! +...+ x^n/n!
clear
n = input('Enter n :');
x = input('Enter x :');
i = 3;
count = 3;
sum = 2 ;
while i <= n
fact = 1;
for k = 1:i
fact = fact*k;
end
if count == 3
sum = sum + x^i/fact;
else
sum = sum ;
end
count = count + 1;
if count == 6
count = 3 ;
end
SVTH : VNG VAN HUNG

CLASS : T307.3

49

BAI TAP MATLAB

Trang

http://www.ebook.edu.vn

i = i + 1;
end
sum
%End of program .
V Du Nh :
Enter n :9
Enter x :2
sum =
3.4236

Bai 56 : Chng trnh tnh tong day so : voi gia tr x(i) bat ky :
%dung lenh while and for tinh tong s = x1/1! + x2^2/2! +.+ x^n/n!
n = input('Enter n :');
i = 1;
s = 0;
k =1;
fact = 1;
while i <= n;
x = input('Enter x :');
fact = fact*k;
s = s + (x^i)/fact;
k = k +1;
i = i + 1;
end
s
% end of program
V Du Nh :
Enter n :4
Enter x :3
Enter x :2
Enter x :5
Enter x :6
s=
79.8333
SVTH : VNG VAN HUNG

CLASS : T307.3

50

BAI TAP MATLAB

Trang

http://www.ebook.edu.vn

Bai 57 : Chng trnh cau truc bang iem :


%chuong trinh cau truc bang diem
clear
con = 'y';
i = 1;
while con == 'y'
n = i;
lop(i).STT = input('enter STT :','s');
lop(i).name = input('enter name :','s');
lop(i).diem = input('enter diem :');
if lop(i).diem >= 9 lop(i).hang ='Gioi';
elseif lop(i).diem < 9 && lop(i).diem >= 7 lop(i).hang = 'Kha';
elseif lop(i).diem < 7 && lop(i).diem >= 5 lop(i).hang = 'Trung Binh';
else lop(i).hang = 'Yeu';
end
i = i + 1;
con = input('continue thanh vien khac y/n :','s');
end
for i = 1:n;
lop(i)
end
% End of program
V Du Nh :
enter STT :1
enter name :Hung
enter diem :9
continue thanh vien khac y/n :y
enter STT :2
enter name :Anh
enter diem :7
continue thanh vien khac y/n :y
enter STT :3
enter name :Sang
SVTH : VNG VAN HUNG

CLASS : T307.3

51

BAI TAP MATLAB

Trang

http://www.ebook.edu.vn

enter diem :6
continue thanh vien khac y/n :y
enter STT :4
enter name :Giang
enter diem :4
continue thanh vien khac y/n :n
ans =
STT: '1'
name: 'Hung'
diem: 9
hang: 'Gioi'
ans =
STT: '2'
name: 'Anh'
diem: 7
hang: 'Kha'
ans =
STT: '3'
name: 'Sang'
diem: 6
hang: 'Trung Binh'
ans =
STT: '4'
name: 'Giang'
diem: 4
hang: 'Yeu'

Bai 58 : Chng trnh tnh tong : P = an*x^n + an-1 *x^(n-1) +..+ a1 *x + a0


% chuong trinh tinh tong :p = an*x^n + a(n-1)*x^(n-1) + ...+ a1*x + a0
clear
n = input('Enter n :');
x = input('Enter x :');
i = 0;
sum = 0 ;
SVTH : VNG VAN HUNG

CLASS : T307.3

52

BAI TAP MATLAB

Trang

http://www.ebook.edu.vn

while i <= n
k = n;
a.n = input('Enter a.n :');
sum = sum + a.n*x^k;
n = n -1;
end
sum
%End of program .
V Du Nh :
Enter n :5
Enter x :2
Enter a.n :5
Enter a.n :3
Enter a.n :4
Enter a.n :2
Enter a.n :1
Enter a.n :6
sum =
256

Bai 59 : : chuong trinh tinh tong day so : s = x/1 + x^2/2! - x^3/3! + x^4/4! x^5/ 5! + x^6/6! x^7/7! + x^8/8!
% chuong trinh tinh tong day so : s = x/1 + x^2/2! - x^3/3! + x^4/4! x^5/5! +
clear
n = input('Enter n :');
x = input('Enter x :');
i = 1;
count = 1;
sum = 0 ;
while i <= n
fact = 1;
for k = 1:i
fact = fact*k;
SVTH : VNG VAN HUNG

CLASS : T307.3

53

BAI TAP MATLAB

Trang

http://www.ebook.edu.vn

end
if count == 1
sum = sum + x^i/fact;
elseif count == 2
sum = sum + x^i/fact;
else
sum = sum - x^i/fact;
end
count = count + 1;
if count == 4
count = 2 ;
end
i = i + 1;
end
sum
%End of program .
V Du Nh :
Enter n :5
Enter x :2
sum =
3.0667

Bai 60 : Tnh tong sau: S = [tong x(i)]^2/[tong x(i)^2]


% tinh tong: S = [tong x(i)]^2/[tong x(i)^2]
n = input('Enter nhap n :');
i = 1;
s1 = 0;
s2 = 0;
while i <= n
x(i) = input('Nhap gia tri x(i) :');
s1 = s1 + x(i);
s2 = s2 + x(i)^2;
i = i +1;
end
SVTH : VNG VAN HUNG

CLASS : T307.3

54

BAI TAP MATLAB

Trang

http://www.ebook.edu.vn

s = s1^2/s2
% End of program.
V Du Nh sau :
Enter nhap n :5
Nhap gia tri x(i) :2
Nhap gia tri x(i) :3
Nhap gia tri x(i) :4
Nhap gia tri x(i) :5
Nhap gia tri x(i) :6
s=
4.4444

Bai 61: Ve o Th Noi Suy 2 Chieu :


% begining of program
years = 1950:10:1990;
service = 10:10:30;
wage = [150.697 199.592 187.625;197.323 195.072 250.287;....
203.212 179.092 322.767;226.505 153.706 426.730;249.633 120.281 598.234];
x = 10:1:30;
y = 1950:2:1990;
z = interp2(service,years,wage,15,1975);
[x,y] = meshgrid(10:1:30,1950:2:1990);
z = interp2(service,years,wage,x,y);
surf(x,y,z)
% End of program

SVTH : VNG VAN HUNG

CLASS : T307.3

55

BAI TAP MATLAB

Trang

http://www.ebook.edu.vn

Bai 62 : tnh tr cua bieu thc : T = tong(i=1:n) ki*x^i/i!


ki = 1 neu i khong phai la mot so nguyen to ( 2,4,6,8...)
ki = -1 neu i la mot so nguyen to (1,3,5,7,9....)
% Beginning of program
clear
n = input('Enter n :');
x = input('Enter x :');
i = 1;
count = 1;
sum = 0 ;
while i <= n
fact = 1;
for k = 1:i
fact = fact*k;
end
if count == 1
sum = sum - x^i/fact;
else
sum = sum + x^i/fact;
end
count = count + 1;
if count == 3
count = 1 ;
end
i = i + 1;
end
sum
%End of program .
V Du Nh :
Enter n :7
Enter x :2
sum =
-0.8698

SVTH : VNG VAN HUNG

CLASS : T307.3

56

BAI TAP MATLAB

Trang

http://www.ebook.edu.vn

Bai 63 : Chng trnh tnh tong cac tnh 2 so lien tiep t 1 en n :


s = 1*2 + 2*3 + 3*4 + 4*5 +....+ (n-1)*n
% Begining of program
n = input('Enter n:');
sum = 0;
for i = 1:n
sum = sum + (i-1)*i;
end
sum
% End of program
V Du Nh :
Enter n:8
sum =
168

SVTH : VNG VAN HUNG

CLASS : T307.3

57

BAI TAP MATLAB

Trang

http://www.ebook.edu.vn

58

3 Bai Thi Hoc K Va Roi


Bai 2 : Chng trnh mang cau truc va tinh cosphi trung binh va dong dien :
% chong trinh tao mang cau truc thiat bi:
n = input('enter n :');
i = 1;
sum(1) = 0;
sum(2) = 0;
while i <= n
TB(i).ten = input('Nhap ten thiet bi :','s');
S(i) = input('Nhap so luong :');
P(i) = input('Nhap cong suat :');
U(i) = input('Nhap dien ap :');
C(i)= input('Nhap cosphi :');
sum(1) = sum(1) + P(i)*S(i)*C(i);
sum(2) = sum(2) + P(i);
I(i) = P(i)/(sqrt(3)*U(i)*C(i))
i = i + 1;
end
Ctb = sum(1)/sum(2)
%End of program

Bai 3 : Chng trnh v th ni suy cosphi bam ong theo iem , thiet lap t le he
truc toa o , co anh nhan va tieu e :
% Begining of program
U =360:5:400;
C = 0.5:0.05:0.9;
x=0;
y = interp1(U,C,x,'spline');
hold on
p = plot(x,y,'o','Erasemode','none','Markersize',5,'color','r')
axis([360 400 0.5 1]),grid
hold on
for x = 360:1:400 ;
SVTH : VNG VAN HUNG

CLASS : T307.3

BAI TAP MATLAB

Trang

http://www.ebook.edu.vn

y = interp1(U,C,x,'spline');
set(p,'Xdata',x,'Ydata',y)
Xlabel('X'),Ylabel('Y')
title('Ve noi suy cosphi')
drawnow
pause(0.01)
end
% End of program

Bai 4 : Chng trnh giao dien co tieu e , he truc toa o , thc n FILE cha
lenh chay va nut ieu khien chng trnh ve o th o cau 68:
%chuong trinh tao giao dien su dung chay chuong trinh ve do thi noi suy cosphi
h = figure('position',[80 100 400 500],'menubar','none',...
'numbertitle','off','name','NUT DIEU KHIEN','color','y')
h0 = uimenu('parent',h,'label','FILE')
%lenh cho phep tao ten nhan la FILE tren thanh menubar.
uimenu('parent',h0,'label','RUN','callback','noisuy')
% tao con cua FILE co ten danh nhan la RUN
uimenu('parent',h0,'label','CLOSE','callback','close')
% tao con cua FILE co ten danh nhan la CLOSE
uicontrol('parent',h,'style','pushbutton','position',[120 20 100 30],....
'string','ve noi suy cosphi','backgroundcolor','g',...
'foregroundcolor','r','callback','noisuy')
axes('parent',h,'position',[0.2 0.2 0.7 0.7])
%End of program

SVTH : VNG VAN HUNG

CLASS : T307.3

59

BAI TAP MATLAB

Trang

http://www.ebook.edu.vn

60

BI TP THC HNH S 1
Thc hnh lnh, kiu d liu, ton t, truy cp, mng, hm trn ca
s lnh.
Lm quen vi mi trng ca s lm vic Matlab.

+ Command Window (Ca s lnh ).


+ Current Directory ( Ca s th mc hin hnh ).
+ Command History (Ca s lch s ).
+ Work Space ( Khng gian lm vic )
I .Cc chc nng trn ca s lnh:
Clc: lm sch mn hnh.
Clear: lm sch ca s workspace.
Help: ly thng tin tr gip.
Edit: son tho chng trnh.
Type: xem ni dung ca file.
, : dch chuyn con tr tri phi.
, : chn li nhng lnh thc hin.
II . Gn cc kiu d liu cho bin:
Thc hin cc dng lnh sau v kim tra kt qu.
>> a = 5
% gn d liu s nguyn
a=
SVTH : VNG VAN HUNG

CLASS : T307.3

BAI TAP MATLAB

Trang

http://www.ebook.edu.vn

61

5
>> b = 7.43
b=
7.4300

% gn d liu s thc

>> c = 3+4i % gn d liu s phc


c=
3.0000 + 4.0000i
>> d = pi
d=
3.1416

% gn s pi = 3.14159265 cho bin d

>> e = eps
% gn s chnh xc ca du chm ng 252
e=
2.2204e-016
>> f = realmin % gn s chm ng dng nh nht 21022
f=
2.2251e-308
>>g = realmax % gn s chm ng dng ln nht 21022
g=
1.7977e+308
>>h = nan
h=
NaN

% khong phai so

III .nh dng cc kiu d liu s vi lnh format : long, long e, short, short e, trong e l c
s 10.
Thc hin 2 dng lnh sau:
>> format long
% s chm c nh l 15 con s
>> A = pi
A=
3.14159265358979
Thc hin tip 2 dng lnh sau ri so snh kt qu.
>> format long e
% s du chm ng l 15 con s
>> A = pi
A=
3.141592653589793e+000
So sanh ket qua khi lan lt thc hien cac dong lenh tren cho thay : ket qua bang nhau.
Nhng c bieu dien khac nhau:
vi A = 3,14159265358973e+000 trong o e + 000 = 100 = 1
SVTH : VNG VAN HUNG

CLASS : T307.3

BAI TAP MATLAB

Trang

http://www.ebook.edu.vn

62

Tng t thc hin cc dng lnh sau.


>> format short
% s chm c nh l 5 con s ( mc nh )
>> A = pi
A=
3.1416
V
>> format short e
% s du chm ng l 5 con s
>> A = pi
A=
3.1416e+000
Tng t so sanh ket qua khi lan lt thc hien cac dong lenh tiep theo tren cho thay :
ket qua bang nhau. Nhng c bieu dien khac nhau:
vi A = 3,1416e+000 trong o e + 000 = 100 = 1

IV .Qui c bin v thc hin cc php ton trn ca s lnh.


Cng +, tr -, nhn *, chia phi /, chia tri \, ly tha ^.
u tin

Php ton

Tnh u tin

(,)

trong ra ngoi

tri qua phi

*,/,\

tri qua phi

+,-

tri qua phi

Tnh bng tay cc biu thc sau thc hin bng matlab v lu li kt qu.
a. 2 / 2 * 3
9 Tnh bang tay: 2/2*3 = (2/2)*3 = 3
9 Tnh bang matlab :
>> 2/2*3
ans =
3
b. 6 2 / 5 + 7 ^ 2 1
9 Tnh bang tay: 6 - 2/5+7^2-1 = 6 - 0,4 + 49 1 = 35,6
9 Tnh bang matlab :
>> 6-2/5+7^2-1
ans =
SVTH : VNG VAN HUNG

CLASS : T307.3

BAI TAP MATLAB

Trang

http://www.ebook.edu.vn

53.6000
c. 10 / 2 \ 5 3 + 2 * 4
9 Tnh bang tay:10/2\5 3 + 2*4 = 5/(10/2) 3 + 8 = 5 / 5 3 + 8 = 6
9 Tnh bang matlab :
>> 10/2\5-3+2*4
ans =
6
Dng matlab tnh cc biu thc sau v lu li kt qu:
3+ 4
a.
5+ 2
>> ( 3 + 4 )/(5+sqrt(2))
ans =
1.0913
b. 2 2
>> 2*pi^2
ans =
19.7392
c.

4.5

>> 4.5^(1/3)
ans =
1.6510
V .Lm vic vi mng trn ca s lnh.
1. Gn d liu nguyn, thc, phc cho mng mt chiu, hai chiu bng ch s.
- mng 1 chiu.
Thc hin cc dng lnh sau lu li kt qu .
>> X(3) = 4 % phn t th 3 ca mng l 4.
X=
0 0 4

>> X(2) = 1 + 3i
% phn t th 2 ca mng l 1 +3i
X=
0
1.0000 + 3.0000i 4.0000
- mng 2 chiu :
Thc hin cc dng lnh sau lu li kt qu.
>> T(1,1) = 1 % gn gi tr cho mng theo ch s ,
SVTH : VNG VAN HUNG

CLASS : T307.3

63

BAI TAP MATLAB

Trang

http://www.ebook.edu.vn

T=
1
>> T(1,2) = 46
T=
1 46
>> T(2,1) = 3 + 2i
T=
1.0000
46.0000
3.0000 + 2.0000i
0
>> T(2,2) = 4.34
T=
1.0000
46.0000
3.0000 + 2.0000i 4.3400
2. Gn d liu nguyn, thc, phc cho mng mt chiu, hai chiu bng ni dung.
- mng 1 chiu.
Thc hin cc dng lnh sau lu li kt qu:
>> x = [ 3 1 5 ]
% gn vevtor hng 3 1 5 cho bin x.
x=
3 1 5

>> y = [ 2.1 4.6 3.8 ]


y=
2.1000 4.6000 3.8000
>> z = [ 2+3i 7-4i 9+5i ]
z=
2.0000 + 3.0000i 7.0000 - 4.0000i 9.0000 + 5.0000i
- mng 2 chiu:
Thc hin cc dng lnh sau lu li kt qu.
>> t = [ 6 5 ; 5 3 ]
t=
6 5
5 3
>> u = [ 8.1 3.6 ; 3.5 7.2 ]
u=
8.1000 3.6000
3.5000 7.2000
>> v = [ 3+i 7-7i ; 8-4i 4+5i ]
SVTH : VNG VAN HUNG

CLASS : T307.3

64

BAI TAP MATLAB

Trang

http://www.ebook.edu.vn

65

v=
3.0000 + 1.0000i 7.0000 - 7.0000i
8.0000 - 4.0000i 4.0000 + 5.0000i
3. Truy cp mng mt chiu, hai chiu bng ch s.
A(i,j) truy cp phn t c ch s i,j
A(1: k,j) truy cp t phn t th nht n phn t th k ca ct j
A(i,1:k) truy cp t th 1 n phn t th k ca hng i
Trc tin ta gn ni dung cho mng.
>>z = [ 2 3.4 4+ 3i ; 2 3 + 2i 7.2]
z=
2.0000
3.4000
4.0000 + 3.0000i
2.0000
3.0000 + 2.0000i 7.2000

Truy cp phn t c ch s 2,1


>>z (2,1)
% truy cp phn t th 2 ca ct 1.
ans =
2
Truy cp t phn t th 1n phn t th 2 ca ct 1
>> z(1:2,1)
% truy cp phn t : t 1 n 2 ca ct 1.
ans =
2
2
Truy cp t phn t th 1 n phn t th 3 ca hng 2.
>> z(2,1:3) % truy cp phn t th 1 n 3 ca hng 2.
ans =
2.0000
3.0000 + 2.0000i 7.2000
VI .Thc hnh mt s hm vi mng trn ca s lnh.
sum, prod, diff, trace, diag, eye, ones, zeros, magic, randn, inv, eig . . .
sum(A) : to ra vector hng vi cc phn t l tng ca cc phn t ca cc ct tng ng.
prod(A) : to vector hng vi cc phn t l tch ca cc phn t ca cc ct tng ng.
diff(A) : tnh cc sai bit gia cc phn t k nhau ca ma trn A.
trace(A) : tnh vt ca ma trn A ( tng cc phn t trn ng cho).
eye(n) : to ra ma trn n v.
magic(n) : to ma trn vung vi tng cc phn t hng, ct v ng cho l bng nhau.
rand(n) : to ma trn vung vi cc phn t c phn b ngu nhin.

Thc hin cc lnh sau v lu li kt qu.


>> z = [ 3.4 4+3i ; 2 3+2i ] % gn ma trn cp 2x2 cho bin z
z=
3.4000
4.0000 + 3.0000i
SVTH : VNG VAN HUNG

CLASS : T307.3

BAI TAP MATLAB

Trang

2.0000

http://www.ebook.edu.vn

3.0000 + 2.0000i

>> I = sum(z) % tnh tng ma trn theo ct tng ng.


I=
5.4000
7.0000 + 5.0000i
>> J = prod(z)
% tnh tch cc ct tng ng
J=
6.8000
6.0000 +17.0000i
>> K = diff(z)
K=
-1.4000
-1.0000 - 1.0000i
>> L = trace(z)
L=
6.4000 + 2.0000i
>> P = eye(4)
P=
1 0 0
0 1 0
0 0 1
0 0 0

0
0
0
1

>> Q = magic(3)
Q=
8 1 6
3 5 7
4 9 2
>> M = rand(5)
M=
0.9501 0.7621
0.2311 0.4565
0.6068 0.0185
0.4860 0.8214
0.8913 0.4447

0.6154
0.7919
0.9218
0.7382
0.1763

0.4057
0.9355
0.9169
0.4103
0.8936

0.0579
0.3529
0.8132
0.0099
0.1389

1. Cho ma trn A = [2 4 1 ; 6 7 2 ; 3 5 9], vit lnh Matlab :


>> A = [ 2 4 1 ; 6 7 2 ; 3 5 9 ]
A=
2 4 1
6 7 2
3 5 9
SVTH : VNG VAN HUNG

CLASS : T307.3

66

BAI TAP MATLAB

Trang

http://www.ebook.edu.vn

a. To vector x l hng th nht ca A:


>> x = A(1,1:3) % truy cp phn t th 1 n 3 ca hng 1 ma trn A
x=
2 4 1
b. To ma trn y l hai hng cn li (cui) ca A:
>> y = A(2:3,1:3) % truy cp phn t th 1 n 3 ca hng 2 v hng 3
% ca ma trn A
y=
6
3

7
5

2
9

c. Tnh tng theo hng ma trn A:


>> (sum(A'))'
ans =
7
15
17
d. Tnh tng theo ct ma trn :
>> sum(A)
ans =
11 16 12

6
8
2. Cho ma trn C =
1

9
7
3
2

5
2
4
8

1
4

3
3
v D =
2
4

2
5

>> C = [6 9 5 1; 8 7 2 3; 1 3 4 4; 5 2 8 2 ]

8
7
, dng Matlab:
3

1
% gn cc gi tr ca ma trn
% 4x4 cho bin C theo ni dung.

C=
6

SVTH : VNG VAN HUNG

CLASS : T307.3

67

BAI TAP MATLAB

Trang

http://www.ebook.edu.vn

>> D = [4 8 ; 3 7 ; 2 3 ; 5 1] % gn cc gi tr ca ma trn
% 2x4 cho bin D theo ni dung.
D=
4

SVTH : VNG VAN HUNG

CLASS : T307.3

68

BAI TAP MATLAB

Trang

http://www.ebook.edu.vn

a. To ma trn E1 l 2 ct nm gia ca ma trn C s dng ton t :


>> E1 = C(1:4,2:3) % truy cp phn t th 2 n 3 ca
% hng 1 n 4 ca ma trn C
E1 =
9

b. To ma trn E2 t hng 1 v 2 v ct 2 v 3 ca ma trn C s dng ton t :


>> E2 = C(1:2,2:3)
E2 =
9

c. To ma trn E3 bng cch ghp 2 ma trn E1 v D vi nhau:


>> E3 = [E1,D]
E3 =
9

VII .Thc hnh cc php ton mng trn ca s lnh.


+ : cng ma trn.
- : tr ma trn.
* : nhn ma trn.
/ : chia phi ma trn.
\ : chia tri ma trn.
^ : tan t ly tha ma trn.
.* : tan t nhn hai phn t tng ng ca hai ma trn.
./ : chia phi hai phn t tng ng ca hai ma trn.
.\ : chia tri hai phn t tng ng ca hai ma trn.
.^ : tan t ly tha tng mi phn t ca ma trn.

SVTH : VNG VAN HUNG

CLASS : T307.3

69

BAI TAP MATLAB

Trang

http://www.ebook.edu.vn

Cho x = [1 4 8], y = [2 1 5] v A = [3 1 6 ; 5 2 7]
>> x = [ 1 4 8 ]
x=
1 4 8
>> y = [ 2 1 5 ]
y=
2 1 5
>> A = [ 3 1 6 ; 5 2 7 ]
A=
3 1 6
5 2 7
Thc hin cc php tnh sau:
a. x*2 + y*3:
>> x*2+y*3
ans =
8 11 31
b. x y.^3:
>> x - y.^3
ans =
-7 3 -117
c. A*3 /x
>> A*3/x
ans =
2.0370
2.5556
d. A.^2
>> A.^2
ans =
9 1 36
25 4 49
===================Ht Bi Tp Thc Hnh S 1 =================

SVTH : VNG VAN HUNG

CLASS : T307.3

70

BAI TAP MATLAB

Trang

http://www.ebook.edu.vn

71

BI TP THC HNH S 2
Thc hnh lp trnh vi m-file
Thc hnh cc lnh trn ca s son tho m-file.
File-New : to tp tin mi.
File-Open: m tp tin c sn.
File-Save: lu tp tin lnh a.
File-Save as lu tp tin ln a vi mt tn khc.
I. Lp trnh vi m-file.
S dng vng lp for, vng lp while, lnh r nhnh if, if-else, if-elseif-else, lnh kim tra
trng hp case, lnh break
1. Lp trnh vi Script m-file.
a. Chng trnh v ng trn tm (3,2) v bn knh 3.

% ve duong tron tam 3,2


t = 0:pi/100:2*pi;
x = 3 + 3*cos(t);
y = 2 + 3*sin(t);
plot(x,y,'b','linewidth',1.5 ,'linestyle' , '+' ),grid
xlabel('x')
ylabel('y')
title('ve duong tron tam 3 2')
% End of program .
% lnh linewidth : dng ci t rng nt v.
% lnh linestyle ci t kiu ca nt v.

Save li vi tn l Duongtron.m
Nhn F5 run hay trong ca s command window g lnh:
>>Duongtron
b. Sinh vin t vit 1 Script m-file c nhim v v 1 hnh Sin
% ve hinh sin
x = 0:pi/100:2*pi;
y = sin(x);
plot(x,y,'m','linewidth',1.5 ,'linestyle' , '+' ),grid
axis('square')
xlabel('x')
ylabel('y')
title('VE HINH SIN')
% to tiu cho th l VE HINH SIN
% End of program
SVTH : VNG VAN HUNG

CLASS : T307.3

BAI TAP MATLAB

Trang

http://www.ebook.edu.vn

72

2. Lp trnh vi function m-file.


a. Chng trnh gii pt bc 2.
% Function m-file
function [x1, x2] = quadratic(a,b,c)
x = roots([a b c]);
x1 = x(1)
x2 = x(2)
%End

Save li vi tn l quadratic.m , trong ca s command window g lnh


>> quadratic ( 1 ,2 ,-3)
% giai pt: x^2 + 2*x -3 = 0
x1 =
-3
x2 =
1
b. Sinh vin t vit 1 function m-file gii phng trnh bc 2 m khng dng hm roots m
matlab h tr.
% chuong trinh giai phuong trinh bac 2 khong dung ham roots
a = input('nhap he so a :');
b = input('nhap he so b :');
c = input('nhap he so c :');
radical = sqrt(b^2 - 4*a*c);
x1 = (-b+radical)/(2*a)
x2 = (-b-radical)/(2*a)
% End of program
Chy chng trnh cho kt qu sau :
% giai pt: a*x^2 + b*x +c = 0
SVTH : VNG VAN HUNG

CLASS : T307.3

BAI TAP MATLAB

Trang

http://www.ebook.edu.vn

73

nhap he so a :1
nhap he so b :2
nhap he so c :-3
x1 =
1
x2 =
-3
II. Thc hnh vi cc lnh nhp xut d liu input v disp trong Script v function m-file.
a. Thc hin li chng trnh gii pt bc 2 vi cc h s a, b, c nhp t bn phm.
% Script m-file
a = input ('nhap he so a:');
b = input ('nhap he so b:');
c = input ('nhap he so c:');
disp (' nghiem cua pt bac 2 la :' )
x = roots([a b c]);
x1 = x(1)
x2 = x(2)
% End of program

Save li vi tn l quadratic2.m , trong ca s command window g lnh:


>> quadratic2
nhap he so a:1
nhap he so b:3
nhap he so c:-4
nghiem cua pt bac 2 la :
x1 =
-4
x2 =
1
b. Sinh vin t thc hin chng trnh v ng cong parabol y=a*x^2 + b vi cc h s a,
b, nhp t keyboard:
Cch v 1:
% Begin of program
a = input ('nhap he so a:'); % nhp h s a t bn phm
b = input ('nhap he so b:'); % nhp h s b t bn phm
x = 0:0.01:1;
p = [ a 0 b]; % to ra a thc: y = a*x^2 + 0*x + b = a*x^2 + b
y = polyval(p,x); % xc nh y ng vi cc gi tr ca x.
plot(x,y,'b','linewidth',1.5 ,'linestyle' , '+' ),grid
SVTH : VNG VAN HUNG

CLASS : T307.3

BAI TAP MATLAB

Trang

http://www.ebook.edu.vn

74

xlabel('x')
ylabel('y')
title('ve duong cong parabol y = a*x^2 + b')
% End of program.
Cch v 2:

% Begin of program
a = input ('nhap he so a:'); % nhp h s a t bn phm
b = input ('nhap he so b:'); % nhp h s b t bn phm
x = 0:0.01:1;
y = a*x.^2 + b
plot(x,y,'b','linewidth',1.5 ,'linestyle' , '+' ),grid
xlabel('x')
ylabel('y')
title('ve duong cong parabol y = a*x^2 + b')
% End of program

c. Sinh vin t thc hin chng trnh tnh trung bnh cc s vi s lng cc s ( t hn 10
s) v gi tr cc s c nhp t bn phm v xut gi tr trung bnh cui cng ra mn hnh.
% Chuong trinh tinh trung binh :
n = input('Enter n :'); % nhp s lng cc s cn tnh trung bnh
i = 1;
% gn s 1 cho bin i
s = 0;
% gn s 0 cho bin s
while i <= n; % dng vng lp while , nu i <= n cho php vng lp mi
x = input('Enter x :');
% nhp gi tr x th nht
s=s+x;
% s = s c + vi gi tr x mi nhp
i = i + 1;
% tng gi tr bin i ln 1 n v
end
% t kha kt thc vng lp while
Tb = s/n % xut gi tr trung bnh cui cng ra mn hnh command windown
% end of program
SVTH : VNG VAN HUNG

CLASS : T307.3

BAI TAP MATLAB

Trang

http://www.ebook.edu.vn

75

Chy chng trnh trn cho ta kt qu sau :


Enter n :5
Enter x :3
Enter x :4
Enter x :5
Enter x :6
Enter x :7
Tb =
5
III. Thc hnh hm chnh, hm con .
To 1 hm trong c nhu hm con hm u tin l l hm chnh v phi c tn cng tn
vi m-file, cn cc hm con phi c tn khc vi m-file.
a. Thc hin on code sau :

% Function m-file
function [z,s] = main(x,y)
z = total(x,y)
s = subtract(x,y)
function z = total(x,y)
z = x + y;
function s = subtract(x,y)
s = x y;
%end of function
Save li vi tn l main.m , trong ca s command window g lnh
>> main(9,4)
z=
13
s=
5
>> main(9,6)
z=
15
s=
3

SVTH : VNG VAN HUNG

CLASS : T307.3

BAI TAP MATLAB

Trang

http://www.ebook.edu.vn

76

b. Sinh vin t to ra 1 Script m-file cho php nhp chiu di v chiu rng ca 1 hnh
ch nht t bn phm v xut gi tr di ng cho ra mn hnh ( phi s dng hm con
trong chng trnh).

Chng trnh thc hin yn cu :


% Begin of program
a = input('nhap gia tri a :');
b = input('nhap gia tri b :');
c = sqrt(a.^2 + b.^2)
% End of program.
V d :
nhap gia tri a :4
nhap gia tri b :3
c=
5
IV. Thc hnh cc hm x l chui.
Cc hm matlab h tr.

double: i chui sang m ASCII.


char: to mng k t t mng s.
strcmp: so snh chui.
strcat: ni chui.
num2str: i s sang chui.
str2num: i chui sang s.
int2str: i s nguyn sang chui.
a. Thc hin cc on code sau.

>> name = 'Truong Dai Hoc DL Cong Nghe Sai Gon'


name =
Truong Dai Hoc DL Cong Nghe Sai Gon
>> name(8:35)
% truy cp chui t k t 8 n k t 35.
ans =
Dai Hoc DL Cong Nghe Sai Gon
Tip tc thc hin.

>> text1='toi hoc tai' %


>> text= [text1 ' ' name] % kt hp 2 chui
>> text1='Toi hoc tai'
text1 =
SVTH : VNG VAN HUNG

CLASS : T307.3

BAI TAP MATLAB

Trang

http://www.ebook.edu.vn

Toi hoc tai


>> text= [text1 ' ' name]
text =
Toi hoc tai Truong Dai Hoc DL Cong Nghe Sai Gon
b. Thc hin cc on code sau

>> A='Dai Hoc Sai Gon'


>> B=double (A)
>> C=char (B)
>> A = 'Dai Hoc Sai Gon'
A=
Dai Hoc Sai Gon
>> B = double(A)
B=
Columns 1 through 12
68

97 105 32 72 111 99 32 83 97 105 32

Columns 13 through 15
71 111 110
>> C = char(B)
C=
Dai Hoc Sai Gon
c. thc hin cc on code v cho kt qu sau.
>> Tg = 2.2774;
>> xg = 144.6364;
>> disp(['time of flight:' num2str(Tg) 's'])
time of flight:2.2774s
>> disp(['distance traveled:' num2str(xg) 'ft'])
distance traveled:144.6364ft
V. Thc hnh mng cu trc trn ca s lnh.
SVTH : VNG VAN HUNG

CLASS : T307.3

77

BAI TAP MATLAB

Trang

http://www.ebook.edu.vn

To mng cu trc.

>> SinhVien(1).hovaten = 'Nguyen Thanh Liem';


>> SinhVien(1).diemtoan = 6;
>> SinhVien(1).diemly = 7;
>> SinhVien(2).hovaten = 'Nguyen Hoang Anh';
>> SinhVien(2).diemtoan = 7;
>> SinhVien(2).diemly = 8;
.
-

Truy cp mng cu trc.

>> SinhVien(1) % truy cp thng tin v thnh vin th 1 ca mng.


ans =
hovaten: 'Nguyen Thanh Liem'
diemtoan: 6
diemly: 7
>> SinhVien(2) % truy cp thng tin v thnh vin th 1 ca mng.
ans =
hovaten: 'Nguyen Hoang Anh'
diemtoan: 7
diemly: 8
>> SinhVien(1).diemtoan

% xem im ton ca thnh vin 1.

ans =
6
>> SinhVien(2).hovaten

% xem h v tn ca thanh vin 2.

ans =
Nguyen Hoang Anh
===================Ht Bi Tp Thc Hnh S 2 =================

SVTH : VNG VAN HUNG

CLASS : T307.3

78

You might also like