You are on page 1of 41

PVU

Matlab cho K s

Cc ni dung chnh

PVU

Matlab c
bn

i s tuyn
tnh

Gii tch hm
mt bin

C hc

PVU

Ni dung 1: Matlab c bn
1. Gii thiu Matlab
2. Php ton, bin, vector, ma trn
2.1 Bin
2.2 Php ton
2.3 Vector

2.4 Ma trn
3. Biu thc Logic

3.1 Cc ton t logic


3.2 Vect v biu thc logic
3.3 Cc hm logic: All, Any v Find

PVU

Ni dung 1: Matlab c bn
4. Lnh iu kin v vng lp
4.1 Lnh IF
4.2 Lnh FOR
4.3 Lnh WHILE
4.4 Lnh SWITCH . . .CASE . . .

4.5 Script v Hm
5. V th
5.1 V th trong 2-D
5.2 V th trong 3-D

Ni dung 2: i s tuyn tnh


PVU

1. Cc php ton ma trn, cc php bin i s cp


1.1 Cc php ton ma trn
1.2 Cc php bin i s cp

2. Ma trn nghch o, Phng trnh ma trn v H phng trnh tuyn tnh


2.1 Ma trn nghch o
2.2 Ma trn gi nghch o

2.3 Gii phng trnh ma trn


3. H phng trnh tuyn tnh
3.1 a v dng ma trn

3.2 S dng tnh ton symbolic


4. nh thc, gii h phng trnh tuyn tnh bng nh thc
4.1 nh thc
4.2 Gii h phng trnh tuyn tnh bng nh thc
5. a thc c trng, tr ring v vect ring

Ni dung 3: Gii tch hm mt bin


PVU

1 Cc php ton tp hp

1.1 nh ngha tp hp v cch khai bo tp hp trong Matlab


1.2 Cc php ton trong tp hp
2. Symbolic math c bn

2.1 Khi nim chung


2.2 Khi ng TOOLBOX
3. Cc bi ton dy s v chui s

3.1 Khi nim v dy s, chui s v cch khai bo trong Matlab


3.2 Mt s hm v x l dy s v chui s trong Matlab

Ni dung 3: Gii tch hm mt bin


PVU

4. Cc bi ton gii hn hm s v tnh lin tc ca hm s


4.1 Gii hn ca hm s
4.2 S lin tc ca hm s

5. Cc bi ton tch phn hm mt bin


5.1 Tch phn bt nh

5.2 Tch phn xc nh


5.3 Tch phn s
5.4 Cc hm trong Matlab dng cho bi ton vi phn hm mt bin

PVU

Ni dung 4: C hc
1 C hc
1.1 ng hc cht im
1.2 Sng c hc

PVU

1. Gii thiu Matlab


Matlab l mt ngn ng lp trnh cp cao c s dng rng ri trong mi trng hc
thut v cng ngh.

Matlab c xem l la chn u tin v c kh nng h tr ti u cho vic nghin cu


cng nh dy hc i vi cc mn ton hc, k thut v khoa hc.

Matlab c vit tt t MATrix LABoratory do mc ch ban u ca Matlab l xy


dng nn mt cng c h tr vic tnh ton cc ma trn mt cch d dng nht.

PVU

Giao din chnh ca Matlab

PVU

Thnh phn giao din

Current Folder: th hin


th mc ang lm vic

Workspace: cha danh


sch cc bin c khai
bo v s dng trong
chng trnh

Command history: cha danh sch cc


cu lnh c thc thi trong ca s dng
lnh (command window)

PVU

Mt s lnh thng dng


Clc: xa mn hnh
Who, whos: xem thng tin cc bin hin hnh
Clear bin, clear all: Xa cc bin

save, load: ghi v phc hi d liu (MAT)


Display(tn_bin) hin th kt qu khng c tn bin
Echo iu khin ca s lp li cc lnh ca script file

Input s dng du nhc a d liu vo


Keyboard trao iu khin tm thi cho bn phm
Pause dng li cho n khi ngi dng nhn mt phm bt k

Pause(n) dng li n giy


Waitforbuttonpress dng li cho n khi ngi dng nhn mt phm bt k.

PVU

2. Php ton, bin, vector, ma trn


2.1 Bin

Trong ngn ng lp trnh Matlab, mt bin (variable) c khai bo v khi to thng qua cu
lnh gn.
Tn bin: Tn bin l mt dy k t c bt u bng ch ci, c di ti a l 31 k t,
bao gm cc ch ci, ch s v du gch di ( _ ), c phn bit ch hoa v ch thng.
>> num = 98
num =
98
>> pi = 3.1415926535897931
pi =
3.1416
>> msg = Good morning

msg =
Good morning

PVU

2. Php ton, bin, vector, ma trn


2.2 Php ton

Matlab cn h tr mt s hm s hc n gin nh:


PVU

hm lm trn round()

Lm trn ln ceil ()
Lm trn xung floor(),
Ly phn d mod()

Tm c chung ln nht gcd()


Tm bi chung nh nht lcm()
Hm ly cn sqrt()
tan(x) tang x
sin(x) sin x
cos(x) cosin x
sign(x) Hm cho du ca x

rem(x,y) Phn d ca php chia x/y


log10(x) log10 ca x
log(x) Lnx

exp(x) e^x

Cc hm ton hc thng thng

PVU

real(x) Hm tr v phn thc ca x

imag(x) Hm tr v phn o ca s phc


abs(x) Tnh argument ( ln M) ca s phc x
angle(x) Tnh gc ca s phc
floor(x) Xp x m v cng
fix(x) Xp x khng
conj(x) S phc lin hp
ceil(x) Xp x dng v cng
atan(x,y) arctang ca phn thc ca x v y
atan(x) arctang x
asin(x) arcsin x
acos(x) arccos x

PVU

2.3 Vector

Vector l mt dng c bit ca ma trn c mt dng hoc mt ct.


khi to vector dng cha cc gi tr ri rc, cc phn t trong vector
phi nm trong cp ngoc vung ([]) v c ngn cch nhau bi khong

trng hoc du phy (,).


>> arr1 = [1 2 3]
arr1 =
123
>> arr2 = [0,-5]
arr2 =
0 -5
>> arr3 = [arr1 arr2]
arr3 =
1 2 3 0 -5

PVU

2.3 Vector

khi to vector dng cha cc gi tr lin tc (mc nh trong


Matlab l 1) hoc cch nhau mt khong gi tr nht nh (cn
gi l bc nhy), Matlab s dng du hai chm (:).
>> arr1 = 1:5

arr1 =
12345
>> arr2 = [1:0.5:2]

arr2 =
1.0000 1.5000 2.0000
>> arr3 = 10:-1:6
arr3 =
10 9 8 7 6

PVU

Mt s php ton trn Vector:


Trch phn t th i: X(i)
Trch nhiu phn t: X([danh sch v tr])
Xa mt phn t trong vector, chng ta s
gn phn t vi vector rng.

PVU

2.4 Ma trn
Ma trn i din cho mng nhiu chiu c nhiu dng v nhiu ct. Phng thc
khai bo v khi to ma trn tng t nh vector.Tuy nhin, kt thc mt dng
trong ma trn, chng ta s dng du chm phy (;).
>> mat = [1 2 3 ; 4,5,6 ; 7:2:11]
mat =
123
456
7 9 11

PVU

Mt s hm khi to cc ma trn c bit:


Ma trn khng: zeros(s dng, s ct)

Ma trn vung khng cp n: zeros(n)


Ma trn n v: eye(n)
Ma trn ng cho: diag([cc phn t trn ng cho chnh])
Ma trn thc ngu nhin trong khong [0,1]: rand(s dng, s ct) hoc
rand(n) (ma trn vung cp n)

Ma trn ton s mt: ones(s dng, s ct)


Ma trn vung mt cp n: ones(n)

PVU

Mt s php ton trn ma trn:


Trch phn t ti dng i ct j: A(i,j)
Trch nhiu phn t: A([danh sch cc dng], [danh sch cc ct])

Trch ng cho chnh ca ma trn: diag(A)


Trch tt c phn t ca ma trn: A(:)
Trch tt c phn t ti ct i: A(:,i)
Trch tt c phn t ti dng j: A(j,:)

Ch : Trong Matlab, ch s cui cng ca dng hay ct ca ma trn hoc


vector c th c thay th bi ch end.

PVU

Bi tp:
Cho x = [1 4 8], y = [2 1 5], v A = [2 7 9 7 ; 3 1 5 6 ; 8 1 2 5]. Xt xem

dng lnh no hp l, d on kt qu, gii thch v th li bng Matlab


a. [x; y]
b. [x; y]

c. A(:,[1 4])
d. A([2 3], [3 1])
e. A(:)

f. [A;A(end, :)]
g. A(1:3,:)
h. [A;A(1 : 2, :)]

PVU

3. Biu thc Logic


3.1 Cc ton t logic

Kt qu ca mt biu thc logic tr v 1 nu biu thc ng v 0 nu biu thc sai.


Nu phn t so snh l vect th kt qu s l mt vect cha cc gi tr 0 hoc 1.

PVU

V d 1. Thc hin cc cu lnh sau v nhn xt kt qu


1. r=1;
r<=0.5
2. r=1:5;
r<=3

3. a=1:5;
b=[0 2 4 5 6];
a==b

V d 2. Thc hin cc cu lnh sau v nhn xt kt qu


1. a = 1; b=2; c=3;
(b*b==4*a*c)& (a=0)
2. a=1:10
(a>=20) & (a<=20)
3. a=1;b=2;
(a=0)|(b=0)|((a==0) & (b==0))

PVU

3. Biu thc Logic


3.2 Vect v biu thc logic
Biu thc logic cho php truy xut mt cch linh hot n cc thnh phn ca mt
vect hay ma trn:
V d:
1. x = [-1 0 2 3 5 6 7 4 9]
2. x(x>0): xut nhng gi tr dng ca vect x.
3. x(x>2 & x <=5): xut nhng gi tr tha 2 < x 5.

4. Lnh x>2 cho kt qu l 0 0 0 1 1 1 1 1 1 l vect cha kt qu so snh tng phn t


tng ng ca x vi 2 v x(x>2) s xut ra cc gi tr ln hn 2.

PVU

3. Biu thc Logic


3.3 Cc hm logic: All, Any v Find
any: Kim tra xem c tn ti mt phn t no ca vect tha iu kin khng?
Nu c th cho kt qu l 1, ngc li l 0.
all: kim tra tt c cc phn t ca vect c tha iu kin khng? Kt qu l 1
nu ng.
find: tr v cc ch s ca mt vect tha iu kin logic no .
V d,
A = [1 2 4;4 5 6]
find(isprime(A)): xut ra cc v tr ca ma trn A c gi tr l s nguyn t.

PVU

4. Lnh iu kin v vng lp


4.1 Lnh IF
C php:

if Biu thc iu kin


Lnh thc thi
end

hoc
if Biu thc iu kin 1
Lnh thc thi nu k 1 ng

elseif Biu thc iu kin 2


Lnh thc thi nu k 2 ng
...

end

V d 1. Hon v x v y nu x > y

PVU

if x > y

temp = y;
y=x;
x = temp;
end

V d 2. Bin lun s nghim ca phng trnh ax2+bx+c = 0, (a # 0)


delt = b2 - 4*a*c;
if delt < 0

disp(PT Vo Nghiem);
elseif delt == 0
disp(PT co nghiem kep);
else
disp(PT co 2 nghiem phan biet);
end

PVU

4. Lnh iu kin v vng lp


4.2 Lnh FOR
C php:
for var = start : step : end
Lnh thc thi
end

Vng lp vi bin chy l var bt u t start v kt thc l end, mi bc lp s tng


mt bc nhy l step. step c th l dng (vng lp tng dn) hoc m (gim dn).
Nu khng ch ra step, bc nhy mc nh l 1.

V d 1. Xut cc gi tr t 1 n 5 ra mn hnh

PVU

for i=1:5, disp(i), end

Xut cc gi tr chn t 1 n 10 theo th t ngc


for i=10:-2:1, disp(i), end

V d 2. Tnh giai tha n! = 1 2 . . . (n 1) n


n = 10;
fact = 1;
for k=1:n
fact = k*fact;
end

V d 3. Tnh tch tt c cc phn t trong vect a = [1 2 3 -2 4]


prod = 1;
for i = 1:length(a), prod = prod*i, end
disp(prod);

PVU

4. Lnh iu kin v vng lp


4.3 Lnh WHILE
C php:
while Biu thc iu kin
Cc lnh thc thi
end
Cc lnh s c thc hin khi no biu thc iu kin cn ng.

V d Tm gi tr ln nht ca n tng 12 + 22 + . . . + n^2 b hn


100.
S = 1; n=1;
while (S + (n+1)2) <100
n = n + 1;
S = S + n2
end
[n, S]

PVU

4. Lnh iu kin v vng lp


S dng Break v Continue:
Break: Dng trong vng lp for hoc while, dng thot khi vng lp ti v tr
n xut hin. Nu c nhiu vng lp lng nhau th lnh break c tc dng nhy
sang vng lp k tip.
Continue: Dng trong vng lp for hoc while, lnh continue cho php b qua cc
cu lnh ng sau n v chuyn sang bc lp k tip trong cng mt vng lp.

PVU

4. Lnh iu kin v vng lp


4.4 Lnh SWITCH . . .CASE
C php:

switch Biu thc iu kin


case Gi tr th 1
Khi lnh 1
case Gi tr th 2
Khi lnh 2
...
otherwise
Khi lnh n
end

PVU

4. Lnh iu kin v vng lp


4.5 Script v Hm
Script: L cc dng lnh Matlab c cha trong mt file c phn m rng .m; file
script c th c son tho bng Matlab Editor hoc cc chng trnh son tho
khc. thc thi script ch cn gi tn file trong ca s dng lnh ca Matlab.
Hm: Cng l cc on lnh Matlab c son tho trong file .m, hm nhn cc tham
s truyn vo, x l v xut ra gi tr. Tn ca hm phi ging nh tn ca file .m,
trnh t tn hm trng vi cc hm c sn ca Matlab.
C php: function [Cc gi tr xut] = Tn_hm(Cc gi tr nhp)

V d 1. Hm tnh gi tr th n ca dy Fibonnaci
function f = Fib(n)
F = zeros(1,n+1);
F(2) = 1;
for i = 3:n+1
F(i) = F(i-1) + F(i-2);
end
f = F(n);
Lu tn file Fib.m.

V d 2. Hm tr v nhiu gi tr

PVU

function [a b] = maxmin(x)

% Ham tra ve gia tri lon nhat va nho nhat cua mang x
a = max(x);
b = min(x);

Lu tn file maxmin.m. Hai gi tr tr v cha trong mng [a b].

V d 3. Hm khng c gi tr tr v.
function circle(a,b,r)
% Ham ve duong tron tam (a,b), ban kinh r
t = 0:pi/20:2*pi;
x = a + r*sin(t);
y = b + r*cos(t);

plot(x,y);
axis equal;

PVU

BI TP
Bi 1. D on kt qu sau v th li bng Matlab
if n > 1
m = n+1
else
m=n-1

vi
a. n = 7, m = ? b. n = 0, m = ? c. n = -10, m = ?

Bi 2.
if T < 30
h = 2*T + 1
elseif T < 10
h=T-2
else
h=0
end
a. T = 50, h = ? b. T = 15, h = ? c. T = 0, h = ?

PVU

BI TP
Bi 3.
if 0 < x < 10
y = 4*x
elseif 10 < x < 40

y = 10*x
else
y = 500
end
a. x = -1, y = ? b. x = 5, y = ? c. x = 30, y = ?

Bi 4. Vit Script tnh hm sau


h(T) = T - 10 khi 0 < T < 100

= 0.45T + 900 khi T > 100


Th cc trng hp a. T = 5 b. T = 110.

PVU

BI TP
Bi 5. Cho vect x = [1 8 3 9 0 1], vit cc dng lnh
a. Tnh tng cc phn t. (So snh kt qu vi lnh sum)
b. Vit hm tnh tng dy con t 1 n j. (So snh kt qu vi lnh cumsum)

Bi 6. Vit hm tnh t hp n chp r theo cng thc sau:

Bi 7. To ma trn M N cc s ngu nhin (dng lnh rand). Chuyn cc gi tr


nh hn 0.2 thnh 0, cc gi tr ln hn hay bng 0.2 thnh 1.
Bi 8. To mt vec t ngu nhin gm 1.000.000 phn t tnh tng bnh phng
cc phn t s dng 2 cch (randi):
a. Dng php ton trn ma trn.
b. Vng lp for.

PVU

BI TP
Bi 9. To mt vec t c gi tr nguyn gm 100.000 phn t trong khong [0,
100], tm tt c nhng phn t chia ht cho 3 bng 2 cch:
a. Dng php ton trn ma trn.
b. Dng for v if.

Bi 10. Cho x = [4 1 6] v y = [6 2 7], tnh cc mng/vector/ma trn sau


a. aij = xiyj
b. bij = xi/yj
c. ci = xiyi, tnh tng cc phn t ca c.

d. dij = xi/(2 + xi + yj)

Bi 11. Cho mt vect bt k, v d x = [-4 0 5 -3 0 3 7 -1 6], vit script m


xem trong vec t c bao nhiu gi tr m, bao nhiu gi tr dng,v bao nhiu gi
tr bng 0.

PVU

BI TP
Bi 12. Vit script tnh trong mt vector cc s ngu nhin (s dng lnh rand):
a. C bao nhiu phn t ng trc mt phn t c gi tr nm trong khong 0.8 n 0.85.
b. C bao nhiu phn t trong khong [0.01, 0.5] ng trc gi tr trung bnh ca vector
.

Bi 13. Vit mt script yu cu nhp mt nhit Fahrenheit ri chuyn sang


Celcius tng ng. Script vn chy n khi khng nhp nhit na th thi. [S
dng hm isempty.]
C = (F-32)/1.8

Bi 14. Gi s ta u t vo mt qu tn dng mt s tin ban u l a, s tin


sinh li hng nm l 10% s tin vn tch ly. Hy tnh s nm khi m li nhun
thu c gp i tin vn ban u.