GIOI_THIEU_MATLAB

Download Report

Transcript GIOI_THIEU_MATLAB

GIỚI THIỆU MATLAB
NỘI DUNG
1. Khai báo biến.
2. Các hàm số sơ cấp cơ bản.
3. Lệnh subs, eval. Các giá trị đặc biệt.
4. Lệnh expand, simplify, solve.
5. Các lệnh real, imag.
6. Các phép toán so sánh.
7. Các phép toán logic
NỘI DUNG
8. Lệnh limit, diff, int.
9. Các hàm vẽ.
10.Code.
Khai báo biến (symbolic)
syms x y
syms x y real
syms x y positive
Các lệnh trên có thể viết cách khác:
x = sym(x)
x = sym(x, real)
Các hàm sơ cấp cơ bản
sin  x  , cos  x  , tan  x  , cot  x  , sinh  x  , cosh  x 
asin  x  , acos  x  , atan  x  .
exp  x   e ,
x
sq rt  x  
a^xa
x
x
log  x   ln  x  , log 10  x   log 10  x 
abs  x   | x |
Các hằng số
pi  
exp(1)  e
inf   
N aN  not a num ber
eps  2
 52
Gán (=), subs(f), subs(f,new),
subs(f,old,new) (thay thế)
syms x y
>> f=sin(x);
>> subs(f,x,y)
ans =
sin(y)
>> x=pi/2;
>> subs(f)
ans =
1
syms x
>>f=sin(x);
>> subs(f,x,pi)
ans =
1.2246e-016
>> subs(f,pi)
ans =
1.2246e-016
>> subs(f,x,sym(pi))
ans =
0
eval(f) (tính giá trị)
syms x
>>f=sin(x);
>>x=pi/2;
>>eval(f)
ans =
1
expand(f) (khai triển), simplify(f) (rút gọn),
factor(f) (phân tích thành thừa số với hệ số nguyên)
>> expand((x^2-1)*(x^3+x))
ans =
x^5 - x
>> factor(ans)
ans =
x*(x - 1)*(x + 1)*(x^2 + 1)
>> simplify(ans)
ans =
x^5 - x
>> f=tan(x+y);
>> expand(f)
ans =
-(tan(x) + tan(y))/(tan(x)*tan(y) - 1)
>> simplify(ans)
ans =
tan(x + y)
>>
>> f=(x+i)*(x-2*j);
>> simplify(f)
ans =
x^2 - x*i + 2
>> factor(ans)
ans =
(x - 2*i)*(x + i)
solve(f) : giải pt f = 0
>> f=x^3-3*x^2+x+2;
>> solve(f)
ans =
2
5^(1/2)/2 + 1/2
1/2 - 5^(1/2)/2
Tập nghiệm là 1 ma trận
cột.
>> s=solve(f)
s=
2
5^(1/2)/2 + 1/2
1/2 - 5^(1/2)/2
>> s(2)
ans =
5^(1/2)/2 + 1/2
>> s(3,1)
ans =
1/2 - 5^(1/2)/2
Truy
xuất
nghiệm
real(z), imag(z), angle(z), abs(z)
>> z=sqrt(3)+i;
>> a=real(z)
a=
1.7321
>> b=imag(z)
b=
1
>> phi=angle(z)
phi =
0.5236
>> module=abs(z)
module =
2.0000
>> conj(z)
ans =
1.7321 - 1.0000i
>> syms(real(z))
>> abs(-2)
ans =
2
>> s=solve(x^2+x+1)
s=
- 1/2 + (3^(1/2)*i)/2
- 1/2 - (3^(1/2)*i)/2
>> real(s(1))
ans =
-1/2
>> imag(s(2))
ans =
-3^(1/2)/2
>>
Các phép toán so sánh số.
Kết quả: 1(đúng), 0(sai)
>> 1>2
ans =
0
>> 1 <=1
ans =
1
>> 1 <=2
ans =
1
>> 1-2~=0
ans =
1
>> 1-2~=-1
ans =
0
>> 1-3==2
ans =
0
>> 1-3==-2
ans =
1
Các phép toán so sánh số
>> f=x^3-3*x^2+x+2;
>> s=solve(f)
s=
2
5^(1/2)/2 + 1/2
1/2 - 5^(1/2)/2
>> s(1)<=s(2)
??? Error using
Mat. hiểu n0 là sym
hoặc string.
>> s=double(solve(f));
>> s(1)<=s(2)
ans =
0
>> s(1)>=s(2)
ans =
1
>> s(2)+s(3) ==1
ans =
1
>> s'
s' = 2.0000 1.6180 -0.6180
Phép toán so sánh string (chuỗi ký tự)
strcmp(A,B): so sánh 2 chuỗi có trùng nhau hay không
strncmp(A,B,n): so sánh n ký tự đầu của 2 chuỗi.
Kết quả: 0, 1.
>> strcmp('yes','no')
ans =
0
>> strcmp('yes','yes')
ans =
1
>> strncmp('yes','year',2)
ans =
1
Các phép toán Logic (Kết quả: 1(đúng), 0(sai)).
Hay: or(A, B); A|B|C…;A||B||C…
>> (1==2)|(1~=2)
ans =
1
>> (1==2)||(1>2)
ans =
0
>> (1==2)||(1>2)|(1<2)
ans =
1
Các phép toán Logic (Kết quả: 1(đúng), 0(sai)).
Và: and(A, B); A&B&C…;A&&B&&C…
>> (1<2)&&(1~=2)
ans =
1
>> (1<2)&(1==2)
ans =
0
>> (1<=2)&(1~=2)&&(1+3-4==0)
ans =
1
Các phép toán Logic (Kết quả: 1(đúng), 0(sai)).
Phủ định: not(A); ~A
>> 1-2==0
ans =
0
>> not(1-2~=0)
ans =
0
>> ~(1-2~=0)
ans =
0
Lệnh limit (tính giới hạn)
limit(f,x,x0)
limit(f,x,x0, 'left')
limit(f,x,0,'right')
>> f=sin(x)/x;
>> limit(f,x,0)
ans =
1
>> limit(f,x,inf)
ans =
0
>> limit(1/x,x,0)
ans =
NaN (không tồn tại)
>> limit(exp(1/x),x,0)
ans =
NaN
>> limit(exp(1/x),x,0,'left')
ans =
0
>> limit(exp(1/x),x,0,'right')
ans =
Inf
Lệnh diff (tính đạo hàm)
diff(f) = f’(x)
diff(f,n) = f(n)(x)
diff(f,n,dim): đạo hàm cấp n theo biến được chỉ ra trong dim
>> f=sin(x^2);
>> diff(f)
ans =
2*x*cos(x^2)
>> diff(f,2)
ans =
2*cos(x^2) - 4*x^2*sin(x^2)
>> syms x y
>> f=sin(x^2*y);
>> diff(f,2,y)
ans =
-x^4*sin(x^2*y)
>> diff(f,2)
ans =
2*y*cos(x^2*y) - 4*x^2*y^2*sin(x^2*y)
(Mặc định là tính đạo hàm theo x)
Lệnh int (tính tích phân)
int(f)
int(f,a,b) (a, b có thể là biến)
double(int(f,a,b))
>> int((x-2)/(x^3+1),0,inf)
>> syms x
>> f=x^2;
>> int(f)
ans =
x^3/3
>> int(f,1,2)
ans =
7/3
ans =
-(2*pi*3^(1/2))/9
>> syms a b
>> int(f,a,b)
ans =
b^3/3 - a^3/3
>> f=sin(x)/x;
>> int(f)
ans =
sinint(x)
>> int(f,1,2)
ans =
sinint(2) - sinint(1)
>> double(int(f,1,2))
ans =
0.6593
Vẽ đồ thị: hàm ezplot
ezplot ( f )
ezplot  f ,  x m in, x m ax   ; ezplot ( f , x m in, x m ax)
ezplot  x ( t ), y ( t ) 
ezplot  x ( t ), y ( t ),  t m in, t m ax  
x3 - 3 x2 + 1
x3 - 3 x2 + 1
150
16
100
14
50
12
0
10
-50
8
-100
6
-150
4
-200
2
-250
0
-300
-2
-350
-4
-6
-4
-2
0
x
2
4
6
0
0.5
1
1.5
2
x
2.5
>> syms x
>> ezplot(f,0,4)
>> f=x^3-3*x^2+1;
>> ezplot(f,[0,4])
>> ezplot(f)
3
3.5
4
x3 - 3 x2 + 1
16
14
12
10
8
6
4
2
0
-2
-4
0
0.5
1
1.5
2
x
2.5
3
3.5
4
>> h=ezplot(f,[0,4]);
>> set(h,'Color','g','LineWidth',2,'LineStyle','--')
Vẽ đồ thị: hàm plot
x  linpace  a , b , n  ;
y  subs ( f );
plot ( y , ' L ineSpec ', ' P ropertiesN am e  )
plot ( x , y , L ineSpec ', ' P ropertiesN am e )
t  linpace  a , b , n  ;
x  subs  x ( t )  ; y  subs ( y ( t ));
plot ( x , y , ' L ineSpec ', ' P ropertiesN am e )
plot ( x , y , L ineSpec ', ' P ropertiesN am e )
20
15
10
5
0
-5
0
0.5
1
1.5
2
2.5
3
>> x=linspace(0,4,50);
>> y=x.^3-3*x.^2+1;
>> plot(y);
3.5
4
20
15
10
20
5
15
0
10
-5
0
0.5
1
1.5
2
2.5
>> plot(x,y,‘*r');
3
3.5
4
5
0
-5
0
0.5
1
1.5
2
2.5
3
3.5
4
>> plot(x,y,'Color','r','LineWidth',2,'LineStyle','--')
x 3-3*x 2+1
20
15
10
5
0
-5
0
0.5
1
1.5
2
2.5
3
>> plot(x,y,'*r')
>> title('x^3-3*x^2+1')
>> grid on
3.5
4
>> syms x
>> f=x^3 - 3*x^2 + 1
>> x0=-1;y0=subs(f,x,x0);
>> k=subs(diff(f),x,x0);
>> X=linspace(x0-3,x0+3,30);
>> Y=subs(f,x,X);Y1=subs(y,x,X);
>> plot(X,Y,'Color','b','LineWidth',2); hold on
>> plot(X,Y1,'Color','r','LineWidth',2);
>> plot(x0,y0,'om','MarkerFaceColor','m');
>> legend('duong cong','tiep tuyen','tiep diem')
>> grid on
>> title('Minh hoa do thi va tiep tuyen','Color','c’)
Minh hoa do thi va tiep tuyen
40
duong cong
tiep tuyen
tiep diem
20
0
-20
-40
-60
-80
-100
-120
-4
-3
-2
-1
0
1
2
Viết code
File  New  Script/ Function: chuyển sang Editor
Editor
Sau khi soạn phải lưu thành file.
Đặt tên và chọn nơi lưu file.
Chọn save để lưu file (ở đây lưu vào desktop)
Bấm F5 để chạy chương trình từ Editor.
Chọn ‘Change Folder’/ ‘Add to Path’.
Chương trình xuất yêu cầu hoặc kết quả ở Command W.