MATLAB Programming Exercises - Prof. Trần, Exercises of Digital Systems Design

A collection of matlab programming exercises from a university course at the dai hoc cong nghe sai gon (saigon university of technology). The exercises cover a wide range of topics in matlab, including basic programming constructs, mathematical operations, graphical plotting, and more. Step-by-step code examples and explanations, making it a valuable resource for students learning matlab. The exercises are organized into multiple sections, allowing learners to progress through the material at their own pace. Whether you're a beginner or an experienced matlab user, this document can serve as a comprehensive guide to enhance your programming skills and deepen your understanding of the matlab environment.

Typology: Exercises

2019/2020

Uploaded on 05/15/2023

tran-anh-8
tran-anh-8 🇻🇳

1 document

1 / 147

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Bài tp MATLAB
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e
pf5f
pf60
pf61
pf62
pf63
pf64

Partial preview of the text

Download MATLAB Programming Exercises - Prof. Trần and more Exercises Digital Systems Design in PDF only on Docsity!

Bài tập MATLAB

ĐH CÔNG NGHӊ SÀI GÒN * BAØI TAÄP MATLAB

Baøi 1 : chöông trình baûng cöûu chöông

% 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: fact = 1; fact = facti; disp(fact); end case'2' for i = 1: fact = 2; fact = facti; disp(fact); end case'3' for i = 1: fact = 3; fact = facti; disp(fact); end case'4' for i = 1: fact = 4; fact = facti; disp(fact); end case'5' for i = 1: fact = 5; fact = fact*i; disp(fact); end

ĐH CÔNG NGHӊ SÀI GÒN * BAØI TAÄP MATLAB

fact = fact*i end % end of program

Baøi 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

Baøi 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

Baøi 5: caùc pheùp toaùn khoâng tieát tuïc

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

ĐH CÔNG NGHӊ SÀI GÒN * BAØI TAÄP MATLAB

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

Baøi 6 : chöông trình caùc pheùp toaùn coù tieáp tuïc

%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); case'' c = ab;

ĐH CÔNG NGHӊ SÀI GÒN * BAØI TAÄP MATLAB

sum = sum + nun; end disp(sum) end % end of program

Baøi 9 : tính toång caùc soá döông. nhaäp caùc soá döông , neáu ta nhaäp 1 soá aâm baát kyø thì chöông trình keát thuùc vaø cho keát quaû laø toång caûu caùc soá döông. % su dung lenh break sum = 0 while sum >= nun = input('Enter number:'); if nun <0 break; else sum = sum + nun; end end disp(sum) % end of program

Baøi 10 : chöông trình veõ ñöôøng troøn coù taâm 3,2 trong m-file

% ve duong tron tam 3, t = 0:pi/100:2pi; x = 3 + 3cos(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.

ĐH CÔNG NGHӊ SÀI GÒN * BAØI TAÄP MATLAB

Baøi 11 : Veõ ñoà thò hình sin(x) baùm ñoäng :

% 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.

Baøi 12 : Toïa ñoä cöïc Caùch 1: % toa do cuc su dung ham polar: % beginning of program theta=[0.0:0.1:10.0]; r1=theta; r2=5cos(theta)+5; h(1)=polar(theta,r1,'m'); set(h(1),'markersize',26); hold on h(2)=polar(theta,r2,'r'); set(h(2),'Linewidth',5); hold off title('polar(theta,r...)''');

ĐH CÔNG NGHӊ SÀI GÒN * BAØI TAÄP MATLAB

subplot(2,2,4); plot3(x,y,z,'linewidth',2.5,'linestyle','.','color','r'),title('hamplot3'); % ham ve do thi ba chieu z theo x va y co theo thay doi duoc cac thuoc tinh. % End of program.

Baøi 14 : Veõ ñoà thò sin vaø cos treân cuøng toïa ñ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; 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 ..

ĐH CÔNG NGHӊ SÀI GÒN * BAØI TAÄP MATLAB

Baøi 15 : Veõ ñoà thò chaïy haøm cos(x) baùm ñoäng : % 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.

Baøi 16 : Veõ ñöôøng troøn elip % ve duong tron nhap gia tri tu ban phim 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:2pi; x1 = a+rcos(t); y1 = b+rsin(t); x2 = a + r1cos(t); y2 = b + r2*sin(t); set(gcf,'defaulttextcolor','r'); h = plot(x1,y1,'r',x2,y2,'b'); set(h,'linewidth',2.25);

ĐH CÔNG NGHӊ SÀI GÒN * BAØI TAÄP MATLAB

Baøi 18 : Veõ ñoà thò daïng soùng theo töøng phaàn

% begining of program: k = 0; for n = 1:3: 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 : Chöông trình caáu truùc baûng ñieåm :

%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

ĐH CÔNG NGHӊ SÀI GÒN * BAØI TAÄP MATLAB

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 : enter name :Hung enter diem : continue thanh vien khac y/n :y enter STT : enter name :Binh enter diem : continue thanh vien khac y/n :y enter STT : enter name :Dat enter diem : continue thanh vien khac y/n :n

ans = 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'

ĐH CÔNG NGHӊ SÀI GÒN * BAØI TAÄP MATLAB

Baøi 21 : Veõ sin(x) vaø cos(x) treân 2 heä toïa ñoä xy nhöng cuøng trong 1 khung

%Begining of program x = -2pi:pi/100:2pi; y1 = sin(x); y2 = cos(x); subplot(2,1,1) % leänh cho pheùt taïo ra nhöõng khung chöùa plot(x,y1,'linewidth',1.0,'linestyle','o','color','m') grid xlabel('x') ylabel('y') title('y1 = sin(x)') legend ('sin(x)',-1) % leänh cho pheùp hieän thò teân vaø maøu cuûa haøm veõ. subplot(2,1,2) % leänh cho pheùt taïo ra nhöõng khung chöùa plot(x,y2,'linewidth',1.0,'linestyle','.','color','r') grid xlabel('x') ylabel('y') title('y2 = cos(x)') legend ('cos(x)',-1) % leänh cho pheùp hieän thò teân vaø maøu cuûa haøm 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.

ĐH CÔNG NGHӊ SÀI GÒN * BAØI TAÄP MATLAB

Baøi 22 : Veõ sin(x) , cos(x), sinc(x) vaø 1 - sin(x) treân 4 heä toïa ñoä xy nhöng cuøng trong 1 khung :

%Begining of program x = -2pi:pi/100:2pi; 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

ĐH CÔNG NGHӊ SÀI GÒN * BAØI TAÄP MATLAB

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

Baøi 24 : duøng leänh while ñeå tính toång chuoãi : s = x^n/n! Caùch 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; % ( kieåm tra I coù < = n hay ko? Neáu <= thì baét ñaàu thöïc hieän voøng laëp môùi). fact = 1; for k = 1:i; fact = fact*k; end s = s + (x^i)/fact; i = i + 1; end disp(s) % end of program

ĐH CÔNG NGHӊ SÀI GÒN * BAØI TAÄP MATLAB

Caùch 2: chæ duøng leänh while ñeå tính :

%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

Baøi 25 : caùch duøng leänh 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'); end % End of progam