bao cao mhh va mp

26
 LP TRÌNH GUI MATLAB KHO SÁT ĐẶC TÍNH CA H THNG TỰ  ĐỘNG MR.TRONGBK BACH KHOA HA NOI BÀI TP DÀI  HÌNH HÓA   PHNG 

Upload: truong-van-trong

Post on 14-Apr-2018

228 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Bao cao MHH va MP

7/29/2019 Bao cao MHH va MP

http://slidepdf.com/reader/full/bao-cao-mhh-va-mp 1/26

 

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 

Page 2: Bao cao MHH va MP

7/29/2019 Bao cao MHH va MP

http://slidepdf.com/reader/full/bao-cao-mhh-va-mp 2/26

NHÓM 12 – ĐỀ 4 BÀI TẬP DÀI MÔ HÌNH HÓA VÀ MÔ PHỎNG 

GVHD : Thầ y. Võ Duy Thành 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 địnhcủ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 ị )

Page 3: Bao cao MHH va MP

7/29/2019 Bao cao MHH va MP

http://slidepdf.com/reader/full/bao-cao-mhh-va-mp 3/26

NHÓM 12 – ĐỀ 4 BÀI TẬP DÀI MÔ HÌNH HÓA VÀ MÔ PHỎNG 

GVHD : Thầ y. Võ Duy Thành 2

CHƯƠNG 1. MÔ HÌNH HỆ THỐNG

1.1.  Hàm tr uy ền h ệ kín tr ong 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 21 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

1lim (t) lim (s) lim (s).W(s) lim (s). (0) 1t s s s

 y y sY sG sG G s

 

1.2.  Hàm tr uy ề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 

 

1

W

-- 

e

Page 4: Bao cao MHH va MP

7/29/2019 Bao cao MHH va MP

http://slidepdf.com/reader/full/bao-cao-mhh-va-mp 4/26

NHÓM 12 – ĐỀ 4 BÀI TẬP DÀI MÔ HÌNH HÓA VÀ MÔ PHỎNG 

GVHD : Thầ y. Võ Duy Thành 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

 K K T Z Z Z Y 

 A Z B Z C Z D

 A Z Y B Z Y C Z Y D Y 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 . . A Y 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]

B Y C Y D Y  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 D F 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

Page 5: Bao cao MHH va MP

7/29/2019 Bao cao MHH va MP

http://slidepdf.com/reader/full/bao-cao-mhh-va-mp 5/26

NHÓM 12 – ĐỀ 4 BÀI TẬP DÀI MÔ HÌNH HÓA VÀ MÔ PHỎNG 

GVHD : Thầ y. Võ Duy Thành 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 K 1 , K 2 

, 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

Page 6: Bao cao MHH va MP

7/29/2019 Bao cao MHH va MP

http://slidepdf.com/reader/full/bao-cao-mhh-va-mp 6/26

NHÓM 12 – ĐỀ 4 BÀI TẬP DÀI MÔ HÌNH HÓA VÀ MÔ PHỎNG 

GVHD : Thầ y. Võ Duy Thành 5

 Hình 2.2. sơ đồ cấ u trúc hệ thố ng 

Page 7: Bao cao MHH va MP

7/29/2019 Bao cao MHH va MP

http://slidepdf.com/reader/full/bao-cao-mhh-va-mp 7/26

NHÓM 12 – ĐỀ 4 BÀI TẬP DÀI MÔ HÌNH HÓA VÀ MÔ PHỎNG 

GVHD : Thầ y. Võ Duy Thành 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

Page 8: Bao cao MHH va MP

7/29/2019 Bao cao MHH va MP

http://slidepdf.com/reader/full/bao-cao-mhh-va-mp 8/26

NHÓM 12 – ĐỀ 4 BÀI TẬP DÀI MÔ HÌNH HÓA VÀ MÔ PHỎNG 

GVHD : Thầ y. Võ Duy Thành 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]+8K 1K 2T3 ) /A

K++ 

End 

YES

 NO 

K =1000

Page 9: Bao cao MHH va MP

7/29/2019 Bao cao MHH va MP

http://slidepdf.com/reader/full/bao-cao-mhh-va-mp 9/26

NHÓM 12 – ĐỀ 4 BÀI TẬP DÀI MÔ HÌNH HÓA VÀ MÔ PHỎNG 

GVHD : Thầ y. Võ Duy Thành 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]=%f\n',k,y(k)) 

end end 

Begin 

K = 1 

K++

End 

YES 

 NO 

K =1000 

In ra y[k]

 NO Mod(k,10) = 0 

YES 

Page 10: Bao cao MHH va MP

7/29/2019 Bao cao MHH va MP

http://slidepdf.com/reader/full/bao-cao-mhh-va-mp 10/26

NHÓM 12 – ĐỀ 4 BÀI TẬP DÀI MÔ HÌNH HÓA VÀ MÔ PHỎNG 

GVHD : Thầ y. Võ Duy Thành 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] 

Page 11: Bao cao MHH va MP

7/29/2019 Bao cao MHH va MP

http://slidepdf.com/reader/full/bao-cao-mhh-va-mp 11/26

NHÓM 12 – ĐỀ 4 BÀI TẬP DÀI MÔ HÌNH HÓA VÀ MÔ PHỎNG 

GVHD : Thầ y. Võ Duy Thành 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 

Page 12: Bao cao MHH va MP

7/29/2019 Bao cao MHH va MP

http://slidepdf.com/reader/full/bao-cao-mhh-va-mp 12/26

NHÓM 12 – ĐỀ 4 BÀI TẬP DÀI MÔ HÌNH HÓA VÀ MÔ PHỎNG 

GVHD : Thầ y. Võ Duy Thành 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 

 

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 K 1 =50, K 2 = 0.5, T 1 = 0.02 , T 2 = 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 đã inra 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 

Page 13: Bao cao MHH va MP

7/29/2019 Bao cao MHH va MP

http://slidepdf.com/reader/full/bao-cao-mhh-va-mp 13/26

NHÓM 12 – ĐỀ 4 BÀI TẬP DÀI MÔ HÌNH HÓA VÀ MÔ PHỎNG 

GVHD : Thầ y. Võ Duy Thành 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

Page 14: Bao cao MHH va MP

7/29/2019 Bao cao MHH va MP

http://slidepdf.com/reader/full/bao-cao-mhh-va-mp 14/26

NHÓM 12 – ĐỀ 4 BÀI TẬP DÀI MÔ HÌNH HÓA VÀ MÔ PHỎNG 

GVHD : Thầ y. Võ Duy Thành 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 K 1 =50, K 2 = 0.5, T 1 = 0.02 , T 2 = 0.03 trên miề nthờ 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)

      A     m     p      l      i      t     u      d     e

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.80

0.2

0.4

0.6

0.8

1

1.2

1.4

System: GPeak amplitude: 1.33

Overshoot (%): 33

At time (seconds): 0.15

System: G

Settling Time (seconds): 0.465

System: G

Final Value: 1

Page 15: Bao cao MHH va MP

7/29/2019 Bao cao MHH va MP

http://slidepdf.com/reader/full/bao-cao-mhh-va-mp 15/26

NHÓM 12 – ĐỀ 4 BÀI TẬP DÀI MÔ HÌNH HÓA VÀ MÔ PHỎNG 

GVHD : Thầ y. Võ Duy Thành 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 raisesthe existing % singleton*. % % H = GUI_MHH_MP_BAI1 returns the handle to a new GUI_MHH_MP_BAI1 or thehandle to % the existing singleton*. % % GUI_MHH_MP_BAI1('CALLBACK',hObject,eventData,handles,...) calls thelocal % function named CALLBACK in GUI_MHH_MP_BAI1.M with the given inputarguments. % 

% GUI_MHH_MP_BAI1('Property','Value',...) creates a new GUI_MHH_MP_BAI1or 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 viavarargin. % % *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 

Page 16: Bao cao MHH va MP

7/29/2019 Bao cao MHH va MP

http://slidepdf.com/reader/full/bao-cao-mhh-va-mp 16/26

NHÓM 12 – ĐỀ 4 BÀI TẬP DÀI MÔ HÌNH HÓA VÀ MÔ PHỎNG 

GVHD : Thầ y. Võ Duy Thành 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 editOvershootas 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'); 

Page 17: Bao cao MHH va MP

7/29/2019 Bao cao MHH va MP

http://slidepdf.com/reader/full/bao-cao-mhh-va-mp 17/26

NHÓM 12 – ĐỀ 4 BÀI TẬP DÀI MÔ HÌNH HÓA VÀ MÔ PHỎNG 

GVHD : Thầ y. Võ Duy Thành 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 adouble 

% --- 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 adouble 

% --- 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 

Page 18: Bao cao MHH va MP

7/29/2019 Bao cao MHH va MP

http://slidepdf.com/reader/full/bao-cao-mhh-va-mp 18/26

NHÓM 12 – ĐỀ 4 BÀI TẬP DÀI MÔ HÌNH HÓA VÀ MÔ PHỎNG 

GVHD : Thầ y. Võ Duy Thành 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 adouble 

% --- 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 adouble 

% --- 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 

Page 19: Bao cao MHH va MP

7/29/2019 Bao cao MHH va MP

http://slidepdf.com/reader/full/bao-cao-mhh-va-mp 19/26

NHÓM 12 – ĐỀ 4 BÀI TẬP DÀI MÔ HÌNH HÓA VÀ MÔ PHỎNG 

GVHD : Thầ y. Võ Duy Thành 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 + Da = 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%====================================================================== 

Page 20: Bao cao MHH va MP

7/29/2019 Bao cao MHH va MP

http://slidepdf.com/reader/full/bao-cao-mhh-va-mp 20/26

NHÓM 12 – ĐỀ 4 BÀI TẬP DÀI MÔ HÌNH HÓA VÀ MÔ PHỎNG 

GVHD : Thầ y. Võ Duy Thành 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; 

Page 21: Bao cao MHH va MP

7/29/2019 Bao cao MHH va MP

http://slidepdf.com/reader/full/bao-cao-mhh-va-mp 21/26

NHÓM 12 – ĐỀ 4 BÀI TẬP DÀI MÔ HÌNH HÓA VÀ MÔ PHỎNG 

GVHD : Thầ y. Võ Duy Thành 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 overshootx =[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

Page 22: Bao cao MHH va MP

7/29/2019 Bao cao MHH va MP

http://slidepdf.com/reader/full/bao-cao-mhh-va-mp 22/26

NHÓM 12 – ĐỀ 4 BÀI TẬP DÀI MÔ HÌNH HÓA VÀ MÔ PHỎNG 

GVHD : Thầ y. Võ Duy Thành 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 adouble 

% --- 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 adouble 

Page 23: Bao cao MHH va MP

7/29/2019 Bao cao MHH va MP

http://slidepdf.com/reader/full/bao-cao-mhh-va-mp 23/26

NHÓM 12 – ĐỀ 4 BÀI TẬP DÀI MÔ HÌNH HÓA VÀ MÔ PHỎNG 

GVHD : Thầ y. Võ Duy Thành 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 adouble 

% --- 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 adouble 

% --- 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 

Page 24: Bao cao MHH va MP

7/29/2019 Bao cao MHH va MP

http://slidepdf.com/reader/full/bao-cao-mhh-va-mp 24/26

NHÓM 12 – ĐỀ 4 BÀI TẬP DÀI MÔ HÌNH HÓA VÀ MÔ PHỎNG 

GVHD : Thầ y. Võ Duy Thành 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 adouble 

% --- 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 

Page 25: Bao cao MHH va MP

7/29/2019 Bao cao MHH va MP

http://slidepdf.com/reader/full/bao-cao-mhh-va-mp 25/26

NHÓM 12 – ĐỀ 4 BÀI TẬP DÀI MÔ HÌNH HÓA VÀ MÔ PHỎNG 

GVHD : Thầ y. Võ Duy Thành 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 theexisting 

% singleton*. % % H = SODOHETHONG returns the handle to a new SODOHETHONG or the handleto % the existing singleton*. % % SODOHETHONG('CALLBACK',hObject,eventData,handles,...) calls the local % function named CALLBACK in SODOHETHONG.M with the given inputarguments. % % SODOHETHONG('Property','Value',...) creates a new SODOHETHONG orraises 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. 

Page 26: Bao cao MHH va MP

7/29/2019 Bao cao MHH va MP

http://slidepdf.com/reader/full/bao-cao-mhh-va-mp 26/26

NHÓM 12 – ĐỀ 4 BÀI TẬP DÀI MÔ HÌNH HÓA VÀ MÔ PHỎNG 

GVHD : Thầy Võ Duy Thành 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.