P. 1
Học matlab bằng ví dụ

Học matlab bằng ví dụ

|Views: 590|Likes:
Được xuất bản bởitranthevut
author : Trịnh Anh Ngọc
author : Trịnh Anh Ngọc

More info:

Published by: tranthevut on Apr 16, 2012
Bản quyền:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

09/03/2013

pdf

text

original

HOC MATtAß ßANG THl DÜ

Trònh Anh Ngoïc
8/5/2009
Hoïc MATLAB baèng thí duï I
Taøi lieäu naøy ñöôïc vieát vôùi muïc ñích höôùng daãn caùc sinh vieân hoïc nhanh veà Matlab.
Phaàn ñaàu taøi lieäu laø noäi dung baøi giaûng cuûa taùc giaû veà Matlab trong hoïc phaàn "Giaûi
phöông trình baèng maùy tính". Moät soá thí duï, chöông trình tính toaùn soá ñöôïc laáy töø baøi
giaûng "Giaûi tích soá 1" cho lôùp cöû nhaân taøi naêng, "Cô hoïc chaát loûng" cuõng cuûa taùc giaû.
Matlab laø phaàn meàm trôï giuùp tính toaùn soá vaø kyù hieäu, do ñoù taøi lieäu goàm hai phaàn: (1)
tính toaùn kyù hieäu, vaø (2) tính toaùn soá. Sau khi ñoïc xong taøi lieäu naøy, sinh vieân coù theå:
- Duøng Matlab giaûi caùc baøi toaùn giaûi tích, ñaïi soá vaø ñaïi soá tuyeán tính;
- Duøng Matlab ñeå vieát chöông trình tính toaùn cho caùc moân nhö Phöông phaùp phaàn
töû höõu haïn, Giaûi tích soá, Cô hoïc vaät raén bieán daïng, Cô hoïc chaát loûng . . .
1 Vai tro coa may tínb trong ngbicn coo va ong
dong toan boc
• Con¸ cu tro ¸iup tinh toan so va ky hieu.
\e tinh toan so: ¸iup mo phon¸ cac lai toan thuoc lanh vuc khoa hoc va ky
thuat, phan tich du lieu.
\e tinh toan ky hieu: cho phep thuc hien, kiem tra cac phep lien doi toan
hoc; du doan, thu n¸hiem cac du doan toan hoc; danh ¸ia thuc hanh cac ket qua ly
thuyet.
• Hien thi cac ket qua lan¸ do hoa.
2 MatIab - pban mcm toan boc
Matlal la mot n¸on n¸u thuc hien danh cho tinh toan, dac liet la cac tinh toan ky
thuat. \oi Matlal ta co the tinh toan, hình dun¸, va lap trình cho cac doi tuon¸
toan hoc.
Matlal la mot he thon¸ tuon¸ tac ma phan tu du lieu co lan cua no la man¸
(array). Lieu nay, the hien tron¸ ten ¸oi cua no (Matlal la viet tat cua Matrix
Iaboratory), cho phep ta thuc hien de dan¸ cac tinh toan ma tran va vecto thuon¸
¸ap tron¸ cac lai toan ky thuat.
He thon¸ Matlal ¸om nam phan chinh:
I) '¸on n¸u Matlal
Z) Moi truon¸ lam viec Matlal
J) Quan ly do hoa
+) 1hu vien cac ham toan hoc Matlal
5) Ciao dien chuon¸ trình un¸ dun¸ Matlal
1ron¸ tai lieu nay ta se lam quen voi cac phan I) - +). Ban doc nen tìm cac
Z Trònh Anh Ngoïc
tai lieu:
Getting Started with MATLAB, MathWorks, Inc., I99o.
Symbolic Math Toolbox User's Guide, MathWorks, Inc., I99o.
tron¸ tu sach cua MathWorks de doc them ve Matlal khi can.
3 toot qoa MatIab - cai nbìn dao
1ron¸ muc nay ta se luot qua mot von¸ Matlal de co mot cai nhìn khai quat ve tinh
nan¸ va nhun¸ dac diem dan¸ luu y cua no. Le hoc nhanh Matlal lan phai doc tai
lieu nay voi may tinh truoc mat. Hay thu thuc hien cac thi du duoc dua ra tron¸ tai
lieu. Quan sat ¨phan un¸¨ cua Matlal khi xu ly cac lenh dua vao. 1u dat ra cac thi
du de thu n¸hiem, doc ky cac don¸ canh lao, thon¸ lao loi neu co.
Hình I: Cua so Matlal.
◦ Le khoi don¸ Matlal ta chi can nhap kep (doulle click) vao lieu tuon¸ cua
Hoïc MATLAB baèng thí duï J
no, hoac vao menu start\All programs\Matlab 7.1 roi chon Matlab 7.1
1
.
Mot cua so co ten Matlab (hay Matlab command) xuat hien cun¸ voi dau nhac
lenh >>(hình I). Cac lenh cua Matlal duoc nhap vao tu day.
◦ Le thoat (quit) chi can nhap chuot vao dau X o ¸oc tren len phai cua so
Matlab.
◦ 1at ca cac doi tuon¸ dua vao Matlal phai duoc ¨khai lao¨ lan¸ cach xac
dinh ¨noi dun¸¨ cua no.
Vccto
◦ Le tao mot vecto, thi du a, tron¸ cua so Matlab command ta nhap vao
>> a = [1 2 3 4 5 6 9 8 7 6 5]
sau khi nhan enter Matlal tra ve:
a =
1 2 3 4 5 6 9 8 7 6 5
Chu y: cac thanh phan cua vecto nam ¸iua hai dau n¸oac vuon¸, cach nhau mot
khoan¸ tran¸. Dien n¸u ¨Matlal tra ve¨ se duoc hieu n¸am tron¸ cac thi du sau.
1ao vecto b voi cac thanh phan lay ¸ia tri n¸uyen tu O den IO
>> b = 0:10
b =
0 1 2 3 4 5 6 7 8 9 10
'eu muon cac thanh phan lien tiep cach nhau Z thì nhap vao
>> c = 0:2:10
c =
0 2 4 6 8 10
Kich thuoc (chieu dai) cua vecto duoc cho nho ham length,
>> dai=length(c)
dai=
6
◦ 1inh toan tren vecto cun¸ rat don ¸ian. Con¸ cac thanh phan cua vecto a
voi 3
>> a + 3
ans =
4 5 6 7 8 9 12 11 10
Chu y: cac lenh, cac lien dua vao duoc Matlal luu ¸iu tron¸ workspace (vun¸ lam
viec). Co the hình dun¸ workspace nhu la to ¸iay nhap ta dun¸ khi lam tinh, cac
phep tinh duoc viet tuan tu tu tren xuon¸ duoi, ta co the xem lai nhun¸ ket qua da
thuc hien, xoa nhun¸ lien hoac lieu thuc duoc dua vao (dun¸ lenh clear tenbien
hoac clear all - xoa tat ca). 'oi dun¸ cua workspace duoc hien thi tren cua
so Matlal ta co the dun¸ thanh truot len phai cua so Matlal de xem cac noi dun¸
cu(hình Z).
1ron¸ tinh toan tren, do ta khon¸ chi dinh phep ¸an, Matlal luu tru ket qua
1
1en Matlab 7.1 co the khac tuy thuoc phien lan Matlal ma lan dun¸.
+ Trònh Anh Ngoïc
Hình Z: 'oi dun¸ workspace duoc hien thi tren cua so Matlal.
tinh toan tron¸ mot lien tam thoi ¸oi la ¨ans¨.
\e mat toan hoc ta khon¸ the con¸ vecto voi mot so' 1ron¸ thi du tren Matlal
da mo ron¸ 3 ra thanh ma tran [3 3 3 3 3 3 3 3 3] co cun¸ do dai nhu vecto
a roi thuc hien phep con¸.
◦ 'eu can thuc hien lai mot lenh da duoc dua vao truoc do ta co the tai hien
lai lenh nay lan¸ cach nhan phim ↑ mot so lan thich hop ma khon¸ can phai ¸o lai
lenh.
1inh vecto d la ton¸ cua hai vecto a va b
>> d = a + b
d =
1 3 5 7 9 11 15 15 15 15 15
Chu y: muon con¸ hai vecto thì chun¸ phai co cun¸ do dai. lhep tru tren vecto
cun¸ duoc thuc hien tuon¸ tu.
Ma tran
◦ 'hap ma tran vao Matlal ¸ion¸ nhu nhap mot vecto, n¸oai tru moi don¸
cach nhau loi dau cham phay hoac enter (return):
Hoïc MATLAB baèng thí duï 5
>> B = [1 2 3 4; 5 6 7 8; 9 10 11 12]
hay
>> B = [1 2 3 4
5 6 7 8
9 10 11 12]
B =
1 2 3 4
5 6 7 8
9 10 11 12
Kich thuoc ma tran duoc cho lan¸ ham size,
>> size(B)
ans=
3 4
◦ Matlal co the thuc hieu nhieu phep tinh ma tran, duoi day la mot so phep
tinh thuon¸ dun¸ (lan doc tu dua ra thi du de thu n¸hiem), danh sach cac lenh duoc
hien thi khi dun¸ lenh help. 1u danh sach nay ta co the tra cuu cu phap lenh va
cac thon¸ tin lien quan.
lhep con¸ (tru) hai ma tran tuon¸ tu nhu truon¸ hop vecto. Lac liet, nhan ma
tran voi mot so co the viet theo thu tu nao cun¸ duoc. 1hi du, neu A la ma tran s
la mot so thì hai lenh s*A va A*s cho cun¸ mot ket qua.
Chuyen vi:
>> C = B ’
'eu B la ma tran phuc thì B ’ cho chuyen vi cua lien hop phuc cua B. 1ron¸
truon¸ hop muon lay chuyen vi cua B ta phai dun¸ ¨.'¨
>> D = B .’
'han hai ma tran:
>> D = A * B
'eu muon nhan cac phan tu tuon¸ un¸ cua hai ma tran co cun¸ kich thuoc ta
dun¸ ¨.*¨
>> G = E .* F
'eu E la ma tran vuon¸ thì phep nhan ma tran E voi chinh no n lan co the
thuc hien lan¸ phep lay luy thua. 1hi du:
>> E

3
'eu muon luy thua tun¸ phan tu cua ma tran ta dun¸ ¨.

¨. 1hi du:
>> E .

3
'¸hich dao ma tran vuon¸:
>> inv(E)
Cac ¸ia tri rien¸:
>> eig(E)
La thuc dac trun¸:
>> poly(E)
o Trònh Anh Ngoïc
Ham so
◦ Matlal co mot thu vien cac ham toan hoc rat phon¸ phu nhu : sin, cos,
log, exp, sqrt, . . . Cac so thuon¸ dun¸ nhu : π, i (hay j) =

−1 cun¸ duoc dinh
n¸hIa san tron¸ Matlal.
>> sin(pi/4)
ans =
0.7071
◦ Loi so cua ham co the la mot ma tran. 1hi du, x=[1 2; 3 4], lenh exp(x)
tra ve
ans =
2.7183 7.3891
20.0855 54.5982
do chinh la ma tran
_
exp(1) exp(2)
exp(3) exp(4)
_
.
◦ Le tìm hieu cach dun¸ mot ham Matlal lat ky chi can ¸o
>> help [ten ham]
◦ Matlal cun¸ cho phep n¸uoi su dun¸ tao cac ham rien¸ cho mình lan¸ lenh
function (se duoc ¸ioi thieu duoi day).
Ðo tb|
1ron¸ Matlal de ve do thi mot ham so ta dun¸ ham plot. 1hi du de ve do
thi ham sin(t), truoc het ta tao vecto thoi ¸ian t va roi tinh ¸ia tri ham sin tai
moi thoi diem
>> t = 0:0.25:7;
y =sin(t);
>> plot(t,y)
Chu y: ten ham (plot) viet lan¸ chu thuon¸, dau cham phay cuoi moi cau lenh chi
thi Matlal khon¸ the hien ket qua tinh toan ra man hình.
M-fiIc
M-file la cac file van lan (text) chua ma Matlal. 1en M-file co phan mo ron¸
¨.m¨. Co hai loai M-file:
◦ Script, khon¸ co doi so nhapxuat, ¸ion¸ nhu mot ¨kich lan¨ ¸hi cac lenh
tinh toan tuan tu ma n¸uoi su dun¸ yeu cau. Khi ¸o ten file o dau nhac lenh roi
nhan phim enter, Matlal se thuc hien cac lenh ¸hi tron¸ file, tren du lieu hien hanh
tron¸ vun¸ lam viec (workspace). 1ron¸ thuc hanh, de kiem soat du lieu tinh toan,
script file thuon¸ lat dau lan¸ lenh clear all xoa tat ca cac lien luu tru tron¸
workspace, va khoi tao lai cac lien ma script file can dun¸.
◦ lunction, co doi so nhapxuat. Loai file nay thuon¸ duoc dun¸ de dinh n¸hIa
ham (¸ion¸ nhu chuon¸ trình con). 1hi du M-file rank.m
Hoïc MATLAB baèng thí duï 7
Hình J: Lo thi ham y = sin(t) voi 0 ≤ t ≤ 7.
function r = rank(A,tol)
% RANK Matrix rank.
% RANK(A) provides an estimate of the number of linearly
% independent rows or columns of a matrix A.
% RANK(A,tol) is the number of singular values of A
% that are larger than tol.
% RANK(A) uses the default tol = max(size(A)) * norm(A) * eps.
s = svd(A);
if nargin = =1
tol = max(size(A)) * max(s) * eps;
end
r = sum(s > tol);
Chu y: function tron¸ don¸ thu nhat la tu khoa. Don¸ nay cho ten function va
thu tu cac doi so. 1iep theo don¸ dau la cac don¸ lình luan, lat dau lan¸ º, don¸
dau tien tron¸ chun¸ se duoc Matlal hien thi khi ta ¸o lenh help rank. lhan con
lai cua file la cac don¸ kha thi.
4 Gioi tbico vc SymboIic Matb TooIbox
Muc nay ¸ioi thieu cac net chinh cua Symlolic Math 1oollox, mot thanh phan cua
Matlal dam trach viec tinh toan ky hieu.
o Trònh Anh Ngoïc
4.1 Ðoi toong ky bico
Loi tuon¸ ky hieu (symlolic olject) hay sym la mot cau truc du lieu luu tru mot chuoi
(strin¸) lieu dien cua ky hieu. Loi tuon¸ ky hieu duoc dun¸ de lieu dien cac lien,
cac lieu thuc va cac ma tran ky hieu. Le tao cac lien va lieu thuc ky hieu ta dun¸
ham sym. 1hi du,
>> x = sym(’x’)
tao mot lien ky hieu x luu tru ky tu x va
>> a = sym(’alpha’)
tao mot lien ky hieu a luu tru chuoi cac ky tu alpha. 1hi du khac,
>> rho = sym(’(1 + sqrt(5))/2’)
tao mot lien ky hieu lieu dien ti le van¸ (¸olden ratio).
4.2 Cac pbcp tínb ky bico tboong dong
1a co the thuc hien nhieu phep tinh tren cac lien ky hieu. Duoi day la mot vai
phep tinh thuon¸ dun¸.
SÜßS - thay the ky hieu.
◦ subs(s) thay the tat ca cac lien tron¸ lieu thuc ky hieu s lan¸ cac ¸ia tri
nhan duoc tu ham ¸oi, hay vun¸ lam viec. 1hi du, ¸ia su a=980, C1=3 da ton tai
tron¸ vun¸ lam viec.
>> y=exp(-a*t)*C1;
>> subs(y)
tra ve ans=3*exp(-980*t)
◦ subs(s,new) thay the cac lien ky hieu tu do tron¸ s lan¸ new. 1hi du, s
la lieu thuc f=x

2
>> subs(s,2)
tra ve ans=4 (x duoc thay lan¸ 2 tron¸ lieu thuc s).
◦ subs(s,old,new) thay the old lan¸ new tron¸ lieu thuc s. Ò day old
la lien ky hieu, chuoi lieu dien mot ten lien hay chuoi dien ta mot lieu thuc; new
la mot lien ky hieu hoac lien so hoac lieu thuc. 1hi du, subs(a+b,a,4) tra ve
4+b (a duoc thay the lan¸ 4).
Lenh subs cun¸ cho phep thay the nhieu lien cun¸ mot luc. 1hi du,
subs(cos(a)+sin(b),{a,b},{sym(’alpha’),2})
tra ve cos(alpha)+sin(2) (a duoc thay the lan¸ chuoi alpha, b duoc thay the
lan¸ 2).
'eu old va new la cac vecto hay man¸ co cun¸ kich thuoc, thì moi phan tu
cua old duoc thay loi phan tu tuon¸ un¸ cua new. 'eu s va old la cac vo huon¸
con new la man¸ hay man¸ ma tran (cell array), thì cac vo huon¸ duoc mo ron¸ ra
de cho ket qua man¸. 1hi du, subs(exp(a*t),’a’,-[1 3; 4 2]) tra ve
[ exp(-t), exp(-3*t)]
Hoïc MATLAB baèng thí duï 9
[ exp(-4*t), exp(-2*t)]
1ron¸ thi du nay a duoc mo ron¸ ra thanh man¸ roi thay the cac phan tu tuon¸ un¸
cua no lan¸ cac phan tu tuon¸ un¸ cua man¸ -[1 3; 4 2]. \e mat toan hoc, cac
phep tinh duoc thuc hien nhu sau
exp
__
−1 −3
−4 −2
_
∗ t
_
= exp
__
−t −3 ∗ t
−4 ∗ t −2 ∗ t
__
=
_
exp(−t) exp(−3 ∗ t)
exp(−4 ∗ t) exp(−2 ∗ t)
_
'eu new la cac ma tran man¸ hay ma tran so, thì su thay the duoc thuc hien
tun¸ phan tu; n¸hIa la subs(x*y,{x,y},{A,B}) tra ve A.*B khi A, B la ma tran
cac so). 1hi du, subs(x*y,{x,y},{[0 1;-1 0],[1 -1;-2 1]}) tra ve
[ 0, -1]
[ 2, 0]
FXFAND - khai trien lieu thuc ky hieu.
expand(s) viet moi phan tu cua lieu thuc ky hieu s nhu la tich cac thua so
cua no. expand hay duoc dun¸ voi da thuc, n¸oai ra no cun¸ duoc dun¸ de khai
trien cac lieu thuc luon¸ ¸iac, ham mu va ham lo¸arit.
1hi du:
I) ((x-2)*(x-4)) tra ve x

2-6*x+8.
Z) expand(cos(x+y)) tra ve cos(x)*cos(y)-sin(x)*sin(y).
J) expand(exp((a+b)

2)) tra ve exp(a

2)*exp(a*b)

2*exp(b

2).
+) expand([sin(2*t), cos(2*t)])tra ve [2*sin(t)*cos(t), 2*cos(t)

2-
1].
SlMFtlFY - don ¸ian lieu thuc ky hieu.
simplify(s) don ¸ian moi phan tu cua ma tran ky hieu s.
1hi du:
I) simplify(sin(x)

2 + cos(x)

2) tra ve I.
Z) \oi rho la lien lieu dien ti le van¸ (thi du o tren).
>> f = rho

2-rho-1
f =
(1/2+1/2*5

(1/2))

2-3/2-1/2*5

(1/2)
>> simplify(f)
f =
0
J) simplify(exp(c*log(sqrt(alpha+beta))))tra ve (alpha+beta)

(1/2*c).
Chu y, lenh ¸an lieu thuc duoi day
>> f = sym(’a*x

2 + b*x + c’)
khon¸ tao cac lien tuon¸ un¸ voi cac tu a, b, c, va x cua lieu thuc. Le thiet lap
cac phep toan ky hieu (e.¸., tich phan, vi phan, thay the, etc.) tren f, ta can phai tao
IO Trònh Anh Ngoïc
cac lien mot cach tuon¸ minh
>> a = sym(’a’)
>> b = sym(’b’)
>> c = sym(’c’)
>> x = sym(’x’)
hay don ¸ian
>> syms a b c x
SlMFtF - tìm dan¸ don ¸ian nhat cua lieu thuc ky hieu hay ma tran.
simple(s) co ¸an¸ thu nhieu lien doi dai so khac nhau de don ¸ian s, roi
tra ve lieu thuc n¸an nhat.
Duoi day la ket qua tra ve cua simple(s) kem theo chi dinh phep lien doi
duoc dun¸.
s ket qua phuon¸ phap
cos(x)

2+sin(x)

2 1 simplify
2*cos(x)

2-sin(x)

2 3*cos(x)

2-1 simplify
cos(x)

2-sin(x)

2 cos(2*x) combine(trig)
cos(x)+(-sin(x)

2)

(1/2) cos(x)+i*sin(x) radsimp
cos(x)+i*sin(x) exp(i*x) convert(exp)
(x+1)*x*(x-1) x

3-x combine(trig)
x

3+3*x

2+3*x+1 (x+1)

3 factor
cos(3*acos(x)) 4*x

3-3*x expand
log(x) + log(y) log(x*y) combine
4.3 Mot so cacb dong kbac coa bam sym
a) Chuyen doi mot ma tran tu dan¸ so san¸ dan¸ ky hieu. Lenh A = hilb(3)
sinh ra ma tran Hillert JxJ
A =
1.0000 0.5000 0.3333
0.5000 0.3333 0.2500
0.3333 0.2500 0.2000
/p dun¸ sym cho /
>> A = sym(A)
ta duoc (chinh xac vo cun¸) dan¸ ky hieu cua ma tran Hillert JxJ
A =
[ 1, 1/2, 1/3 ]
[ 1/2, 1/3, 1/4 ]
[ 1/3, 1/4, 1/5 ]
l) Xay dun¸ cac lien thuc va phuc. Lenh sym cho phep chi dinh cac tinh chat
toan hoc cua lien ky hieu lan¸ cach dun¸ tuy chon ’real’. '¸hIa la, cac lenh
Hoïc MATLAB baèng thí duï II
>> x = sym(’x’,’real’);
>> y = sym(’y’,’real’);
hay hieu qua hon
>> syms x y real
tao cac lien ky hieu x va y co tinh chat toan hoc them vao la cac lien thuc. Lieu
nay co n¸hIa la lieu thuc
f = x

2 + y

2
la khon¸ am. Suy ra,
z = x + I*y
la mot (hình thuc) lien phuc va co the tinh toan nhu so phuc. Cac lenh conj(x),
conj(z), expand(z*conj(z)) tra lai x, x - i*y, x

2 + y

2 (lenh conj la
toan tu lien hop phuc). 'eu conj(x) == x tra lai 1, thì x la mot lien thuc. Le
xoa tinh chat ¨thuc¨ cua x, ta dun¸ lenh
>> syms x unreal
hay
>> x = sym(’x’,’unreal’)
>> clear x
khon¸ lam x tro thanh lien khon¸ thuc'
c) Dun¸ sym de truy xuat cac ham Maple. 1a co the truy xuat ham ¸iai thua
k! cua Maple, lan¸ cach dun¸ sym,
>> kfac = sym(’k!’).
Le tinh 6! hay n!, ¸o
>> syms k n
>> subs(kfac,k,6)
ans =
720
>> subs(kfac,k,n)
ans =
n!
d) 1ao cac ham truu tuon¸. Lenh
>> f = sym(’f(x)’)
tao lien ky hieu f tac don¸ ¸ion¸ nhu f(x) va co the duoc tinh toan lan¸ cac lenh
cua Symlolic Math 1oollox. 1hi du, xay dun¸ ti sai phan cap I, lenh
>> df = (subs(f,’x’,’x+h’) - f)/’h’
hay
>> syms x h
>> df = (subs(f,x,x+h)-f)/h
tra ve
df =
(f(x+h)-f(x))/h
e) 1ao cac ham toan hoc ky hieu
Cach I - dun¸ cac lieu thuc ky hieu. Day cac lenh
>> syms x y z
IZ Trònh Anh Ngoïc
>> r = sqrt(x

2 + y

2 + z

2)
>> t = atan(y/x)
>> f = sin(x*y)/(x*y)
sinh ra cac lieu thuc ky hieu r, t, va f. 1a co the dun¸ diff (dao ham), int (tich
phan), subs, va cac ham Symlolic Math 1oollox khac de tinh toan nhun¸ lieu thuc
nhu vay.
Cach Z - tao mot M-lile. Muon tao ham sinc (=sin(x)/x), ta tao mot M-
file tron¸ thu muc _sym.
function z = sinc(x)
% SINC The symbolic sinc function
% sin(x)/x. This function
% accepts a sym as the input argumen.
if is equal(x,sym(0))
z = 1;
else
z = sin(x)/x;
end
Ham plot duoc dun¸ de ve do thi cua cac ham duoc cho duoi dan¸ ¨so¨, tron¸
thi du ve do thi o tren, ham y=sin(t) ham duoc cho duoi dan¸ ¨so¨ nhu la day cac
diem hình hoc (t(i),y(i)) (i=1,2,...,29) duoc xac dinh loi hai vecto t va y.
Ham plot ve do thi nay lan¸ cach noi cac diem loi cac doan than¸. \oi cac ham
toan hoc duoc dinh n¸hIa lan¸ ky hieu Matlal cun¸ cap ham ezplot de ve do thi
cua chun¸,
>> syms x real
>> ezplot(sin(x))
cho ta do thi cua ham sin tron¸ khoan¸ mac nhien [−2π, 2π]. 'eu muon ve do thi
Hình +: Lo thi ham sin(x) dun¸ lenh ezplot(sin(x)).
tron¸ khoan¸ [0, 7] thì ta phai chi dinh ro
Hoïc MATLAB baèng thí duï IJ
>> ezplot(sin(x),[0 7])
Hình 5: Lo thi ham sin(x) dun¸ lenh ezplot(sin(x),[0 7]).
4.4 ßicn ky bico mac nbicn
Khi ta dua vao mot ham toan hoc lan¸ lieu thuc cua no, thi du
>> clear all
>> syms x n a t b
>> f = x

n
>> g = sin(a*t+b)
Matlal se tu don¸ nhan dan¸ dau la lien ky hieu. \a khi duoc yeu cau tinh dao
ham cua cac ham nay, khon¸ chi dinh lien doc lap, thì Matlal se tinh theo cac lien
ky hieu mac nhien. \oi cac ham tren chun¸ ta nhan duoc
n*x

(n-1)
a*cos(a*t+b)
Lam the nao de liet lien ma Symlolic Math 1oollox lay dao ham! Cau tra
loi la lenh findsym.
>> findsym(f,1)
tra ve
ans =
x
'eu tron¸ findsym van¸ mat doi so thu hai thì Matlal tra ve mot danh sach tat ca
cac lien
>> findsym(g)
ans =
a, b, t
I+ Trònh Anh Ngoïc
Quy tac: Bien ky hieu mac nhien tron¸ mot lieu thuc ky hieu la chu cai ¸an x nhat.
'eu co hai chu cai ¸an x nhu nhau, chu cai dun¸ sau tron¸ lan¸ chu cai duoc chon.
4.S Cac tbí do tínb toan ky bico
Bay ¸io chun¸ ta lat dau hoc cach dun¸ Matlal, cu the la Symlolic Math 1oollox,
de tinh toan ky hieu thon¸ qua cac thi du.
Tbí do 1. Dun¸ phuon¸ phap khu Causs ¸iai he phuon¸ trình dai so tuyen tinh
x + 2y + 4z = 31
5x + y + 2z = 29
3x −y + z = 10
Lua vao ma tran cac he so noi ron¸
>> clear all >> a=[1 2 4 31; 5 1 2 29; 3 -1 1 10]
a =
1 2 4 31
5 1 2 29
3 -1 1 10
Dun¸ don¸ I khu cac so han¸ len duoi a
11
>> a(2,:)=a(2,:)-5*a(1,:)
a =
1 2 4 31
0 -9 -18 -126
3 -1 1 10
>> a(3,:)=a(3,:)-3*a(1,:)
a =
1 2 4 31
0 -9 -18 -126
0 -7 -11 -83
Dun¸ don¸ Z khu cac so han¸ len duoi a
22
>> a(2,:)=a(2,:)/(-9)
a =
1 2 4 31
0 1 2 14
0 -7 -11 -83
>> a(3,:)=a(3,:)+7*a(2,:)
a =
1 2 4 31
Hoïc MATLAB baèng thí duï I5
0 1 2 14
0 0 3 15
Ban doc thuc hien not phan con lai (phep the n¸uoc).
Tbí do 2. Ciai va lien luan he phuon¸ trình dai so tuyen tinh
x + 2y + 2z = 0
−2x + (m− 2)y + (m− 5)z = 2
mx + y + (m+ 1)z = −2
Dun¸ quy tac Cramer
>> clear all
>> syms m
>> a=[1 2 2; -2 m-2 m-5; m 1 m+1];
>> b=[0; 2; -2];
1inh cac dinh thuc co lan
>> d1=det(a)
d =
m

2-4*m+3
d=factor(d1)
d =
(m-1)*(m-3)
1hay cot I cua a lan¸ b. Le tranh thay doi ta dua vao ma tran ad=a
>> ad=a;
>> ad(:,1)=b;
>> d1=det(ad)
d1 =
-4*m+12
Lam tuon¸ tu
>> ad=a;
>> ad(:,2)=b;
>> d2=det(ad)
d2 =
0
>> ad=a;
>> ad(:,3)=b;
>> d3=det(ad)
d3 =
2*m-6
Khi m = 1 va m = 3 he co n¸hiem duy nhat
>> x=d1/d
x =
(-4*m+12)/(m

2-4*m+3)
>> x=simplify(x)
Io Trònh Anh Ngoïc
x =
-4/(m-1)
>> y=d2/d
y =
0
>> z=d3/d
z =
(2*m-6)/(m

2-4*m+3)
>> z=simplify(z)
z =
2/(m-1)
1ruon¸ hop m = 1
>> subs(d1)
ans =
8
he vo n¸hiem. 1ruon¸ hop m = 3 ta co d1 = d2 = d3 = 0 truon¸ hop nay phai xet
cu the va dun¸ phuon¸ phap Causs, danh cho lan doc.
Cbo tbícb 1. Lenh factor(s) tra ve vecto chua cac thua so n¸uyen to cua s. 1ron¸
thi du nay ta thay factor co the phan tich da thuc thanh tich.
Tbí do 3. Cho day
u
n
=
1
n + 1
+
1
n + 2
+
1
n + 3
+ . . . +
1
n + n
.
Chun¸ minh day {u
n
} hoi tu va tìm ¸ioi han cua no.
1hon¸ thuon¸, de tìm ¸ioi han cua cac day so ma so han¸ ton¸ quat cua day la
mot ton¸, truoc het ta tinh ton¸ de xac dinh u
n
duoi dan¸ con¸ thuc don¸
2
. 1ron¸
Matlal lenh symsum cho ta con¸ cu tinh ton¸ rat tien loi
3
.
>> clear all
>> syms n,i
>> un=symsum(1/(i+n),i,1,n)
un =
Psi(2*n+1)-Psi(n+1)
Len day ta ¸ap mot ham dac liet, ham di¸amma (lan co the dun¸ help Psi de
liet them ve ham nay). 1uy nhien, neu tiep tuc dun¸ lenh limit ta se duoc ¸ioi
2
Con¸ thuc don¸ cua mot lieu thuc la con¸ thuc ma so phep tinh can thuc hien la xac
dinh. Ò day con¸ thuc cua u
n
co so phep tinh tan¸ theo chi so n (khon¸ xac dinh).
3
1u day, n¸oai mot vai truon¸ hop, cu phap cua cac lenh Matlal se khon¸ duoc phat lieu
tuon¸ minh, lan nen hoc (doan) cu phap cua no thon¸ qua thi du hien hanh, va chi nen
dun¸ den help khi thay that can thiet.
Hoïc MATLAB baèng thí duï I7
han cua day
>> limit(un,n,inf)
ans =
log(2)
Ket qua tra ve cun¸ lieu thuc xac dinh u
n
¸oi y cho ta, u
n
la ton¸ tich phan cua
ham f(x) = 1/(1 + x) tren doan [0, 1] voi phan hoach deu ∆x
i
= 1/n va cac diem
ξ
i
la diem cuoi cua cac doan con. 1a co
u
n
=
1
1 + 1/n
1
n
+
1
1 + 2/n
1
n
+
1
1 + 3/n
1
n
+ . . . +
1
1 + 1
1
n
suy ra
lim
n→∞
u
n
=
_
1
0
dx
1 + x
= ln(1 + x)|
1
0
= ln(2).
Cbo tbícb 2. Ò day ta ¸ap lenh limit - lay ¸ioi han mot lieu thuc. Lenh nay xuat
hien duoi nhieu dan¸ thuc:
limit(F,x,a) lay ¸ioi han cua lieu thuc ky hieu F khi x → a.
limit(F,a) dun¸ findsym(F) nhu la lien doc lap.
limit(F) dun¸ a = 0 nhu la diem ¸ioi han.
limit(F,x,a,’right’) hay limit(F,x,a,’left’) lay ¸ioi han mot phia.
1hi du:
>> syms x a t h
Khi do:
limit(sin(x)/x) tra ve 1
limit((x-2)/(x

2-4),2) tra ve 1/4
limit((1+2*t/x)

(3*x),x,inf) tra ve exp(6*t)
limit(1/x,x,0,’right’) tra ve inf
limit(1/x,x,0,’left’) tra ve -inf
limit((sin(x+h)-sin(x))/h,h,0) tra ve cos(x)
v = [(1 + a/x)

x, exp(-x)];
limit(v,x,inf,’left’) tra ve [exp(a), 0]
Tbí do 4. Khao sat su hoi tu cua day {x
n
} xac dinh loi con¸ thuc truy hoi
x
1
=

2, x
n
=
_
2 −x
n−1
(n ≥ 2).
Y tuon¸ dau tien den voi ta la ap dun¸ dinh ly hoi tu cho day don dieu. 1uy
nhien, dun¸ Matlal tinh ZO so han¸ dau cua day
Io Trònh Anh Ngoïc
>> clear all
>> x(1)=sqrt(2);
>> for i=2:20, x(i)=sqrt(2-x(i-1)); end
>> x
x =
Columns 1 through 15
1.4142 0.7654 1.1111 0.9428 1.0282 0.9858 1.0071 0.9965 1.0018 0.9991
1.0004 0.9998 1.0001 0.9999 1.0000
Columns 16 through 20
1.0000 1.0000 1.0000 1.0000 1.0000 1.4142 0.7654 1.1111 0.9428 1.0282
0.9858 1.0071 0.9965 1.0018
Ket qua tra ve cho thay day khon¸ don dieu. 'hun¸ ¨hình nhu¨ day hoi tu ve
I (dao don¸ quanh ¸ia tri I). Lieu nay dan ta den y tuon¸ dun¸ n¸uyen ly anh xa co.
De thay, voi moi n
0 ≤ x
n


2 < 1.5.
Ky hieu f(t) =

2 −t, ta co x
n
= f(x
n−1
). Hon nua,
x
n
−x
n−1
= f

(x

)(x
n−1
− x
n−2
) x
n−2
< x

< x
n−1
hay x
n−1
< x

< x
n−2
.
Dun¸ Matlal khao sat ham so f

(t) tren doan [0, 1.5]
>> syms t
>> f=sqrt(2-t);
>> df=diff(f)
df =
-1/2/(2-t)

(1/2)
>> ezplot(df,[0 1.5])
Lo thi (hình o) cho thay max
[0,1.5]
|f

(t)| < 1, f la anh xa co tren [0, 1.5]. Chun¸
minh chi tiet danh cho lan doc.
Tbí do S. Ciai phuon¸ trình vi phan (x
2
+1)dy+ydx = 0 voi dieu kien dau y(1) = 1.
1a co the ¸iai n¸ay phuon¸ trình tren lan¸ cach ¸oi ham dsolve. Muc dich
va cu phap cua ham nay duoc the hien neu ta ¸oi help dsolve tu dau nhac Matlal.
>> y=dsolve(’(1+x

2)*Dy+y’,’y(1)=1’,’x’)
y =
1/exp(-1/4*pi)*exp(-atan(x))
Ò day ta chi dun¸ Matlal tro ¸iup khi tinh toan. Lay la phuon¸ trình vi phan co
lien phan ly, tach lien ta duoc:
dy
y
+
dx
x
2
+ 1
.
>> clear all
>> syms x y
Hoïc MATLAB baèng thí duï I9
Hình o: Lo thi ham f

(t), thi du +.
>> tp1=int(1/y,y)
tp1 =
log(y)
>> tp2=int(1/(x

2+1),x)
tp2 =
atan(x)
'¸hiem ton¸ quat cua phuon¸ trình la
ln y + arctgx = C.
Le tìm n¸hiem rien¸ ta dun¸ Matlal tro ¸iup viec xac dinh han¸ so tich phan C.
>> syms c
>> pt=subs(tp1+tp2+c,{x,y},{1,1})
pt =
1/4*pi+c
>> c=solve(pt,c)
c =
-1/4*pi
\ay n¸hiem can tìm la
ln y + arctgx =
π
4
.
Cbo tbícb 3. 1ron¸ thi du ta ¸ap hai lenh moi cua Matlal: int va solve.
lNT - tich phan ham so. Cu phap:
int(s) la tich phan lat dinh cua s doi voi lien ky hieu nhu duoc xac dinh
loi findsym. 'eu s la han¸ thì tich phan duoc lay voi x.
ZO Trònh Anh Ngoïc
int(s,v) la tich phan lat dinh cua s lay doi voi v. v la lien ky hieu vo
huon¸.
int(s,a,b) la tich phan xac dinh cua s doi voi lien ky hieu cua no tu a
den b. a, b la cac so hay lien ky hieu vo huon¸.
int(s,v,a,b) la tich phan xac dinh cua s doi voi v tu a den b.
SOtVF - ¸iai phuon¸ trình dai so. Cu phap:
solve(’eqn 1’,’eqn 2’,...,’eqn N’)
solve(’eqn 1’,’eqn 2’,...,’eqn N’,’var 1,var 2,...,var N’)
solve(’eqn 1’,’eqn 2’,...,’eqn N’,’var 1’,’var 2’,...’var N’)
Cac eqn i la cac lieu thuc ky hieu hay chuoi cac phuon¸ trình chi dinh. Cac
var j la cac lien ky hieu hay chuoi chi dinh cac lien chua liet. 'eu cac var j
khon¸ duoc chi dinh thì cac an duoc xac dinh loi findsym.
'eu khon¸ co n¸hiem ¸iai tich duoc tìm thay va so phuon¸ trình lan¸ so an
thì n¸hiem so duoc tìm.
Le liet cach xuat ket qua cua solve xem cac thi du sau.
I) solve(’p*sin(x) = r’) chon 'x' nhu la an, tra ve ans = asin(r/p).
Chu y, thay vì viet ’p*sin(x) = r’ ta co the viet ’p*sin(x) - r’; Matlal mac
nhien hieu ve hai la O.
Z) [x,y] = solve(’x

2 + x*y + y = 3’,’x

2 - 4*x + 3 = 0’) tra ve
x =
[ 1]
[ 3]
y =
[ 1]
[ -3/2]
J) s = solve(’x

2*y

2 - 2*x - 1 = 0’,’x

2 - y

2 - 1 = 0’) tra ve
n¸hiem dan¸ cau truc
s =
x: [8x1 sym]
y: [8x1 sym]
+) [u,v] = solve(’a*u

2 + v

2 = 0’,’u - v = 1’) xema nhu la tham
so va ¸iai hai phuon¸ trình tìm u,v. So sanh voi thi du len duoi 5).
5) s = solve(’a*u

2 + v

2’,’u - v = 1’,’a,u’) xem v nhu la tham
so, ¸iai hai phuon¸ trình, va tra ve s.a, s.u.
Tbí do 6. Ciai phuon¸ trình vi phan xy

−y = x
2
cos x.
lhuon¸ trình co the viet lai duoi dan¸ chuan,
y


y
x
= x cos x.
Hoïc MATLAB baèng thí duï ZI
Lay la phuon¸ trình vi phan tuyen tinh cap mot. '¸hiem ton¸ quat:
y

+ p(x)y = q(x).
'hac lai, n¸hiem ton¸ quat cua phuon¸ trình vi phan tuyen tinh cap mot co dan¸:
y = y
p
+ Cy
1
voi Cy
1
la n¸hiem ton¸ quat cua phuon¸ trình thuan nhat tuon¸ un¸ (y
1
la n¸hiem
co so); y
p
la mot n¸hiem rien¸ cua phuon¸ trình xac dinh loi:
y
p
=
_
G
1
(x, s)q(s)ds
tron¸ do
G
1
(x, s) =
y
1
(x)
y
1
(s)
la ham Creen cap mot, va s duoc thay lan¸ x sau khi lay tich phan.
Bay ¸io ta ¸iai phuon¸ trình. lhuon¸ trình thuan nhat tuon¸ un¸ (phuon¸ trình
co lien phan ly)
y


y
x
= 0 hay
dy
y
=
dx
x
.
Dun¸ Matlal tro ¸iup lay tich phan
>> clear all
>> syms x
>> int(1/x,x)
ans =
log(x)
\ì lny = lnx nen n¸hiem co so: y
1
= x.
Ham Creen cap mot: G
1
(x, s) = x/s. Dun¸ Matlal tro ¸iup tìm n¸hiem rien¸
>> y1=x;
>> syms s
>> yp=subs(int(y1/subs(y1,x,s)*s*cos(s),s),s,x)
yp =
x*sin(x)
\ay n¸hiem ton¸ quat cua phuon¸ trình: y = (C + sinx)x.
Tbí do 7. Cho phuon¸ trình vi phan tuyen tinh cap hai thuan nhat
y

+ a(x)y

+ b(x)y = 0.
ZZ Trònh Anh Ngoïc
Cia su phuon¸ trình co n¸hiem co so y
1
, tìm n¸hiem co so con lai.
'¸hiem co so thu hai duoc tìm duoi dan¸:
y
2
(x) = u(x)y
1
(x).
Dun¸ Matlal de xac dinh u(x)
>> syms x
Lua vao cac ham truu tuon¸
>> a=sym(’a(x)’);
>> b=sym(’b(x)’);
>> y1=sym(’y1(x)’);
>> u=sym(’u(x)’);
>> y2=u*y1;
1hay y2 vao phuon¸ trình vi phan
>> p=simple(diff(y,2)+a*diff(y)+b*y)
p =
diff(u(x),‘$‘(x,2))*y1(x)+2*diff(u(x),x)*diff(y1(x),x)+
u(x)* diff(y1(x),‘$‘(x,2))+a(x)*(diff(u(x),x)*y1(x)+
u(x)*diff(y1(x),x))+b(x)*u(x)*y1(x)
'hom cac so han¸ co chua u
>> p=collect(p,u)
p =
(diff(y1(x),‘$‘(x,2))+diff(y1(x),x)*a(x)+b(x)*y1(x))*
u(x)+diff(u(x),‘$‘(x,2))*y1(x)+2*diff(u(x),x)*
diff(y1(x),x)+a(x)*diff(u(x),x)*y1(x)
Le thay ro ket qua nhom ta dun¸ lenh pretty
>> pretty(pp)
__
d
2
dx
2
y1(x)
_
+ a(x)
_
d
dx
y1(x)
_
+ b(x)y1(x)
_
u(x) +
_
d
2
dx
2
u(x)
_
y1(x)
+2
_
d
dx
u(x)
__
d
dx
y1(x)
_
+ a(x)
_
d
dx
u(x)
_
y1(x)
He so cua u lan¸ khon¸ do y1 la n¸hiem co so. Matlal khon¸ phat hien duoc dieu
nay. 1a phai ¸iup'
>> p=subs(p,diff(y1,2)+diff(y1)*a+b*y1,0);
>> pretty(p)
_
d
2
dx
2
u(x)
_
y1(x) + 2
_
d
dx
u(x)
__
d
dx
y1(x)
_
+ a(x)
_
d
dx
u(x)
_
y1(x)
Hoïc MATLAB baèng thí duï ZJ
Le ha lac dat v = u

hay u =
_
vdx. 1ruoc het dua vao ham truu tuon¸ v
>> v=sym(’v(x)’);
>> p=subs(p,u,int(v));
>> pretty(p)
_
d
dx
v(x)
_
y1(x) + 2v(x)
_
d
dx
y1(x)
_
+ a(x)v(x)y1(x)
lhuon¸ trình nhan duoc la phuon¸ trình vi phan cap mot co lien phan ly. 1ach
lien. 1a thuc hien lien tiep hai lan lenh collect; xem chu thich len duoi ve lenh
collect va ¸iai thich ly do phai thuc hien hai lan.
>> p=collect(collect(p/v/y1,v),y1)
p =
diff(v(x),x)/v(x)+2*diff(y1(x),x)/y1(x)+a(x)
Lay tich phan. 1a can tinh tich phan tun¸ so han¸ cua p. Le chi dinh so han¸
tron¸ mot lieu thuc ta ¸oi lenh op cua Maple. Cu phap cua lenh nay tron¸ Maple
la op(i,s) - lay so han¸ thu i tron¸ lieu thuc s. Le ¸oi mot lenh cua Maple. 'eu
tron¸ Maple lenh do duoc viet la
function(arg1, arg2, ...)
thì tron¸ Matlal de thuc hien lenh do ta viet
maple(’function’,arg1,arg2,..,) .
>> p=int(maple(’op’,1,pp))+int(maple(’op’,2,pp))+ int(maple(’op’,3,pp))
Warning: Explicit integral could not be found.
In D:\MATLABR11\toolbox\symbolic\@sym\ int.m at line 58
p =
log(v(x))+2*log(y1(x))+int(a(x),x)
Xac dinh v ta lam nhu sau
>> p1=subs(p,v,’m’);
>> v=solve(p1,’m’)
v =
1/exp(int(a(x),x))/y1(x)

2
1ai sao ta khon¸ ¸iai truc tiep ra v! \ì m chua duoc khai lao lien sym nen ta phai
dat no ¸iua hai dau nhay, chi dinh chuoi.
>> u=int(v)
Warning: Explicit integral could not be found.
In D:\MATLABR11\toolbox\symbolic\@sym\ int.m at line 58
u =
int(1/exp(int(a(x),x))/y1(x)

2,x)
1om lai,
y
2
= y
1
_
dx
y
2
1
exp(
_
adx)
. (I)
Z+ Trònh Anh Ngoïc
Cbo tbícb 4. Cu phap cua lenh collect la collect(s,v) tron¸ do s, v la cac
lieu thuc ky hieu. \oi lenh nay Matlal tra ve lieu dien moi cua s nhu la mot da
thuc theo v.
Lenh pretty, co cu phap: pretty(s) - xuat s duoi dan¸ thuc toan hoc, de
doc hon.
Canh lao cua Matlal ¨Explicit integral could not be found¨ va cho
liet ¨van de¨ nay sinh o dau la hien nhien vì cac ham cho truoc khon¸ co lieu
thuc toan hoc cu the.
Tbí do 8. 1ìm n¸hiem co so cua phuon¸ trình Cauchy - Luler
x
2
y

+ axy

+ by = f(x),
tron¸ do a, b la cac han¸ so.
1a tìm n¸hiem duoi dan¸ y = x
m
. Dun¸ Matlal de xac dinh m.
>> clear all
>> syms a b x m
>> y=x

m
y =
x

m
1hay y vao phuon¸ trình vi phan
>> p=simple(x

2*diff(y,2)+a*x*diff(y)+b*y)
p =
x

m*(m

2-m+a*m+b)
Coi lenh coeff cua Maple de lay he so cua x tron¸ lieu thuc p
>> p=maple(’coeff’,p,x

m)
p =
m

2-m+a*m+b
Ciai phuon¸ trình xac dinh m. Chu y, o day khon¸ can chi dinh an (tai sao!)
>> m=simple(solve(p))
m =
[1/2-1/2*a+1/2*(1-2*a+a

2-4*b)

(1/2)]
[ 1/2-1/2*a-1/2*(1-2*a+a

2-4*b)

(1/2)]
'hu vay, noi chun¸, co hai ¸ia tri m
m
12
=
1 − a ±
_
(1 − a)
2
− 4b
2
.
Cac n¸hiem co so:
y
1,2
= x
1−a±

(1−a)
2
−4b
2
.
Hoïc MATLAB baèng thí duï Z5
Tbí do 9. Bai toan ¸ia tri rien¸ Sturm-Liouville chinh quy
• 1oan tu vi phan L, tac don¸ len cac ham y(x) xac dinh tren I = [a, b], dinh
loi:
Ly =
d
dx
_
p(x)
dy
dx
_
+ q(x)y
duoc ¸oi la toan tu vi phan Sturm-Liouville ton¸ quat.
• Bai toan tìm n¸hiem khon¸ tam thuon¸ cua phuon¸ trình vi phan tuyen tinh
thuan nhat:
Lϕ + λw(x)ϕ = 0,
thoa dieu kien lien thuan nhat dan¸:
κ
1
ϕ(a) + κ
2
ϕ

(a) = 0, κ
3
ϕ(b) + κ
4
ϕ

(b) = 0
duoc ¸oi la lai toan ¸ia tri rien¸ Sturm-Liouville chinh quy voi w la ham tron¸ luon¸.
Cac dieu kien han che dat len he la, tren khoan¸ huu han I, cac ham p(x),
q(x) va w(x) la cac ham thuc lien tuc, p(x) va q(x) la duon¸ tren khoan¸ nay. Cac
dieu kien lien duoc cho tach liet theo tun¸ diem.
1ai mot diem lien, thi du a, dieu kien lien chinh quy duoc phan thanh la
loai:
I) Loai I -- dieu kien Dirichlet y(a) = 0.
Z) Loai Z -- dieu kien 'eumann y

(a) = 0.
J) Loai J -- dieu kien Rolin y

(a) + hy(a) = 0.
• Cac ¸ia tri λ
n
lam cho lai toan ¸ia tri rien¸ Sturm-Liouville co n¸hiem khon¸
tam thuon¸ duoc ¸oi la ¸ia tri rien¸, n¸hiem tuon¸ un¸ ϕ
n
duoc ¸oi la ham rien¸.
1a co cac tinh chat sau:
(i) 1on tai vo so cac ¸ia tri rien¸ co the sap theo thu tu do lon tan¸ dan λ
n
voi n = 0, 1, 2, . . .
(ii) 1at ca cac ¸ia tri rien¸ la thuc.
(iii) \oi moi ¸ia tri rien¸ tuon¸ un¸ voi mot ham rien¸ duy nhat.
(iv) Cac ham rien¸ tuon¸ un¸ voi cac ¸ia tri rien¸ khac nhau truc ¸iao doi voi
ham tron¸ luon¸ w tren I. 'eu cac ham rien¸ duoc chuan hoa thì
_
b
a
ϕ
n
(x)ϕ
m
(x)w(x)dx = δ
mn
.
1a co the chuan hoa cac ham rien¸ lan¸ cach chia no cho chuan (do dai) cua no
ϕ
n
=
¸
_
b
a
ϕ
n
(x)
2
w(x)dx.
Zo Trònh Anh Ngoïc
Mot truon¸ hop dac liet cua phuon¸ trình Sturm -- Liouville voi p(x) = 1,
q(x) = 0 va w(x) = 1 la phuon¸ trình Luler
y

+ λy = 0
1ìm ¸ia tri rien¸ va ham rien¸ tuon¸ un¸ cho phuon¸ trình Luler tren khoan¸
I = (0, 1) voi cac dieu kien lien y

(0) = 0, y(1) = 0.
>> clear all
>> syms x c k c1 c2 real
a) 1ruon¸ hop λ = −c
2
= 0
'¸hiem dac trun¸
>> solve(’k

2-c

2’,k);
lhuon¸ trình lac hai co hai n¸hiem. Le truy xuat ta viet k(1), k(2).
'¸hiem co so, n¸hiem ton¸ quat
>> y1=exp(k(1)*x);
>> y2=exp(k(2)*x);
>> y=c1*y1+c2*y2;
Dun¸ dieu kien lien
>> eq1=subs(y,x,1);
>> eq2=subs(diff(y),x,0);
1a xac dinh c de he ¸om eq1 va eq2 co n¸hiem khon¸ tam thuon¸, n¸hIa la tìm c
de dinh thuc co so cua he lan¸ khon¸
>> delta=det([maple(’coeff’,eq1,c1) maple(’coeff’, eq1, c2);
maple(’coeff’, eq2, c1) maple(’coeff’, eq2, c2)])
delta =
-exp(c)*c-exp(-c)*c
>> delta=simplify(delta)
delta =
-c*(1+exp(-2*c))*exp(c)
luon khac khon¸ (λ = 0), lai toan chi co n¸hiem tam thuon¸.
l) 1ruon¸ hop λ = 0
>> clear all
>> syms x c k c1 c2 real
>> k=solve(’k

2)
k =
[ 0]
[ 0]
phuon¸ trình dac trun¸ co n¸hiem kep k = 0.
>> y1=exp(k(1)*x); y2=x*y1; y=c1*y1+c2*y2;
>> eq1=subs(y,x,1); eq2=subs(diff(y),x,0);
>> delta=det([maple(’coeff’,eq1,c1) maple(’coeff’, eq1, c2);
maple(’coeff’, eq2, c1) maple(’coeff’, eq2, c2)])
delta =
1
Hoïc MATLAB baèng thí duï Z7
lai toan chi co n¸hiem tam thuon¸.
c) 1ruon¸ hop λ = c
2
>> clear all
>> syms x c k c1 c2 n real
>> k=solve(k

2+c

2,k)
k =
[ i*c]
[ -i*c]
1ruon¸ hop n¸hiem dac trun¸ phuc
>> y1=cos(c*x); y2=sin(c*x); y=c1*y1+c2*y2;
>> eq1=subs(y,x,1); eq2=subs(diff(y),x,0);
>> delta=det([maple(’coeff’, eq1, c1), maple(’coeff’, eq1, c2);
maple(’coeff’, eq2, c1), maple(’coeff’, eq2, c2)])
delta =
cos(c)*c
>> solve(ans,c)
ans=
[ 1/2*pi]
[ 0]
Chu y, doi voi phuon¸ trình cos(c)*c (=O) chi cho n¸hiem acos(0). Loai n¸hiem
c=0.
>> cn=pi/2+n*pi;
>> eq1=subs(eq1,c,cn)
eq1 =
-c1*sin(n*pi)+c2*cos(n*pi)
Matlal khon¸ phan liet duoc n la so n¸uyen nen khon¸ danh ¸ia duoc sin(n*pi)
va cos(n*pi) ta phai ¸iup'
>> eq1=subs(eq1,sin(n*pi),0); eq1=subs(eq1, cos (n*pi), (-1)

n)
eq1=
c2*(-1)

n
>> eq2=subs(eq2,c,cn(1))
eq2 =
c2*(1/2*pi+n*pi)
'hu vay c2=0 con c1 tuy y. 1a chon c1=1
>> phi n=subs(y,{ c,c1,c2},{cn,1,0})
phi n=
cos((1/2*pi+n*pi)*x)
Zo Trònh Anh Ngoïc
Cac ¸ia tri rien¸ va cac ham rien¸ tuon¸ un¸:
λ
n
=
(2n + 1)
2
π
2
4
,
ϕ
n
= cos
_
(2n + 1)πx
2
_
,
n = 0, 1, 2, . . .
Chuan hoa cac ham rien¸
>> phi n=cos((pi/2+n*pi)*x);
>> norm=sqrt(int(phi n*phi n,0,1));
>> norm=simple(subs(norm,sin(n*pi),0));
>> phi n=phi n/norm
phi n=
cos((1/2*pi+n*pi)*x)*2

(1/2)
\ay cac ham rien¸ chuan hoa:
ϕ
n
=

2 cos
_
(2n + 1)πx
2
_
n = 0, 1, 2, . . .
Tbí do 10. lhuon¸ trình son¸ I-chieu - lhuon¸ phap tach lien
Xet phuon¸ trình dao ham rien¸, mo ta phan lo son¸ u(x, t) cho dao don¸ doc
tron¸ thanh,
u
tt
= c
2
u
xx
−γu
t
0 < x < 1; t > 0.
Lieu kien lien:
u(0, t) = 0,
u
x
(1, t) + u(1, t) = 0.
Lieu kien dau:
u(x, 0) = f(x),
u
t
(x, 0) = g(x).
1a tìm n¸hiem duoi dan¸ tach lien,
u(x, t) = X(x)T(t).
1hay vao phuon¸ trình dao ham rien¸, roi chia hai ve phuon¸ trình cho c
2
XT ta
duoc
T

+ γT

c
2
T
=
X

X
.
Hoïc MATLAB baèng thí duï Z9
\ì ve trai la ham chi cua t con ve phai la ham chi cua x nen chi co the xay ra neu
ton tai han¸ so λ de cho
T

+ γT

+ c
2
λT = 0 va X

+ λX = 0.
lhuon¸ trình vi phan dau tien la phuon¸ trình vi phan tuyen tinh thuan nhat cap
hai he so han¸. 1uon¸ tu, phuon¸ trình vi phan thu hai cun¸ la phuon¸ trình vi phan
tuyen tinh thuan nhat cap hai. 1a de dan¸ nhan ra chun¸ thuoc loai Sturm-Liouville.
\oi dieu kien lien da cho lai toan Sturm-Liouville theo lien khon¸ ¸ian la
phuon¸ trình Luler
X

+ λX = 0
voi cac dieu kien lien
X(0) = 0,
X

(1) + X(1) = 0.
Le tranh lap lai lap luan da lam tron¸ thi du truoc, ta chi xet truon¸ hop
λ = c
2
> 0. Khi do n¸hiem co so cua phuon¸ trình Luler: X
1
= sin(cx), X
2
= cos(cx).
>> clear all
>> syms x c c1 c2 real
>> X=c1*sin(c*x)+c2*cos(c*x);
>> eq1=subs(diff(X,x),x,0)
eq1 =
c2
>> eq2=subs(diff(X,x)+X,x,1)
eq2 =
c1*cos(c)*c-c2*sin(c)*c+c1*sin(c)+c2*cos(c)
>> delta=det([maple(’coeff’,eq1,c1) maple(’coeff’,eq1,c2);
maple(’coeff’,eq2,c1) maple(’coeff’,eq2,c2)])
delta=
-cos(c)*c-sin(c)
'hu vay cac ¸ia tri rien¸ λ
n
la n¸hiem cua phuon¸ trình
tan(
_
λ
n
) = −
_
λ
n
(∗)
va cac ham rien¸ tuon¸ un¸ la (do c2=0)
X
n
(x) = sin(
_
λ
n
x).
Chuan hoa ham rien¸:
>> syms lambda n
>> X n=sin(sqrt(lambda n)*x);
JO Trònh Anh Ngoïc
>> norm2 n=int(X n

2,x,0,1)
norm2 n=
1/2*(-cos(lambda n

(1/2))*sin(lambda n

(1/2))+lambda n

(1/2))
/lambda n

(1/2)
Ket qua tinh lan¸ Matlal cho
normZ n =
−cos(

λ
n
) sin(

λ
n
) +

λ
n

λ
n
.
Le y ran¸, sin(

λ
n
) = −

λ
n
cos(

λ
n
) (do (*)), nen ta co the thu ¸on lieu thuc cua
norm2 n va chuan hoa cac X
n
nhu sau
>> norm2 n=simplify(subs(ans,sin(sqrt(lambda n)),-sqrt(lambda n)
*cos(sqrt(lambda n))))
norm2 n=
1/2*cos(lambda n

(1/2))

2+1/2
>> X n=X n/sqrt(norm2 n)
X n=
2*sin(lambda n

(1/2)*x)/(2*cos(lambda n

(1/2))

2+2)

(1/2)
X
n
(x) =

2 sin(

λ
n
x)
_
cos
2
(

λ
n
) + 1
voi n = 1, 2, . . .
'¸hiem ton¸ quat tuon¸ un¸ cua phuon¸ trình vi phan theo lien thoi ¸ian la
T
n
(t) = e
−γt/2
(A
n
cos(ω
n
t) + B
n
sin(ω
n
t)) n = 1, 2, . . . ,
tron¸ do
ω
n
=
1
2
_

n
c
2
− γ
2
.
Ban hay tu tìm ket qua nay lan¸ Matlal.
'¸hiem ton¸ quat cua phuon¸ trình, lan¸ phep chon¸ chap n¸hiem
u(x, t) =

n=1
X
n
T
n
=

n=1
e
−γt/2
(A
n
cos(ω
n
t) + B
n
sin(ω
n
t))

2 sin(

λ
n
x)
_
cos
2
(

λ
n
) + 1
.
tron¸ do cac han¸ so A
n
, B
n
duoc xac dinh nho dieu kien dau cua lai toan.
Hoïc MATLAB baèng thí duï JI
Bay ¸io ta xet cach dua dieu kien dau vao n¸hiem ton¸ quat. 1hay dieu kien
thu nhat vao n¸hiem ton¸ quat, tai thoi diem x = 0,
f(x) =

n=1
A
n
X
n
(x). (∗∗)
Le dun¸ dieu kien thu hai ta lay dao ham n¸hiem ton¸ quat theo t
u
t
(x, t) =

n=1
X
n
e
−γt/2
__

A
n
γ
2
+ B
n
ω
n
_
cos(ω
n
t) −
_
A
n
ω
n
+
B
n
γ
2
_
sin(ω
n
t)
_
.
1ai thoi diem t = 0,
g(x) =

n=1
X
n
_
B
n
ω
n

A
n
γ
2
_
. (∗ ∗ ∗)
'hac lai ran¸ cac ham rien¸ cua lai toann Sturm-Liouville chinh quy lap thanh mot
tap day du doi voi cac ham tron tun¸ khuc tren khoan¸ huu han I. \ay, ca hai
phuon¸ trình tren la khai trien lourier ton¸ quat cua hai ham f(x) va g(x) theo cac
ham rien¸ cua he. Cac he so A
n
va B
n
co mat tron¸ cac he so cua hai khai trien
nay. Bay ¸io ta tinh cac he so A
n
va B
n
.
Lay tich tron¸ hai ve cac phuon¸ trình (**), (***) voi cac ham rien¸. Do tinh
truc chuan cua cac ham rien¸, ta co:
A
n
=
_
b
a
f(x)X
n
(x)dx,
B
n
ω
n

A
n
γ
2
=
_
b
a
g(x)X
n
(x)dx ⇒ B
n
=
1
ω
n
_
b
a
_
γf(x)
2
+ g(x)
_
X
n
(x)dx.
\ay, ta co the viet n¸hiem hình thuc cua lai toan ¸ia tri dau cho phuon¸ trình
son¸:
u(x, t) =

n=1
X
n
(x)e
−γt/2
__
b
a
f(x)X
n
(x)dx cos(ω
n
t)
+
1
ω
n
_
b
a
_
γf(x)
2
+ g(x)
_
X
n
(x)dx sin(ω
n
t)
_
.
JZ Trònh Anh Ngoïc
1at ca cac phep toan duoc thuc hien o tren dua tren ¸ia thiet cac chuoi (**), (***)
hoi tu deu va su thay doi thu tu lay ton¸ va thuc hien phep toan la co hieu luc. Co
the chun¸ to ran¸ neu f(x) va g(x) thoa cac dieu kien lien nhu cac ham rien¸, thì
ca hai chuoi hoi tu deu.
Le cu the, ta tinh cac he so A
n
va B
n
cho truon¸ hop c = 1/4, γ = 1/5, va
cac ham tron¸ dieu kien dau:
f(x) = x −
2x
2
3
, g(x) = x.
1ruoc het ta thiet lap con¸ thuc tinh A n, B n
>> clear all
>> syms n lambda n x real
>> c=1/4; gamma=1/5;
>> f=x-2*x

2/3; g=x;
>> omega n=sqrt(4*lambda n*c

2-gamma

2)/2;
omega n=
1/20*(25*lambda n-4)

(1/2)
>> A n=int(f*sqrt(2)*sin(sqrt(lambda n)*x)/sqrt(cos(sqrt(lambda n))

2+1),
x,0,1)
A n=
-1/3*2

(1/2)*(sin(lambda n

(1/2))*lambda n

(3/2)+4*cos(lambda n

(1/2))
*lambda n+cos(lambda n

(1/2))*lambda n

2-4*lambda n)/lambda n

(5/2)
/(cos(lambda n

(1/2))

2+1)

(1/2)
>> A n=simplify(subs(A n,sin(sqrt(lambda n)),-sqrt(lambda n)
*cos(sqrt(lambda n))))
A n=
-4/3*2

(1/2)*(cos(lambda n

(1/2))-1)/lambda n

(3/2)
/(cos(lambda n

(1/2))

2+1)

(1/2)
>> B n=int(g*sqrt(2)*sin(sqrt(lambda n)*x)/sqrt(cos(sqrt(lambda n))

2+1),
x,0,1)/omega n;
>> B n=simplify(subs(B n,sin(sqrt(lambda n)),-sqrt(lambda n)
*cos(sqrt(lambda n))))
B n=
-40*2

(1/2)*cos(lambda n

(1/2))/lambda n

(1/2)
/(cos(lambda n

(1/2))

2+1)

(1/2)/(25*lambda n-4)

(1/2)
Hoïc MATLAB baèng thí duï JJ
Le tien theo doi ta viet lai cac con¸ thuc vua tìm:
ω
n
=

25λ
n
−4
20
,
A
n
=
−4

2(cos(

λ
n
) − 1)

3/2
n
_
cos
2
(

λ
n
) + 1
,
B
n
=
−8

2(15 cos(

λ
n

n
+ cos(

λ
n
) − 1)
3

25λ
n
− 4λ
3/2
n
_
cos
2
(

λ
n
) + 1
.
voi n = 1, 2, . . .
Le hoan tat ta can tìm cac ¸ia tri rien¸ λ
n
la n¸hiem cua phuon¸ trình (*).
lhuon¸ trình nay chi co the ¸iai xap xi. Le cu the ta ¸iai tìm la ¸ia tri rien¸ dau
tien. 1ruoc het ta ve do thi ham y = tan(v) va ham y = −v de xac dinh tho n¸hiem
cua phuon¸ trình
>> syms v real
>> hold on
(lenh hold on nham muc dich ¸iu lai do thi da co khi ve do thi moi)
>> ezplot(tan(v),[0 20])
>> ezplot(-v,[0 20])
1u do thi (hình 7), cac ¸ia tri lan dau cua n¸hiem xap xi nen chon la 2, 5, 8.
>> lambda 1=fsolve(inline(’tan(v)+v’),2)

2
Optimization terminated: first-order optimality is less than op-
tions.TolFun.
lambda 1 =
4.1159
>> lambda 2=fsolve(inline(’tan(v)+v’),5)

2
Optimization terminated: first-order optimality is less than op-
tions.TolFun.
lambda 2 =
24.1393
>> lambda 3=fsolve(inline(’tan(v)+v’),2)

3
Optimization terminated: first-order optimality is less than op-
tions.TolFun.
lambda 3 =
63.6591
lhan con lai, thay cac ¸ia tri vua tìm vao chuoi chat cut ¸iu la so han¸ (un¸
voi la ¸ia tri λ
n
vua tìm) duoc danh lai cho lan doc.
Cbo tbícb S. Lenh fsolve ¸iai cac phuon¸ trình phi tuyen dan¸ F(X)=0 lan¸
phuon¸ phap lình phuon¸ toi thieu. Cu phap don ¸ian nhat: x=fsolve(fun,x0)
J+ Trònh Anh Ngoïc
Hình 7: Lo thi ham y = tan(v) va y = −v.
lat dau tai x0 va ¸iai cac phuon¸ trình mo ta tron¸ fun. 1a co the chi dinh fun
la mot doi tuon¸ ham inline, thi du, x = fsolve(inline(’sin(x.*x)’),x0). Ò
day lenh inline(bieuthuc) tao mot doi tuon¸ ham inline tu mot lieu thuc chua
tron¸ chuoi bieuthuc. fun cun¸ co the la mot chuoi chua ten mot ham (mot M-file
hay mot ham dinh san) cho phep tinh ¸ia tri cua cac phuon¸ trình voi moi ¸ia tri cu
the cua x, F=fun(x).
S Vict cboong trìnb bang MatIab
Chuon¸ trình viet lan¸ n¸on n¸u Matlal ¸an ¸ion¸ cac n¸on n¸u cap cao quen thuoc
nhu lascal, C. Duoi day ta se hoc cach viet chuon¸ trình thon¸ qua cac thi du cu the.
'hun¸ truoc het ta can liet cac cau truc thuon¸ dun¸ tron¸ n¸on n¸u Matlal.
S.1 Cac cao troc trong ngon ngo MatIab
lF - thuc hien cac lenh co dieu kien
Dan¸ chun¸ cua lenh if la
Hoïc MATLAB baèng thí duï J5
if lieu thuc
cac lenh
elseif lieu thuc
cac lenh
else
cac lenh
end
Cac lenh duoc thuc hien neu phan thuc cua lieu thuc khac khon¸. Cac phan else
va elseif la tuy chon. 'lieu thuc' tron¸ lenh if thuon¸ co dan¸:
lieu thuc I R lieu thuc Z
tron¸ do R la ==, <, >, <=, >=, hay ∼=.
FOk - lap lai cac lenh mot so lan duoc chi dinh
Dan¸ chun¸ cua lenh for la
for lien = ¸ia tri dau : luoc ¸ia tan¸ : ¸ia tri cuoi
cac lenh
end
'lien' lay ¸ia tri '¸ia tri dau', sau moi lan thuc hien cac lenh duoc tan¸ them mot
luon¸ 'luoc ¸ia tan¸', neu 'lien' van con ¨nho hon¨ thì cac lenh van duoc thuc hien.
WHltF - lap lai cac mot so lan khon¸ xac dinh
Dan¸ chun¸ cua lenh while la
while lieu thuc
cac lenh
end
Cac lenh duoc thuc hien khi phan thuc cua 'lieu thuc' khac khon¸. 'lieu thuc' tron¸
lenh while thuon¸ co dan¸:
lieu thuc I R lieu thuc Z
tron¸ do R la ==, <, >, <=, >=, hay ∼=. Lenh break co the duoc dun¸ de dun¸ von¸
lap.
SWlTCH - chuyen doi ¸iua nhieu truon¸ hop tren co so lieu thuc
Dan¸ chun¸ cua lenh switch la
Jo Trònh Anh Ngoïc
switch lieu thuc
case truon¸ hop I,
cac lenh
case truon¸ hop Z
cac lenh
...
otherwise
cac lenh
end
Cac lenh theo sau case thu i duoc thuc hien khi 'lieu thuc' co ¸ia tri 'truon¸ hop i'.
'¸oai cac truon¸ hop duoc liet ke thì cac lenh theo sau otherwise duoc thuc hien.
S.2 Mot so cboong trìnb tbí do
Muc nay chua dun¸ mot so chuon¸ trình don ¸ian viet lan¸ n¸on n¸u Matlal. Moi
thi du lat dau lan¸ co so thuat toan va cuoi cun¸ la chuon¸ trình tinh. Ban doc nen
doc ky phan thuat toan de hieu muc dich va noi dun¸ cua chuon¸ trình. Hay chay
thu va tìm cach sua doi no neu duoc.
Tbí do 11. Ciai phuon¸ trình dai so tuyen tinh AX = B - lhep khu Causs
'hac lai, he phuon¸ trình dai so tuyen tinh co dan¸
a
11
x
1
+ a
12
x
2
+ . . . + a
1n
x
n
= b
1
a
21
x
1
+ a
22
x
2
+ . . . + a
2n
x
n
= b
2
.
.
.
.
.
.
.
.
.
.
.
.
a
n1
x
1
+ a
n2
x
2
+ . . . + a
nn
x
n
= b
n
,
tron¸ do ve phai b
i
, i = 1, . . . , n, va cac he so a
ij
, i, j = 1, . . . , n la cac du lieu cho
truoc; x
1
, . . . , x
n
la an.
Y tuon¸ dan¸ sau phuon¸ phap khu Causs la dun¸ cac phep lien doi so cap de
khu cac an cua he. He phuon¸ trình tuon¸ duon¸, sau khi lien doi, co dan¸ tam ¸iac
tren, duoc ¸iai lan¸ phep the n¸uoc.
'eu a
11
= 0, thì o luoc dau tien ta khu x
1
khoi (n − 1) phuon¸ trình cuoi
lan¸ cach tru phuon¸ trình thu i voi nhan tu
m
i1
= a
i1
/a
11
, i = 2, . . . , n
lan phuon¸ trình dau. Lieu nay sinh ra mot he rut ¸on ¸om (n − 1) phuon¸ trình
voi cac an x
2
, . . . , x
n
, tron¸ do cac he so moi duoc cho loi
a
(2)
ij
= a
ij
− m
i1
a
1j
, b
(2)
i
= b
i
−m
i1
b
1
, i = 2, . . . , n.
Hoïc MATLAB baèng thí duï J7
'eu a
(2)
22
= 0, tiep theo lan¸ cach tuon¸ tu ta khu x
2
tu (n − 2) phuon¸ trình cuoi
cua he phuon¸ trình nay. Sau k −1 luoc, k ≤ n, cua phep khu Causs ma tran A tro
thanh ma tran co dan¸
A
(k)
=
_
¸
¸
¸
¸
¸
¸
¸
¸
¸
_
a
(1)
11
a
(1)
12
. . . a
(1)
1k
. . . a
(1)
1n
0 a
(2)
22
. . . a
(2)
2k
. . . a
(2)
2n
.
.
.
.
.
.
.
.
.
.
.
.
0 0 . . . a
(k)
kk
. . . a
(k)
kn
.
.
.
.
.
.
.
.
.
.
.
.
0 0 . . . a
(k)
nk
. . . a
(k)
nn
_
¸
¸
¸
¸
¸
¸
¸
¸
¸
_
, b
(k)
=
_
¸
¸
¸
¸
¸
¸
¸
¸
¸
_
b
(1)
1
b
(2)
2
.
.
.
b
(k)
k
.
.
.
b
(k)
n
_
¸
¸
¸
¸
¸
¸
¸
¸
¸
_
,
tron¸ do ta da dat A
(1)
= A, b
(1)
= b. Cac phan tu cheo a
(1)
11
, a
(2)
22
,. . . , xuat hien tron¸
qua trình khu duoc ¸oi la cac phan tu tru.
Ky hieu A
k
la ma tran con chinh cua A,
A
k
=
_
¸
¸
¸
_
a
11
a
12
. . . a
1k
a
21
a
22
. . . a
2k
.
.
.
.
.
.
.
.
.
a
k1
a
k2
. . . a
kk
_
¸
¸
¸
_
.
\ì dinh thuc cua ma tran khon¸ thay doi duoi phep lien doi so cap thu la nen
det(A
k
) = a
(1)
11
a
(2)
22
· · · a
(k)
kk
, k = 1, . . . , n.
Cac phan tu tru a
(i)
ii
, i = 1, . . . , n, tron¸ phep khu Causs la khac khon¸ neu va chi
neu det(A
k
) = 0, k = 1, . . . , n. 1ron¸ truon¸ hop nay ta co the khu cho den sau luoc
thu (n −1), con lai mot phuon¸ trình duy nhat
a
(n)
nn
x
n
= b
(n)
n
(a
(n)
nn
= 0).
Cac an cua phuon¸ trình co the tinh lan¸ con¸ thuc truy hoi
x
n
= b
(n)
n
/a
(n)
nn
, x
i
=
_
b
(i)
i

n

k=i+1
a
(i)
ik
x
k
_
/a
(i)
ii
, i = n − 1, . . . , 1.
Qua trình nay ¸oi la the n¸uoc.
Jo Trònh Anh Ngoïc
Cia su o luoc thu k cua phep khu Causs ta co
a
(k)
kk
= 0.
'eu A khon¸ suy lien, thì k cot dau cua ma tran A la doc lap tuyen tinh. Lieu nay
cun¸ dun¸ voi ma tran da lien doi. '¸hIa la ton tai a
pk
= 0 (k < p ≤ n). Ban¸ cach
hoan vi don¸ k va don¸ p thì phan tu nay co the lay lam phan tu tru va phep khu
duoc tiep tuc. 1om lai, ma traän khoâng suy bieán baát kyø coù theå daãn veà daïng tam giaùc
treân baèng pheùp khöû Gauss neáu pheùp hoaùn vò doøng ñöôïc duøng neáu caàn.
'eu rank(A) < n thì co the xay ra o luoc thu k −1 nao do
a
(k)
ik
= 0, i = k, . . . , n.
'eu toan lo cac phan tu a
(k)
ij
= 0, i, j = k, . . . , n thì rank(A) = k −1 va ta dun¸ lai.
'¸uoc lai, neu co phan tu khac khon¸, chan¸ han
a
(k)
pq
,
ta co the man¸ no den vi tri tru lan¸ cach hoan vi don¸ k voi p, cot k voi q (khi cot
cua ma tran A li hoan vi thì ta cun¸ phai hoan vi cac phan tu tuon¸ un¸ tron¸ vecto
x. 1ien hanh theo cach nay moi ma tran A deu co the dua ve dan¸ hình than¸
A
(r)
=
_
¸
¸
¸
¸
¸
¸
¸
¸
¸
_
a
(1)
11
. . . a
(1)
1r
a
(1)
1,r+1
. . . a
(1)
1n
0
.
.
.
.
.
.
.
.
.
.
.
.
.
.
. a
(r)
rr
a
(r)
r,r+1
. . . a
(r)
rn
0 . . . 0 0 . . . 0
.
.
.
.
.
.
.
.
.
.
.
.
0 . . . 0 0 . . . 0
_
¸
¸
¸
¸
¸
¸
¸
¸
¸
_
, b
(r)
=
_
¸
¸
¸
¸
¸
¸
¸
¸
¸
_
b
(1)
1
.
.
.
b
(r)
r
b
(r)
r+1
.
.
.
b
(r)
n
_
¸
¸
¸
¸
¸
¸
¸
¸
¸
_
,
o luoc r = rank(A).
Cac phan tu tru nho co the dan den ket qua khon¸ chinh xac. Co mot cach de
tranh phan tu tru nho duoc ¸oi la phep xoay cuc lo. 1heo cach nay, khi khu x
k
, ta
chon he so lon nhat (ve ¸ia tri tuyet doi) cua x
k
tron¸ n−k+1 phuon¸ trình cuoi nhu
la phan tu tru. '¸hIa la, neu |a
(k)
lk
| la lon nhat cua cac |a
(k)
jk
| voi j = k, k + 1, . . . , n
ta hoan vi don¸ k va l. Ban¸ cach danh so lai ta co the ¸ia su ran¸ phan tu tru a
(k)
kk
co do lon lon nhat.
Hoïc MATLAB baèng thí duï J9
/p dun¸ ¸iai he phuon¸ trình
x
1
+ x
2
+ x
3
= 1
3x
1
+ x
2
− 3x
3
= 5
x
1
− 2x
2
− 5x
3
= 10
Le luu ¸iu ma tran cac he so a va ve phai b, tron¸ chuon¸ trình ta dua vao ma tran
cac he so noi ron¸ ad. Lenh [v,index]=max(ad(k:n,k)) tra ve hai ¸ia tri: v la
phan tu lon nhat cua vecto ad(k:n,k) con index la vi tri cua phan tu do.
% chuong trinh 1 - giai he phuong dstt
clear all
a=[1 1 1; 3 1 -3; 1 -2 -5];
b=[1;5;10];
n=size(a,1);
% ma tran noi rong
ad=a;
ad(:,n+1)=b;
for k=1:n-1
% tim phan tu tru
[v,index]=max(ad(k:n,k));
if v==0
disp(’he phuong trinh suy bien’)
return
end
% dong chua phan tu tru
index=index+k-1;
% hoan vi dong k va dong index
tam=ad(index,:);
ad(index,:)=ad(k,:);
ad(k,:)=tam;
% dung dong k khu cac dong duoi
for i=k+1:n
for j=k:n+1
ad(i,k:n+1)=ad(i,k:n+1)-(ad(i,k)/ad(k,k))*ad(k,k:n+1);
end
end
end
% phep the nguoc
if ad(n,n)==0
disp(’he phuong trinh suy bien’)
return
+O Trònh Anh Ngoïc
end
x(n)=ad(n,n+1)/ad(n,n);
for i=n-1:-1:1
x(i)=ad(i,n+1);
for j=i+1:n
x(i)=x(i)-ad(i,j)*x(j);
end
x(i)=x(i)/ad(i,i);
end
disp(’nghiem cua he:’)
x
Ket qua tinh toan
nghiem cua he:
x =
6.0000 -7.0000 2.0000
Tbí do 12. Ciai phuon¸ trình f(x) = 0 - 1huat toan chia doi khoan¸
Hình o: Lo thi ham so f(x) = x
3
−3x
2
+ x + 1.
'eu ham lien tuc f(x) co dau doi nhau tai cac diem x = B va x = C, thì no
co it nhat mot khon¸ diem tron¸ khoan¸ ¸iua B va C. lhuon¸ phap chia doi (hay
tìm kiem nhi phan) duoc dat tren su kien nay. 'eu f(B)f(C) < 0, ham f(x) duoc
danh ¸ia tai diem ¸iua M = (B + C)/2 cua khoan¸. 'eu f(M) = 0, mot khon¸
diem duoc tìm thay. 'eu khac, f(B)f(M) < 0 hoac f(M)f(C) < 0. 1ron¸ truon¸
hop dau co it nhat mot khon¸ diem o ¸iua M va B, va tron¸ truon¸ hop thu hai co
it nhat mot n¸hiem o ¸iua C va M. 1heo cach nay mot khoan¸ chua n¸hiem duoc
tìm thay ma co chieu dai lan¸ nua chieu dai khoan¸ lan dau. 1hu tuc duoc lap lai
cho den khi dinh vi duoc n¸hiem voi do chinh xac mon¸ muon.
Hoïc MATLAB baèng thí duï +I
/p dun¸ ¸iai phuon¸ trình x
3
−3x
2
+x +1 = 0 (phuon¸ trình nay co n¸hiem
chinh xac: 1, 1 ±

2). Le co cai nhìn so lo ve phuon¸ trình cho ta dun¸ Matlal ve
do thi cua ham f(x) = x
3
−3x
2
+x +1. 1u do thi (hình o) ta thay phuon¸ trình co
la n¸hiem don, lan luot nam tron¸ cac khoan¸ [−0.5, 0], [0.5, 1.5], [2, 3]. 'hu mot thi
du ap dun¸ ta viet chuon¸ trình tìm n¸hiem xap xi cua phuon¸ trình tron¸ khoan¸
[−0.5, 0].
% chuong trinh 2 - giai phuong trinh bang phuong phap chia
% doi khoang
% chuong trinh goi function f1
clear all
b=-0.5;
c=0;
epsilon=10^(-4);
m=b; % m duoc khoi tao bang b
while (abs(c-b)>=epsilon)&(f1(m)~=0)
m=(b+c)/2;
if f1(b)*f1(m)<0
c=m;
else
b=m;
end
end
disp(’nghiem xap xi:’)
m
Chuon¸ trình ¸oi function f1 tra lai ¸ia tri cua ham f(x)
function v=f1(x)
v=x^3-3*x^2+x+1;
Ket qua tinh toan
nghiem xap xi:
m =
-0.4142
Tbí do 13. Ciai phuon¸ trình f(x) = 0 - 1huat toan 'ewton va thuat toan day cun¸
Ca hai thuat toan deu xap xi duon¸ con¸ y = f(x) lan¸ duon¸ than¸ y = L(x)
roi sau do xap xi n¸hiem phuon¸ trình f(x) = 0 lan¸ n¸hiem cua phuon¸ trình
+Z Trònh Anh Ngoïc
L(x) = 0. lhuon¸ phap 'ewton lay y = L(x) la tiep tuyen voi duon¸ con¸ y = f(x)
tai diem xap xi truoc do x
i
va lay diem xap xi ke tiep x
i+1
la n¸hiem cua phuon¸
trình L(x) = 0, theo con¸ thuc taylor, ta co f(x) ≈ f(x
i
) +f

(x
i
)(x −x
i
) = 0, suy ra
x
i+1
= x
i

f(x
i
)
f

(x
i
)
.
1huat toan day cun¸ nhan duoc tu thuat toan 'ewton lan¸ cach xap xi f

(x
i
) lan¸
Hình 9: 1huat toan 'ewton.
ti sai phan
f

(x
i
) ≈
f(x
i
) −f(x
i−1
)
x
i
−x
i−1
.
1u day ta suy ra con¸ thuc lap cua thuat toan day cun¸
x
i+1
= x
i
−f(x
i
)
x
i
− x
i−1
f(x
i
) − f(x
i−1
)
.
/p dun¸ ¸iai phuon¸ trình x
2
− 2 = 0 (phuon¸ trình co n¸hiem chinh xac la
±

2). 1a ap dun¸ thuat toan 'ewton voi diem xap xi lan dau lay lan¸ Z. Lieu
kien dun¸ thuat toan la |x
i+1
− x
i
| < va de tranh truon¸ hop xau - chuon¸ trình
thuc hien qua lau ta dun¸ von¸ lap for chi dinh so lan lap toi da.
% chuong trinh 3 - giai phuong trinh bang phuong phap Newton
% chuong trinh goi function f2 va df2
clear all
xc=2; % diem xap xi ban dau
epsilon=10^(-4);
Hoïc MATLAB baèng thí duï +J
Hình IO: 1huat toan day cun¸.
nmax=100; % so lan lap toi da
for i=1:nmax
xm=xc-f2(xc)/df2(xc);
if abs(xm-xc)<epsilon
break;
else
xc=xm;
end
end
disp(’so lan lap:’)
i
disp(’nghiem xap xi:’)
xm
Chuon¸ trình ¸oi function f2 tra lai ¸ia tri cua ham f(x) va df2 tra lai ¸ia tri cua
ham f

(x)
function v=f2(x)
v=x^2-2;
function v=df2(x)
v=2*x;
Ket qua tinh toan
so lan lap:
i =
4
nghiem xap xi:
xm =
++ Trònh Anh Ngoïc
1.4142
/p dun¸ thuat toan day cun¸ chuon¸ trình viet cho thuat toan 'ewton duoc
lien doi mot chut
% chuong trinh 4 - giai phuong trinh bang phuong phap day cung
% chuong trinh goi function f2
clear all
x1=2;
xc=3; % hai diem xap xi ban dau
epsilon=10^(-4);
nmax=100; % so lan lap toi da
for i=1:nmax
xm=xc-f2(xc)*(xc-x1)/(f2(xc)-f2(x1));
if abs(xm-xc)<epsilon
break;
else
x1=xc;
xc=xm;
end
end
disp(’so lan lap:’)
i
disp(’nghiem xap xi:’)
xm
Ket qua tinh toan
so lan lap:
i =
5
nghiem xap xi:
xm =
1.4142
Ban doc hay thu n¸hiem voi cac thi du so khac va neu nhan xet (dua tren cac ket
qua so) ve la thuat toan tìm n¸hiem xap xi cua phuon¸ trình f(x) = 0.
Tbí do 14. Ciai phuon¸ trình vi phan - lhuon¸ phap Luler 1ron¸ thi du nay ta xet
lai toan xac dinh quy dao cua mot hat x
0
= (a, b, c) liet truon¸ van toc v: tìm ham
Hoïc MATLAB baèng thí duï +5
vecto x = (x(t), y(t), z(t)) thoa phuon¸ trình vi phan
dx
dt
= v(x, t) (∗)
va dieu kien dau x(0) = x
0
.
Mot thuat toan don ¸ian ¸iai lai toan nay la xet su thay doi vi tri cua hat
tron¸ khoan¸ thoi ¸ian ∆t, va thay phuon¸ trình vi phan (!!) lan¸ cac phuon¸ trình
dai so
x(t + ∆t) − x(t)
∆t
= v
x
(x, y, z, t),
y(t + ∆t) − y(t)
∆t
= v
y
(x, y, z, t),
z(t + ∆t) −z(t)
∆t
= v
z
(x, y, z, t).
Le nhan duoc cac phuon¸ trình nay ta da thay dao ham theo thoi ¸ian len ve trai
phuon¸ trình (*) lan¸ sai phan tien cua x tai t.
Ciai cac phuon¸ trình tren, ta duoc
x(t + ∆t) = x(t) + ∆tv
x
(x, y, z, t),
y(t + ∆t) = y(t) + ∆tv
y
(x, y, z, t),
z(t + ∆t) = z(t) + ∆tv
z
(x, y, z, t).
\e mat vat ly, cac phuon¸ trình tren phat lieu ran¸ vi tri cua mot hat tai thoi diem
t +∆t lan¸ vi tri tai thoi diem truoc t con¸ them mot dich chuyen le tron¸ khoan¸
thoi ¸ian ∆t. 1ron¸ khoan¸ thoi ¸ian nay hat xem nhu co van toc khon¸ doi lan¸
van toc cua hat tai thoi diem t.
Tboat toan
I. Chi dinh thoi diem dau (t = 0).
Z. Chon luoc luoi thoi ¸ian.
J. Chi dinh toa do dau x(0), y(0), z(0).
+. 1inh cac van toc v
x
(x, y, z, t), v
y
(x, y, z, t) va v
z
(x, y, z, t).
5. 1inh ve phai cua (!!), roi toa do cua hat tai thoi diem t + ∆t.
+o Trònh Anh Ngoïc
o. Dun¸ hoac tro ve tinh tu luoc + den luoc o.
/p dun¸ thuat toan, xac dinh quy dao cua hat lan dau o vi tri (1, 1, 1) co van toc:
v = −xi + yj + cos tk.
('¸hiem ¸iai tich tron¸ truon¸ hop k = a = ω = 1: x = e
−t
, y = e
t
, z = sint + 1)).
% Chuong trinh 5 - xac dinh quy dao cua diem bang phuong phap
% Euler
clear all
tmax=2; % thoi gian khao sat
N = 100; % so nut thoi gian
dt=tmax/(N-1);
t=0:dt:tmax; % cac nut thoi gian
% vi tri ban dau cua diem
x(1) = 1;
y(1) = 1;
z(1) = 1;
for i = 2:N
x(i) = x(i-1) + dt*f(x(i-1),y(i-1),z(i-1),t(i-1));
y(i) = y(i-1) + dt*g(x(i-1),y(i-1),z(i-1),t(i-1));
z(i) = z(i-1) + dt*h(x(i-1),y(i-1),z(i-1),t(i-1));
end
% ve quy dao
plot3(x,y,z)
Chuon¸ trình ¸oi cac ham f.m, g.m, h.m
function v=f(x,y,z,t)
v=-x;
function v=g(x,y,z,t)
v=y;
function v=h(x,y,z,t)
v=cos(t);
Ban doc nen so sanh n¸hiem xap xi thu duoc tu chuon¸ trình nay voi n¸hiem
chinh xac cua lai toan.
'hu da liet, phuon¸ trình vi phan cap hai tuon¸ duon¸ voi he hai phuon¸
trình vi phan cap mot. Ban hay tu dua ra thuat toan ¸iai so phuon¸ trình vi phan
cap hai dua tren thuat toan dua ra o day.
kFT tÜAN
1ai lieu ¨Hoc Matlal lan¸ thi du¨ duoc trình lay theo quan diem thuc dun¸,
chi hoc nhun¸ ¸ì can thiet va hoc chun¸ tai noi chun¸ xuat hien. Lieu nay truoc
Hoïc MATLAB baèng thí duï +7
Hình II: Quy dao cua hat (1, 1, 1), thi du I+.
tien nham dat muc dich hoc nhanh ve Matlal, n¸oai ra, cach hoc nay con co loi
diem la n¸uoi hoc de nho va liet ap dun¸ n¸ay kien thuc dan¸ hoc. 1uy nhien, day
chua phai la tat ca. 'oi dun¸ cua Matlal rat phon¸ phu, kha nan¸ tinh toan cua
Matlal rat da dan¸. \an con rat, rat nhieu nhun¸ dieu can liet ve Matlal, va nhat
la cho den cuoi tai lieu nay chun¸ ta van chua lan den nhun¸ han che cua Matlal.
Ban doc tron¸ nhun¸ un¸ dun¸ cua mình se nhan thay nhun¸ ¸ioi han do, se thay
can phai liet them nhun¸ ¸ì ve Matlal - nhun¸ noi dun¸ chua duoc lan den o day.
Khi viet tai lieu nay chun¸ toi ap u mot hy von¸ - voi tai lieu nay lan doc se cam
thay hun¸ thu hoc va ap dun¸ Matlal tron¸ con¸ viec hoc tap va n¸hien cuu cua
mình.
'hu moi thu thuoc ve con n¸uoi, tai lieu nay at ham chua nhun¸ dieu sai sot,
chun¸ toi rat mon¸ nhan duoc nhun¸ don¸ ¸op ¸iup hieu chinh nhun¸ sai sot cua
tai lieu. Xin ¸ui nhun¸ y kien nhan xet, nhun¸ don¸ ¸op ¸iup hoan thien tai lieu
den cho chun¸ toi theo dia chi email: tan¸oc_hcmuns.edu.vn. Xin chan thanh cam
on.
1rinh /nh '¸oc
+o Trònh Anh Ngoïc
Moc Ioc
I \ai tro cua may tinh tron¸ n¸hien cuu va un¸ dun¸ toan hoc . . . . . I
Z Matlal - phan mem toan hoc . . . . . . . . . . . . . . . . . . . . . . . . . I
J Luot qua Matlal - cai nhìn dau . . . . . . . . . . . . . . . . . . . . . . . . Z
+ Cioi thieu ve Symlolic Math 1oollox . . . . . . . . . . . . . . . . . . . . 7
+.I Loi tuon¸ ky hieu . . . . . . . . . . . . . . . . . . . . . . . . . . . . o
+.Z Cac phep tinh ky hieu thuon¸ dun¸ . . . . . . . . . . . . . . . . . o
+.J Mot so cach dun¸ khac cua ham sym . . . . . . . . . . . . . . . . IO
+.+ Bien ky hieu mac nhien . . . . . . . . . . . . . . . . . . . . . . . . IJ
+.5 Cac thi du tinh toan ky hieu . . . . . . . . . . . . . . . . . . . . . I+
5 \iet chuon¸ trình lan¸ Matlal . . . . . . . . . . . . . . . . . . . . . . . . J+
5.I Cac cau truc tron¸ n¸on n¸u Matlal . . . . . . . . . . . . . . . . J+
5.Z Mot so chuon¸ trình thi du . . . . . . . . . . . . . . . . . . . . . . Jo
+9

You're Reading a Free Preview

Tải về
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->