1. LẬP TRÌNH GUI MATLAB KHẢO SÁT ĐẶC TÍNH CỦA HỆ
THỐNG TỰ ĐỘNG
MR.TRONGBK
BACH KHOA
HA NOI BÀI TẬP DÀI MÔ HÌNH HÓA VÀ MÔ PHỎNG
2. http://quangcaobkvietnam.com BÀI TẬP DÀI MÔ HÌNH HÓA VÀ MÔ PHỎNG
Tác giả: facebook.com/mr.trongbk 1
Đề 4: cho mô hình sau
Các yêu cầu chung :
Với sơ đồ cấu trúc trên, Viết chương trình mô phỏng có giao diện đồ họa để xét tính ổn
định của hệ thống.
Chương trình mô phỏng có các yêu cầu sau:
1. Chương trình có thể viết bằng ngôn ngữ C/C++, VC, VB, Matlab ….
2. Cho phép người dùng nhập các thông số T1, T2, T3, K1, K2 …
3. Cho phép người dùng nhập bước cắt mẫu T
4. Hiển thị đường cong quá độ trên hệ trục có khắc tọa độ để đánh giá tính ổn định
của hệ thống
5. Thể hiện được các tiêu chí như ymax , σ% , yod , Tmax , Tod … trên đồ thị
6. In ra màn hình 100 điểm y[k] ( cứ 10 điểm in ra một giá trị )
3. http://quangcaobkvietnam.com BÀI TẬP DÀI MÔ HÌNH HÓA VÀ MÔ PHỎNG
Tác giả: facebook.com/mr.trongbk 2
CHƯƠNG 1. MÔ HÌNH HỆ THỐNG
1.1. Hàm truyền hệ kín trong miền thời gian
Hình 1.1. Sơ đồ khối của hệ thống
Hàm truyền kín của hệ thống :
1 2
1 2 1 2
3 2
1 2 1 2 1 2 1 2
1 2
1
. .
1 1 .
(s)
1 . . .1 . .
1 1
K K
s T s T s K K
G
K K T T s T T s s K K
s T s T s
Giá trị đâu ra khi hệ thống ở chế độ xác lập :
0 0 0
1
lim (t) lim (s) lim (s).W(s) lim (s). (0) 1
t s s s
y y sY sG sG G
s
1.2. Hàm truyền hệ thống trong miền rời rạc
Để mô tả hệ thống trên miền rời rạc , thay
2 1
.
1
z
s
T z
Thu được :
1 2
3 2
1 2 1 2 1 2
3 3 2
1 2
3 2
.
(s)
2 1 2 1 2 1
. . . . . .
1 1 1
. . 3 3 1
. . .
K K
G
z z z
T T T T K K
T z T z T z
K K T Z Z Z
A Z B Z C Z D
Trong đó các hệ số A, B, C, D được xác định như sau:
2 3
1 2 1 2 1 2
2 3
1 2 1 2 1 2
8 4( )T 2T .
24 4( )T 2T 3. .
A TT T T K K T
B TT T T K K T
y
1
W
--
e
4. http://quangcaobkvietnam.com BÀI TẬP DÀI MÔ HÌNH HÓA VÀ MÔ PHỎNG
Tác giả: facebook.com/mr.trongbk 3
2 3
1 2 1 2 1 2
2 3
1 2 1 2 1 2
24 4( )T 2T 3. .
8 4( )T 2T .
C TT T T K K T
D TT T T K K T
Ta có hàm sai phân như sau:
3 3 2
1 2
3 2
3 2 3 3 2
1 2
. . 3 3 1(z)
W(z) . . .
. . (z) . . (z) . . (z) . (z) . . .W(z) 3 .W(z) 3 .W(z) W(z)
K K T Z Z ZY
A Z B Z C Z D
A Z Y B Z Y C Z Y DY K K T Z Z Z
dùng tính chất dịch gốc của biến đổi Z ta tìm được phương trình sai phân tương ứng
với phương trình trên :
3
1 2. [k 3]+B. [k 2]+C. [k 1]+D. [k]=K . . W[k 3] 3.W[k 2] 3.W[k+1] W[k]AY Y Y Y K T
do tín hiệu vào là hàm bước nhảy W(t) = 1(t) nên ta có :
W[k+3] = W[k+2] = W[k+1] = W[k] = 1
Vậy ta có : 3
1 2. [k 3]+B. [k 2]+C. [k 1]+D. [k ]=8.K . .AY Y Y Y K T
Vậy cuối cùng ta tìm được phương trình sai phân của hệ điều khiển tự động
như sau:
3
1 2. [k 2] . [k 1] . [k ] + 8.K K T
[k 3]
BY C Y DY
Y
A
Từ phương trình sai phân này ta có thể lập trình trên máy tính để tìm được đáp
ứng y(t) khi tín hiệu vào là hàm bước nhảy đơn vị 1(t).
1.3. Tính ổn định của hệ thống
Tiêu chuẩn jury tương tự như tiêu chuẩn Routh – Huwitz được sử dụng để
phân tích hệ thống số.
Phương trình đặc tính của hệ thống : 3 2
(Z) . . .Z DF A Z B Z C
Theo tiêu chuẩn Jury điều kiện cần và đủ để hệ thống ổn định là :
a 0 (1)
1 ( 1) A B C D
det det
F A B C D
a F
D A
D A D C
A
A D A D
Từ điều kiện này ta có thể lập trình kiểm tra tính ổn định của hệ thống
5. http://quangcaobkvietnam.com BÀI TẬP DÀI MÔ HÌNH HÓA VÀ MÔ PHỎNG
Tác giả: facebook.com/mr.trongbk 4
CHƯƠNG 2 : GIAO DIỆN VÀ CHƯƠNG TRÌNH MÔ PHỎNG
2.1 Giao diện đồ họa của chương trình
Hình 2.1. giao diện đồ họa
Giao diện gồm có :
- Panel Tham số hệ thống : đó là nơi người dùng nhập các giá trị cho K1 , K2
, T1 , T2 , T cho hệ thống.
- Panel Điều Khiển : có ba nút điều khiển Start để chạy chương trình mô
phỏng và nút sơ đồ đề xem cấu trúc của hệ thống, nút Cancel để thoát
chương trình mô phỏng
- Panel đặc tính : mơi hiển thị một số thông số quan trọng của hệ thống như :
ymax , σ% , yod , Tmax , Tod …
- Có khung tọa độ dùng để hiện thị đường cong đáp ứng quá độ của hệ thống
- Có cửa số hiển thị thông báo xem hệ thống có ổn định hay không
7. http://quangcaobkvietnam.com BÀI TẬP DÀI MÔ HÌNH HÓA VÀ MÔ PHỎNG
Tác giả: facebook.com/mr.trongbk 6
2.2 Sơ đồ khối của chương trình
2.2.1 lưu đồ khố của toàn bộ chương trình
.
program
Nhập các tham số :
+hệ số khuếch đại: K1, K2
+ hằng số thời gian : T1,T2
+ chu kỳ cắt mẫu : T
Nút cancel
HT ổn định không?
Kết
thúc
NO
NO
YES
Nút Start
NO
YES
YES
YES
NO
+ Vẽ đồ thị
+ vẽ hiển thị thông tin lên đồ thị
+ Thông báo HT ổn định
+ xuất thông tin về : ymax , σ% , yod ,
Tmax , Tod lên Static Text
+ nếu nút sơ đồ đc ấn thì mở sơ đồ khối
+ Vẽ đồ thị
+ Thông báo HT không ổn
định
+ nếu nút sơ đồ đc ấn thì
mở sơ đồ khối
Nút cancel
So do
CT con
8. http://quangcaobkvietnam.com BÀI TẬP DÀI MÔ HÌNH HÓA VÀ MÔ PHỎNG
Tác giả: facebook.com/mr.trongbk 7
2.2.2 Lưu đồ khối giải phương trình sai phân
Code lập trình trên matlab :
%======================================================================
% khoi tao gia tri dau
%======================================================================
y(1)=0;
y(2)=0;
y(3)=0;
%======================================================================
% giai phuong trinh sai phan
%======================================================================
for k = 1 : 1000
y(k+3) = ((-B*y(k+2)-C*y(k+1)-D*y(k)+8*K1*K2*T*T*T))/A;
end
Begin
K = 1
Y[k+3] = ( - BY[k+2]-Cy[k+1]-DY[k]+8K1K2T3
) /A
K++
End
YES
NO
K =1000
9. http://quangcaobkvietnam.com BÀI TẬP DÀI MÔ HÌNH HÓA VÀ MÔ PHỎNG
Tác giả: facebook.com/mr.trongbk 8
2.2.3 Lưu đồ in ra 100 giá trị y[k]
Code lập trình trên matlab :
disp('100 gia tri cua y')
for k = 1 : 1000
if(mod(k,10)==0)
fprintf('y[%d]=%fn',k,y(k))
end
end
Begin
K = 1
K++
End
YES
NO
K =1000
In ra y[k]
NO
Mod(k,10) = 0
YES
10. http://quangcaobkvietnam.com BÀI TẬP DÀI MÔ HÌNH HÓA VÀ MÔ PHỎNG
Tác giả: facebook.com/mr.trongbk 9
2.2.4 Lưu đồ khối tìm Ymax và Tmax
Code của matlab :
y_max = y(1);
for k = 1: 1000
if (y(k)>y_max)
y_max = y(k);
k_max = k;
end
end
Begin
K = 1
Max = y[1]
K++
YES
NO
K =1000
NO
YES
End
ymax =max
Tmax = Kmax*T
Max = y[k]
Kmax = k
Max < y[k]
11. http://quangcaobkvietnam.com BÀI TẬP DÀI MÔ HÌNH HÓA VÀ MÔ PHỎNG
Tác giả: facebook.com/mr.trongbk 10
y_max ;
Tmax = k_max*T ;
2.2.5 Lưu đồ khối tìm Tod
Tod là giá trị mà khí đo đáp ứng đầu ra luôn nằm trong khoảng giá trị xác
lập 2% giá trị xác lập tức đáp ứng nằm gọn trong [y_ss – 0.98*y_ss
y_ss+1.02*y_ss]
Code lập trình trên matlab :
k=1000;
while (y(k)>0.98*y_ss)&(y(k)<1.02*y_ss)
k=k-1;
Tod =k*T;
End
Begin
K = 1000
End
k = k – 1
Tod = k*T
NO
( y[k] > 0.98Y_ss )&( y[k] < 1.2Y_ss )
YES
12. http://quangcaobkvietnam.com BÀI TẬP DÀI MÔ HÌNH HÓA VÀ MÔ PHỎNG
Tác giả: facebook.com/mr.trongbk 11
2.3 Nhận xét về đường cong thu được v
Để minh họa hoạt động của chương trình chúng ta sẽ deno với các giá trị :
1
2
1
2
50
0.5
0.02
0.03
0.01
K
K
T
T
T
Khi chạy trương trình thu được kết quả như sau:
Hình 2.2. kết quả mô phỏng ứng với K1 =50, K2 = 0.5, T1 = 0.02 , T2 = 0.03 , T = 0.01
Và chương trình hiện thông báo như sau để người sử dụng biết rằng chương trình đã in
ra 100 giá trị y[k] ra file Data.txt được tạo ra trong cùng thư mục chứa chương trình :
Hình 2.3. thông báo đã in 100 giá trị ra file Data.txt
13. http://quangcaobkvietnam.com BÀI TẬP DÀI MÔ HÌNH HÓA VÀ MÔ PHỎNG
Tác giả: facebook.com/mr.trongbk 12
ở cửa sổ command windown chương trình sẽ in ra 100 giá trị của y[k] , cứ 10 giá trị thì
in ra 1 giá trị :
100 gia tri cua y
y[10]= 0.78 y[20]= 1.26 y[30]= 0.88 y[40]= 1.02 y[50]= 1.01
y[60]= 0.99 y[70]= 1.01 y[80]= 1.00 y[90]= 1.00 y[100]= 1.00
y[110]= 1.00 y[120]= 1.00 y[130]= 1.00 y[140]= 1.00 y[150]= 1.00
y[160]= 1.00 y[170]= 1.00 y[180]= 1.00 y[190]= 1.00 y[200]= 1.00
y[210]= 1.00 y[220]= 1.00 y[230]= 1.00 y[240]= 1.00 y[250]= 1.00
y[260]= 1.00 y[270]= 1.00 y[280]= 1.00 y[290]= 1.00 y[300]= 1.00
y[310]= 1.00 y[320]= 1.00 y[330]= 1.00 y[340]= 1.00 y[350]= 1.00
y[360]= 1.00 y[370]= 1.00 y[380]= 1.00 y[390]= 1.00 y[400]= 1.00
y[410]= 1.00 y[420]= 1.00 y[430]= 1.00 y[440]= 1.00 y[450]= 1.00
y[460]= 1.00 y[470]= 1.00 y[480]= 1.00 y[490]= 1.00 y[500]= 1.00
y[510]= 1.00 y[520]= 1.00 y[530]= 1.00 y[540]= 1.00 y[550]= 1.00
y[560]= 1.00 y[570]= 1.00 y[580]= 1.00 y[590]= 1.00 y[600]= 1.00
y[610]= 1.00 y[620]= 1.00 y[630]= 1.00 y[640]= 1.00 y[650]= 1.00
y[660]= 1.00 y[670]= 1.00 y[680]= 1.00 y[690]= 1.00 y[700]= 1.00
y[710]= 1.00 y[720]= 1.00 y[730]= 1.00 y[740]= 1.00 y[750]= 1.00
y[760]= 1.00 y[770]= 1.00 y[780]= 1.00 y[790]= 1.00 y[800]= 1.00
y[810]= 1.00 y[820]= 1.00 y[830]= 1.00 y[840]= 1.00 y[850]= 1.00
y[860]= 1.00 y[870]= 1.00 y[880]= 1.00 y[890]= 1.00 y[900]= 1.00
y[910]= 1.00 y[920]= 1.00 y[930]= 1.00 y[940]= 1.00 y[950]= 1.00
y[960]= 1.00 y[970]= 1.00 y[980]= 1.00 y[990]= 1.00 y[1000]= 1.00
da in ra du : 100 theo yeu cau!
Vậy là chương trình đã thực hiện được tất cả các yêu cầu đặt ra
14. http://quangcaobkvietnam.com BÀI TẬP DÀI MÔ HÌNH HÓA VÀ MÔ PHỎNG
Tác giả: facebook.com/mr.trongbk 13
Đối chiếu với nhập lệnh trực tiếp trên cửa sổ command windown của matlab
K1=50;
K2=0.5;
T1=0.02
T2=0.03
G = tf(K1*K2,[T1*T2 T1+T2 1 K1*K2])
step(G)
Thu được :
Hình 2.4. kết quả mô phỏng ứng với K1 =50, K2 = 0.5, T1 = 0.02 , T2 = 0.03 trên miền
thời gian trong cửa sổ command windown
Nhận xét :
- hai kết quả ở Hình 2.2. và Hình 2.3. có dạng đường cong giống nhau
- các thông số ở 2 hình thu được xấp sĩ nhau với sai lệch nhỏ không đáng kể
Step Response
Time (seconds)
Amplitude
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8
0
0.2
0.4
0.6
0.8
1
1.2
1.4
System: G
Peak amplitude: 1.33
Overshoot (%): 33
At time (seconds): 0.15
System: G
Settling Time (seconds): 0.465
System: G
Final Value: 1
15. http://quangcaobkvietnam.com BÀI TẬP DÀI MÔ HÌNH HÓA VÀ MÔ PHỎNG
Tác giả: facebook.com/mr.trongbk 14
PHỤ LỤC A
Code đầy đủ của chương trình mô phỏng GUI_MHH_MP_BAI1.m
function varargout = GUI_MHH_MP_BAI1(varargin)
% GUI_MHH_MP_BAI1 MATLAB code for GUI_MHH_MP_BAI1.fig
% GUI_MHH_MP_BAI1, by itself, creates a new GUI_MHH_MP_BAI1 or raises
the existing
% singleton*.
%
% H = GUI_MHH_MP_BAI1 returns the handle to a new GUI_MHH_MP_BAI1 or the
handle to
% the existing singleton*.
%
% GUI_MHH_MP_BAI1('CALLBACK',hObject,eventData,handles,...) calls the
local
% function named CALLBACK in GUI_MHH_MP_BAI1.M with the given input
arguments.
%
% GUI_MHH_MP_BAI1('Property','Value',...) creates a new GUI_MHH_MP_BAI1
or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before GUI_MHH_MP_BAI1_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to GUI_MHH_MP_BAI1_OpeningFcn via
varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help GUI_MHH_MP_BAI1
% Last Modified by GUIDE v2.5 17-Sep-2013 01:41:30
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @GUI_MHH_MP_BAI1_OpeningFcn, ...
'gui_OutputFcn', @GUI_MHH_MP_BAI1_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
16. http://quangcaobkvietnam.com BÀI TẬP DÀI MÔ HÌNH HÓA VÀ MÔ PHỎNG
Tác giả: facebook.com/mr.trongbk 15
% --- Executes just before GUI_MHH_MP_BAI1 is made visible.
function GUI_MHH_MP_BAI1_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to GUI_MHH_MP_BAI1 (see VARARGIN)
% Choose default command line output for GUI_MHH_MP_BAI1
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes GUI_MHH_MP_BAI1 wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = GUI_MHH_MP_BAI1_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
Hinhnen=imread('bk.jpg');
axes(handles.Logo);
image(Hinhnen);
function editOvershoot_Callback(hObject, eventdata, handles)
% hObject handle to editOvershoot (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,'String') returns contents of editOvershoot as text
% str2double(get(hObject,'String')) returns contents of editOvershoot
as a double
% --- Executes during object creation, after setting all properties.
function editOvershoot_CreateFcn(hObject, eventdata, handles)
% hObject handle to editOvershoot (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'),
get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
17. http://quangcaobkvietnam.com BÀI TẬP DÀI MÔ HÌNH HÓA VÀ MÔ PHỎNG
Tác giả: facebook.com/mr.trongbk 16
end
function editYmax_Callback(hObject, eventdata, handles)
% hObject handle to editYmax (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,'String') returns contents of editYmax as text
% str2double(get(hObject,'String')) returns contents of editYmax as a
double
% --- Executes during object creation, after setting all properties.
function editYmax_CreateFcn(hObject, eventdata, handles)
% hObject handle to editYmax (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'),
get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function editYod_Callback(hObject, eventdata, handles)
% hObject handle to editYod (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,'String') returns contents of editYod as text
% str2double(get(hObject,'String')) returns contents of editYod as a
double
% --- Executes during object creation, after setting all properties.
function editYod_CreateFcn(hObject, eventdata, handles)
% hObject handle to editYod (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'),
get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
18. http://quangcaobkvietnam.com BÀI TẬP DÀI MÔ HÌNH HÓA VÀ MÔ PHỎNG
Tác giả: facebook.com/mr.trongbk 17
function editTmax_Callback(hObject, eventdata, handles)
% hObject handle to editTmax (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,'String') returns contents of editTmax as text
% str2double(get(hObject,'String')) returns contents of editTmax as a
double
% --- Executes during object creation, after setting all properties.
function editTmax_CreateFcn(hObject, eventdata, handles)
% hObject handle to editTmax (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'),
get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function editTod_Callback(hObject, eventdata, handles)
% hObject handle to editTod (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,'String') returns contents of editTod as text
% str2double(get(hObject,'String')) returns contents of editTod as a
double
% --- Executes during object creation, after setting all properties.
function editTod_CreateFcn(hObject, eventdata, handles)
% hObject handle to editTod (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'),
get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in ButtonStart.
function ButtonStart_Callback(hObject, eventdata, handles)
% hObject handle to ButtonStart (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
19. http://quangcaobkvietnam.com BÀI TẬP DÀI MÔ HÌNH HÓA VÀ MÔ PHỎNG
Tác giả: facebook.com/mr.trongbk 18
% handles structure with handles and user data (see GUIDATA)
%==========================================================================
% nhap lay du lieu tu cac editText
%==========================================================================
hold off;
K1=str2double(get(handles.editK1,'String'));
K2=str2double(get(handles.editK2,'String'));
T1=str2double(get(handles.editT1,'String'));
T2=str2double(get(handles.editT2,'String'));
T=str2double(get(handles.editT,'String'));
%======================================================================
% xet tinh on dinh
%======================================================================
clc
% ham truyen he kin G(z)= (az^3 + bz^2 + cz + a)/(Az^3 + Bz^2 + Cz + D)
% phuong trinh dac tinh F(Z) = Az^3 + Bz^2 + Cz + D
a = K1*K2*T*T*T;
b = 3*K1*K2*T*T*T;
c = 3*K1*K2*T*T*T;
d = K1*K2*T*T*T;
A = 8*T1*T2 + 4*T*(T1+T2) + 2*T*T + K1*K2*T*T*T ;
B = - 24*T1*T2 - 4*T*(T1+T2) + 2*T*T + 3*K1*K2*T*T*T;
C = 24*T1*T2 - 4*T*(T1+T2) - 2*T*T + 3*K1*K2*T*T*T ;
D = - 8*T1*T2 + 4*T*(T1+T2) - 2*T*T + K1*K2*T*T*T ;
% ap dung tieu chuan jury cho ham dac tinh co dang da thuc bac 3
a0 = A+B+C+D;
a1 = - A + B - C + D ;
a3 = abs(D*D - A*A);
a4 = abs(D*B - A*C);
% he on dinh khi (a0>0)&(a1<0)&(abs(D)<A)&(a3>a4) duoc thoa man
%======================================================================
% khoi tao gia tri dau cua phuong trinh sai phan
%======================================================================
y(1)=0;
y(2)=0;
y(3)=0;
%======================================================================
% giai phuong trinh sai phan
%======================================================================
axes(handles.dothi);
for k = 1 : 1000
y(k+3) = ((-B*y(k+2)-C*y(k+1)-D*y(k)+8*K1*K2*T*T*T))/A;
end
plot(T*[1:1000],y(1:1000),'r','LineWidth',1.5)
grid
%======================================================================
% In ra 100 gia tri cua y cu 10 gia tri in ra 1 lan ra cua so
% command windown
%======================================================================
20. http://quangcaobkvietnam.com BÀI TẬP DÀI MÔ HÌNH HÓA VÀ MÔ PHỎNG
Tác giả: facebook.com/mr.trongbk 19
disp('100 gia tri cua y')
% bien dem so gia tri in ra
m=0;
for k = 1 : 1000
if(mod(k,10)==0)
fprintf('y[%d]=%6.2f ',k,y(k))
if(mod(k,50)==0)
fprintf('n')
end
m = m+1;
end
end
fprintf('nda in ra du : %d theo yeu cau!n',m)
%======================================================================
% In ra 100 gia tri cua y cu 10 gia tri in ra 1 lan ra cua so
% file text duoi ten file Data.txt duoc chua trong cung thu muc voi
% chuong trinh
%======================================================================
fileID = fopen('Data.txt','w');
for k = 1:1000
if(mod(k,10)==0)
fprintf(fileID,'y[%d]=%6.2f ',k,y(k));
if(mod(k,50)==0)
fprintf(fileID,'n');
end
end
end
fclose(fileID);
% =========================================================================
% THIET LAP THAM SO CUA DO THI
% - neu he on dinh moi xuat cac tham so danh gia cac chi tieu on dinh len
% do thi
% - neu he khong on dinh thi se khong co cac chi tieu on dinh hien hi len
% do thi
%==========================================================================
if (a0<0)|(a1>0)|(abs(D)>A)|(a3<a4)
set(handles.tinhondinh,'string','he thong khong on dinh!')
set(handles.Tmax,'string','')
set(handles.Ymax,'string','')
set(handles.Overshoot,'string','')
set(handles.Yod,'string','')
set(handles.Tod,'string','')
plot(T*[1:100],y(1:100),'r','LineWidth',1.5)
grid
else
set(handles.tinhondinh,'string','he thong on dinh !')
%======================================================================
% Tin Ymax
%======================================================================
y_max = y(1);
for k = 1: 1000
if (y(k)>y_max)
y_max = y(k);
k_max = k;
21. http://quangcaobkvietnam.com BÀI TẬP DÀI MÔ HÌNH HÓA VÀ MÔ PHỎNG
Tác giả: facebook.com/mr.trongbk 20
end
end
y_max ;
Tmax = k_max*T ;
set(handles.Tmax,'string',num2str(Tmax))
set(handles.Ymax,'string',num2str(y_max))
%======================================================================
% Overshoot
%======================================================================
y_ss = (K1*K2)/(K1*K2);
overshoot = 100*(y_max-y_ss)/y_ss ;
set(handles.Overshoot,'string',num2str(overshoot))
set(handles.Yod,'string',num2str(y_ss))
%======================================================================
% thoi gian on dinh
%======================================================================
k=1000;
%set(handles.Tod,'string',num2str(Tod))
while (y(k)>0.98*y_ss)&(y(k)<1.02*y_ss)
k=k-1;
Tod =k*T;
end
set(handles.Tod,'string',num2str(Tod))
%======================================================================
% ve duong line
%======================================================================
% ve duong dong Ymax
hold all
x = [0 Tmax+0.2];
y=[y_max y_max];
plot(x,y,'--b')
text(0.005,y_max+0.04,'Ymax')
% ve duong dong Tmax
x = [Tmax Tmax];
y = [0 y_max];
plot(x,y,'--b')
text(Tmax+0.003,0.05,'Tmax')
% ve duong dong Yod
x = [0 1000*T];
y = [y_ss y_ss];
plot(x,y)
text(0.005,y_ss+0.05,'Yod')
% ve duong dong Tod
x = [Tod Tod];
y = [0 1.02*y_ss];
plot(x,y,'--b')
text(Tod+0.005,0.05,'Tod')
% ve overshoot
x =[Tmax+0.1 Tmax+0.1];
y = [y_ss y_max];
plot(x,y,'--g')
text(Tmax+0.12,(y_ss+y_max)/2,'overshoot')
axis([0 Tod+30*T 0 y_max+0.4])
% ve duong ong bao 0.92y_ss va 1.02y_ss
22. http://quangcaobkvietnam.com BÀI TẬP DÀI MÔ HÌNH HÓA VÀ MÔ PHỎNG
Tác giả: facebook.com/mr.trongbk 21
x = [0 1000*T];
y = 0.98*[y_ss y_ss];
plot(x,y,'--b')
x = [0 1000*T];
y = 1.02*[y_ss y_ss];
plot(x,y,'--b')
hold off
end
hold off
msgbox(' da in ra 100 gia tri theo yeu cau ra file text co ten Data.txt
','Thong bao')
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
close;
function editT_Callback(hObject, eventdata, handles)
% hObject handle to editT (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,'String') returns contents of editT as text
% str2double(get(hObject,'String')) returns contents of editT as a
double
% --- Executes during object creation, after setting all properties.
function editT_CreateFcn(hObject, eventdata, handles)
% hObject handle to editT (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'),
get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function editT1_Callback(hObject, eventdata, handles)
% hObject handle to editT1 (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,'String') returns contents of editT1 as text
% str2double(get(hObject,'String')) returns contents of editT1 as a
double
23. http://quangcaobkvietnam.com BÀI TẬP DÀI MÔ HÌNH HÓA VÀ MÔ PHỎNG
Tác giả: facebook.com/mr.trongbk 22
% --- Executes during object creation, after setting all properties.
function editT1_CreateFcn(hObject, eventdata, handles)
% hObject handle to editT1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'),
get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function editT2_Callback(hObject, eventdata, handles)
% hObject handle to editT2 (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,'String') returns contents of editT2 as text
% str2double(get(hObject,'String')) returns contents of editT2 as a
double
% --- Executes during object creation, after setting all properties.
function editT2_CreateFcn(hObject, eventdata, handles)
% hObject handle to editT2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'),
get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function editK1_Callback(hObject, eventdata, handles)
% hObject handle to editK1 (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,'String') returns contents of editK1 as text
% str2double(get(hObject,'String')) returns contents of editK1 as a
double
% --- Executes during object creation, after setting all properties.
function editK1_CreateFcn(hObject, eventdata, handles)
% hObject handle to editK1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
24. http://quangcaobkvietnam.com BÀI TẬP DÀI MÔ HÌNH HÓA VÀ MÔ PHỎNG
Tác giả: facebook.com/mr.trongbk 23
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'),
get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function editK2_Callback(hObject, eventdata, handles)
% hObject handle to editK2 (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,'String') returns contents of editK2 as text
% str2double(get(hObject,'String')) returns contents of editK2 as a
double
% --- Executes during object creation, after setting all properties.
function editK2_CreateFcn(hObject, eventdata, handles)
% hObject handle to editK2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'),
get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in In.
% --- Executes on button press in Hethong.
function Hethong_Callback(hObject, eventdata, handles)
% hObject handle to Hethong (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
Sodohethong
25. http://quangcaobkvietnam.com BÀI TẬP DÀI MÔ HÌNH HÓA VÀ MÔ PHỎNG
Tác giả: facebook.com/mr.trongbk 24
Mã nguồn chương trình Sodohethong.m
function varargout = Sodohethong(varargin)
% SODOHETHONG MATLAB code for Sodohethong.fig
% SODOHETHONG, by itself, creates a new SODOHETHONG or raises the
existing
% singleton*.
%
% H = SODOHETHONG returns the handle to a new SODOHETHONG or the handle
to
% the existing singleton*.
%
% SODOHETHONG('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in SODOHETHONG.M with the given input
arguments.
%
% SODOHETHONG('Property','Value',...) creates a new SODOHETHONG or
raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before Sodohethong_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to Sodohethong_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help Sodohethong
% Last Modified by GUIDE v2.5 17-Sep-2013 01:49:57
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @Sodohethong_OpeningFcn, ...
'gui_OutputFcn', @Sodohethong_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before Sodohethong is made visible.
function Sodohethong_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
26. http://quangcaobkvietnam.com BÀI TẬP DÀI MÔ HÌNH HÓA VÀ MÔ PHỎNG
Tác giả: facebook.com/mr.trongbk 25
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to Sodohethong (see VARARGIN)
% Choose default command line output for Sodohethong
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes Sodohethong wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = Sodohethong_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
Hinhnen=imread('Sodohethong.jpg');
axes(handles.Sodohethong);
image(Hinhnen);
% --- Executes on button press in Cancel.
function Cancel_Callback(hObject, eventdata, handles)
% hObject handle to Cancel (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
close;
Tài liệu tham khảo
1. Gs.Ts. Nguyễn Công Hiền , Ts. Nguyễn Phạm Thục Anh.Mô hình hóa hệ thống và
Mô phỏng . Nhà xuất bản Khoa học và Kỹ thuật, Hà Nội.
2. Võ Duy Thành . Chuyên đề thiết kế hệ thống điều khiển dùng MATLAB.