You are on page 1of 34

23/09/2016

MatLab R2013b
1. Cài đặt

2. Các vấn đề cơ bản

3. Mô phỏng bằng Simulink

4. Mô phỏng bằng mã lệnh


1

Cài đặt
Bước 1: Chạy file Setup.exe, chọn Install without using
Internet
Bước 2: Chọn I have the File Installation Key for my license và
nhập key 12345678901112131415.

Bước 3: Khi được hỏi file license, chỉ đến file license.dat trong thư
mục CYGiSO

Bước 4: Copy file dll trong thư mục CYGiSO đè lên file có trong thư
mục cài đặt MatLab (x64 nếu Win 64 bit, x86 nếu Win 32 bit)

1
23/09/2016

MatLab R2013b
1. Cài đặt

2. Các vấn đề cơ bản

3. Mô phỏng bằng Simulink

4. Mô phỏng bằng mã lệnh


3

Các tools đã cài đặt Simulink

Cửa sổ lệnh

2
23/09/2016

Chọn New > Graphical User Interface để


tạo giao diện cho chương trình

Chọn Blank GUI

Các đối tượng tạo giao diện

Vùng thiết kế

3
23/09/2016

Các đối tượng tạo giao diện

Push Button: Edit Text: ô nhập dữ liệu

Radio Button: Pop-up Menu:

Slider: Check box:

List Box:
Static Text: hiện văn bản tĩnh.

Axes: trục toạ độ

4
23/09/2016

Chọn Tools > Menu Editor để tạo menu

10

5
23/09/2016

Tạo menu như sau:

Label Tag Label Tag


File mnuFile Exit mnuExit
Help mnuHelp About mnuAbout
11

Thiết kế giao diện như sau:

Để chọn thuộc tính đối tượng, double-click vào đối


tượng để mở cửa sổ Inspector

12

6
23/09/2016

Các thuộc tính cần chú ý:


String: giá trị hiển thị trên đối tượng.
Tag: tên của đối tượng, dùng để phân biệt với các đối tượng khác.
FontName: tên font của văn bản hiện trên đối tượng.
FontSize: kích thước font.
Unit: đơn vị xác định vị trí đối tượng, nên chọn là normalized.
Value: giá trị của đối tượng, dùng cho các đối tượng Popup-menu, List Box, Radio
Button, Check Box, Slider.
Callback: mã lệnh thực hiện khi tác động lên đối tượng (nên dùng file .m để hiệu
chỉnh mã lệnh)

13

Đối tượng Thông số


Nút nhấn String: Convert
Tag: pushbuttonConvert
3 Radio Button String: Celcius, Rankine, Kelvin
Value: 1.0, 0.0, 0.0
Tag: radiobuttonC, radiobuttonR,
radiobuttonK
Static text String: Fahrenheit degree
Static text String: Celcius degree
Tag: text2
Slider Max: 9032
Min: -459.4
SliderStep: [0.001 0.1]
Tag: SliderDoF
Edit text String: 32
Tag: editDoF
Edit text String: 0
Tag: editKQ

14

7
23/09/2016

Nhấn Save lưu file: nhận tên vd1

Cửa sổ soạn mã lệnh hiện ra:

15

Dùng Go To chuyển đến các đối tượng mong muốn:

16

8
23/09/2016

function pushbuttonConvert_Callback(hObject, eventdata,


handles)
% hObject handle to pushbuttonConvert (see GCBO)
% eventdata reserved - to be defined in a future version of
MATLAB
% handles structure with handles and user data (see
GUIDATA)
doF = str2num(get(handles.editDoF,'String'));
if get(handles.radiobuttonC,'Value') == 1
set(handles.editKQ,'String',num2str((doF-32)*5/9));
elseif get(handles.radiobuttonR,'Value') == 1
set(handles.editKQ,'String',doF+459.7);
else
set(handles.editKQ,'String',(doF-32)*5/9+273.15);
end

17

function radiobuttonC_Callback(hObject, eventdata, handles)


% hObject handle to radiobuttonC (see GCBO)
% eventdata reserved - to be defined in a future version of
MATLAB
% handles structure with handles and user data (see
GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of
radiobuttonC
set(handles.radiobuttonK,'Value',0);
set(handles.radiobuttonR,'Value',0);
set(handles.text2,'String','Celcius degree');
18

9
23/09/2016

function radiobuttonR_Callback(hObject, eventdata, handles)


% hObject handle to radiobuttonR (see GCBO)
% eventdata reserved - to be defined in a future version of
MATLAB
% handles structure with handles and user data (see
GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of
radiobuttonR
set(handles.radiobuttonC,'Value',0);
set(handles.radiobuttonK,'Value',0);
set(handles.text2,'String','Rankine degree'); 19

function radiobuttonK_Callback(hObject, eventdata, handles)


% hObject handle to radiobuttonK (see GCBO)
% eventdata reserved - to be defined in a future version of
MATLAB
% handles structure with handles and user data (see
GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of
radiobuttonK
set(handles.radiobuttonR,'Value',0);
set(handles.radiobuttonC,'Value',0);
set(handles.text2,'String','Kelvin degree'); 20

10
23/09/2016

function sliderDoF_Callback(hObject, eventdata, handles)


% hObject handle to sliderDoF (see GCBO)
% eventdata reserved - to be defined in a future version of
MATLAB
% handles structure with handles and user data (see
GUIDATA)

% Hints: get(hObject,'Value') returns position of slider


% get(hObject,'Min') and get(hObject,'Max') to
determine range of slider
set(handles.editDoF,'String',get(handles.sliderDoF,'Value'));
21

function mnuExit_Callback(hObject, eventdata, handles)


% hObject handle to mnuExit (see GCBO)
% eventdata reserved - to be defined in a future version of
MATLAB
% handles structure with handles and user data (see GUIDATA)
close

function mnuAbout_Callback(hObject, eventdata, handles)


% hObject handle to mnuAbout (see GCBO)
% eventdata reserved - to be defined in a future version of
MATLAB
% handles structure with handles and user data (see GUIDATA)
msgbox('Help About','Help','help');

22

11
23/09/2016

MatLab R2013b

1. Cài đặt

2. Các vấn đề cơ bản

3. Mô phỏng bằng Simulink

4. Mô phỏng bằng mã lệnh

23

Simulink

- Cho phép thiết kế dạng sơ đồ khối.

- Bao gồm nhiều thư viện có sẵn: Communication System, DSP


System, Control System …

- Khởi động: gõ lệnh Simulink hay chọn Simulink Library

24

12
23/09/2016

- Cửa sổ Simulink

New Model

Các thư viện

Các thành phần trong thư viện

25
- Nhấn New Model để tạo cửa sổ thiết kế

- Kéo và thả khối Signal Generator (trong thư viện Simulink >
Sources) vào cửa sổ thiết kế

26

13
23/09/2016

- Thêm các khối và nối liên kết như hình


Simulink > Sinks > Scope

Simulink > Signal Routing > Mux

Simulink > Sources > Sine Wave

Simulink > Math Operations >


Product

Simulink Extras > Additional Sinks


> Power Spectral Density
27

- Double-click vào khối Signal - Khối Sine Wave


Generator và chọn thông số về
biên độ, tần số

28

14
23/09/2016

- Khối Power Spectral Density

- Vào menu Simulation > Model Configuration Parameters

29

- Chọn các thông số như hình

- Nhấn Run chạy mô phỏng


30

15
23/09/2016

31

Simulink > Math Operations >


Simulink > Sources > Constant
Add

- Khối Sine Wave: Amplitude = 1

- Khối Constant: Constant Value = 1


32

16
23/09/2016

33

- Dùng công cụ có sẵn trong Simulink DSP System Toolbox > Sinks >
Spectrum Analyzer

DSP System Toolbox > Sources >


Sine Wave DSP System Toolbox > Sinks >
Time Scope

Communications System Toolbox >


Modulation > Analog Passband Modulation

34

17
23/09/2016

35

Communications System Toolbox >


Modulation > Analog Passband Modulation

36

18
23/09/2016

37

f m u

u: lực kéo của động cơ (ngõ vào của mô hình).


f: lực ma sát.

Phương trình biểu diễn như sau:

mv' = u – bv hay v' = (u – bv)/m

m: khối lượng xe.


v: vận tốc xe (ngõ ra của mô hình).
b: hệ số ma sát.

38

19
23/09/2016

v = v1dt  v1 = v'.

Simulink > Continuous > Integrator

Simulink > Math > Gain

v'
bv

v' = (u – bv)/m
u – bv
bv

u
39

Simulink > Sources > Step

40

20
23/09/2016

Mô hình điều khiển vị trí động cơ DC

J = 0.01 (moment quán tính của rotor)


b = 0.1 (hệ số giảm chấn của hệ thống cơ khí)
K = Kt = Ke = 0.01 (hằng số sức điện động)
R = 10 (điện trở dây quấn)
L = 0.5 (hệ số tự cảm)
V: ngõ vào (điện áp đặt trên cuộn dây động cơ)
: ngõ ra (vị trí trục quay)
i: dòng điện chạy trong cuộn dây

41

MatLab R2013b

1. Cài đặt

2. Các vấn đề cơ bản

3. Mô phỏng bằng Simulink

4. Mô phỏng bằng mã lệnh

42

21
23/09/2016

Từ cửa sổ lệnh, gõ edit

Nhập đoạn mã: f = [1 1];


i = 1;
while(f(i)+f(i+1))<1000
f(i + 2)= f(i) +f(i+1);
i = i + 1;
end
plot(f)

Nhấn Save để lưu Đặt tên file là fibo:

43

Từ cửa sổ lệnh, gõ fibo


1000

900

800

700

600

500

400

300

200

100

0
0 2 4 6 8 10 12 14 16

- Script file không có đối số.

‐ Dùng khi thi hành một loạt lệnh MATLAB theo một trình tự nhất định

44

22
23/09/2016

Nhập đoạn mã: function y = tb(x)


%Tinh tri trung binh cua cac phan tu
[m,n] = size(x);
if m == 1
m = n;
end
y = sum(x)/m;

Đặt tên file là tb Dòng xuất hiện khi gõ lệnh help tb

Một hàm M-file gồm các phần cơ bản sau:

Một dòng định nghĩa hàm: function y = tb(x) gồm từ khoá function, đối số trả về y,
tên hàm tb và đối số vào x.

Thân hàm chứa mã MATLAB.

Tên hàm phải bắt đầu bằng ký tự và cùng tên với file chứa hàm.
Tên hàm là tb thì tên file là tb.m

Từ cửa sổ lệnh, gõ ans =


z = 1:99;
tb(z) 45
50

Vẽ đồ thị
Giữ hình đã vẽ trước đó

Vẽ đồ thị hàm số y1=sinx.cos2x và hàm số y2=sinx2 trong [0-2π]


x=0:0.01:2*pi;
y1=sin(x).*cos(2*x);
Định lại trục tọa độ
plot(x,y1)
grid on

hold on
y2=sin(x.^2);
plot(x,y2,’k’); Thêm các tiêu đề trên trục tọa độ
axis([0 4*pi –1.25 1.25])

xlabel('Time’)
ylabel(‘Amplitude’)
title(‘y1=sinx.cos2x and y2=sin(x^2)’)
legend(‘sinx.cos2x’,’sinx^2’)
46

23
23/09/2016

y1=sinx.cos2x and y2=sin(x 2)

sinx.cos2x
1
sinx 2

0.5
Amplitude

-0.5

-1

0 2 4 6 8 10 12
Time

47

Dùng hàm subplot để vẽ nhiều trục toạ độ

subplot(2,3,5) %2,3: xác định có 2 hàng, 3 cột


% 5: chọn trục thứ 5 (đếm từ trái sang phải,
trên xuống dưới)

48

24
23/09/2016

x=0:0.01:2*pi;
y2=sin(x.^2);
y1=sin(x).*cos(2*x);
subplot(2,1,2);
subplot(2,1,1)
plot(x,y2,'k');
plot(x,y1)
xlabel('Time');
xlabel('Time');
ylabel('Amplitude');
ylabel('Amplitude');
title('sinx^2');
title('sinx.cos(2x)');
sinx.cos(2x)
1

0.5
Amplitude

-0.5

-1
0 1 2 3 4 5 6 7
Time

sinx 2
1

0.5
Amplitude

-0.5

-1
0 1 2 3 4 5 6 7 49
Time

Có thể thêm văn bản vào bất kỳ chỗ nào trên hình vẽ nhờ hàm text.
text(300,.25*exp(‐.005*300),’\bullet\leftarrow\fontname
{times}0.25{\ite}^(‐0.005{\itt}}
at,{\itt}=300’,ʹFontSize’,14)

Để thêm các công thức toán học, ta dùng dạng LaTeX

Biên dịch dạng latex

text('units','inch','position',[.2 5], 'fontsize',14,


'interpreter', 'latex', 'string' ,['$$\hbox {magic(3) is }
\left( {\matrix{ 8 & 1 & 6 \cr' '3 & 5 & 7 \cr 4 & 9 & 2 } }
\right)$$']);

50

25
23/09/2016

Chỉ số trên và dưới: ^ và _


text('units','inch','position',[.2 5], 'fontsize',14,
'interpreter', 'latex', 'string',['$$a^{b^2}_{123}$$']);

Nhóm biểu thức: \overline, \underline, \overbrace, \underbrace


['$$a = \overbrace{abc}^{123}$$']);
['$$b = \underbrace{abc}_{123}$$']);
['$$c = \overline{abc}$$']);
['$$d = \underline{123}$$']);

51

Dạng phân số: \frac

['$$lim_{n\rightarrow\infty}\frac{2n+1}{n^2}=0$$']

Tích phân, tổng, căn bậc hai: \int, \sum, \sqrt

['$$\sum_{n=0}^{\infty}a_nx^n$$']
['$$\int_{a}^{b}e^{x^2}dx$$']
['$$\sqrt{\sum_{i=1}^Nf(x_i)}$$']

Các dấu chấm: \ldots, \cdots, \vdots, \ddots, \cdot

['$$a \ldots b \vdots c \cdots d \ddots e \cdot$$']

Các dấu vi phân: \dot, \ddot


['$${\ddot x} - 2\dot x ^2= x^2$$'] 52

26
23/09/2016

Các dấu ngoặc: \left và \right, sau đó là các dấu ngoặc như (, ), |, [, ]

Chú ý là phải luôn có cả \left và \right

Ma trận: {\matrix{ }}

['$${\matrix{\cos(\phi) & -\sin(\phi) \cr'...


'\sin(\phi) & \cos(\phi)}}$$']

['$$A = det\left|{\matrix{2 & -5 & x^2


\cr -2 & x^3 \cr & & \sqrt{x}}}
\right|$$']

53

54

27
23/09/2016

55

56

28
23/09/2016

57

58

29
23/09/2016

sin
2 1
['$$\int_{-\infty}^{+\infty}\frac{e^{-
x}+e^x}{\sqrt{2x+1}}sin\left(\omega(x - \tau)\right)dx$$']

['$$\ddot y = x\left[{\matrix{x & x^2 & x^3 \cr x^2 & x^3 &
x \cr x^3 & x & x^2}}\right]$$']

1

['$$\Delta\dot {\vec{Z_e}} + k^2\dot {\vec{Z_e}} = -


\frac{1}{j\omega\epsilon}\dot{\vec{J}}_{ng}$$']

59

Vẽ mặt paraboloid z = x2+y2


Vẽ đồ thị 3D
t=-5:0.1:5;
[x,y]=meshgrid(t); subplot(2,2,3), meshz(z)
z=x.^2+y.^2; title('meshz(z)')
subplot(2,2,1), mesh(z) subplot(2,2,4), waterfall(z)
title('mesh(z)') title('waterfall(z)')
subplot(2,2,2), meshc(z)
title('meshc(z)')

60

30
23/09/2016

subplot(2,2,1), surf(z)
title('surf(z)')
subplot(2,2,2), surfc(z)
title('surfc(z)')
subplot(2,2,3), surfl(z)
title('surfl(z)')
subplot(2,2,4)

axis tight
camlight right
surf(x,y,z,'EdgeColor','none', ...
'FaceColor',[0.9 0.1 0.2], ...
'FaceLighting','phong', ...
'AmbientStrength',0.3, ...
'DiffuseStrength',0.6, ...
'Clipping','off',...
'BackFaceLighting','lit', ...
'SpecularStrength',1, ...
'SpecularColorReflectance',1, ...
'SpecularExponent',7) %colored faces
l1 = light('Position',[40 100 20],'Style','local', ...
'Color',[0 0.8 0.8]);
l2 = light('Position',[.5 -1 .4], 'Color',[0.8 0.8 0]); 61
title('surf(z) with light')

62

31
23/09/2016

t = 0:0.005:4*pi;
Ω fm =100;
ft = 10;V0 = 5;
vam = V0*(cos(ft*t)).*cos(fm*t);
subplot(2,1,1);
plot(t,vam)
axis([0 4 -10 10]);
subplot(2,1,2);
pwelch(vam);
10

-5

-10
0 0.5 1 1.5 2 2.5 3 3.5 4

Welch Power Spectral Density Estimate


40
Power/frequency (dB/rad/sample)

20

-20

-40

-60
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 631
Normalized Frequency ( rad/sample)

1 Ω t = 0:0.001:2*pi;
fm = 200;m = 0.5;
ft = 40;V0 = 5;
vam = V0*(1+m*cos(ft*t)).*cos(fm*t);
subplot(2,1,1);
plot(t,vam)
axis([0 2 -10 10]);
subplot(2,1,2);
pwelch(vam);
10

-5

-10
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2

Welch Power Spectral Density Estimate


40
Power/frequency (dB/rad/sample)

20

-20

-40

-60
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
Normalized Frequency ( rad/sample) 64

32
23/09/2016

t = 0:.001:2*pi;
y = square(2*pi*t);y=(1+y)/2;
xc = sin(2*pi*5*t);
Tín hiệu nhị phân xask = xc.*y;
(đơn cực) subplot(2,1,1);
plot(t,xask)
axis([0 4 -2 2]);
subplot(2,1,2);
pwelch(xask);
2

-1

-2
0 0.5 1 1.5 2 2.5 3 3.5 4

Welch Power Spectral Density Estimate


20
Power/frequency (dB/rad/sample)

-20

-40

-60

-80
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
Normalized Frequency ( rad/sample) 65

y2 = (1-y)/2;
Tín hiệu nhị phân m(t) +
xc2 = sin(2*pi*5*t); BFSK
(đơn cực) x2 = xc2.*y2;
Acos 2 xfsk = x1+x2;
+
Đảo subplot(2,1,1);
bit Acos 2 plot(t,xfsk)
axis([0 4 -2 2]);
t = 0:.001:2*pi;
subplot(2,1,2);
y = square(2*pi*t);
pwelch(xfsk);
y1=(1+y)/2;
xc1 = sin(2*pi*10*t);
2
x1 = xc1.*y1;
1

-1

-2
0 0.5 1 1.5 2 2.5 3 3.5 4

Welch Power Spectral Density Estimate


20
Power/frequency (dB/rad/sample)

-20

-40

-60

-80
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 66
Normalized Frequency ( rad/sample)

33
23/09/2016

t = 0:.001:2*pi;
Tín hiệu nhị phân y = square(2*pi*t);
BPSK
(lưỡng cực) xc = sin(2*pi*10*t);
xpsk = xc.*y;
subplot(2,1,1);
Acos 2 plot(t,xpsk)
axis([0 4 -2 2]);
subplot(2,1,2);
pwelch(xpsk);
2

-1

-2
0 0.5 1 1.5 2 2.5 3 3.5 4

Welch Power Spectral Density Estimate


20
Power/frequency (dB/rad/sample)

-20

-40

-60

-80
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
Normalized Frequency ( rad/sample) 67

34

You might also like