Tải bản đầy đủ (.pdf) (31 trang)

Bai 4 thuc hanh thiet ke mo hinh hoa mo phong xe bus phuc vu

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (1019.69 KB, 31 trang )

Bài 4. XÂY DỰNG MÔ HÌNH HÓA MÔ PHỎNG XE BUS PHỤC VỤ NHÂN DÂN
TRÊN CÔNG CỤ GUIDE CỦA PHẦN MỀM MATLAB
I. Mục đích
- Giúp sinh viên hiểu phương pháp xây dựng mô hình hóa mô phỏng một hệ thống vật lý.
- Thu thập và phân tích các bảng dữ liệu của hệ thống.
- Đánh giá được kết quả mô phỏng, so sánh độ tương đồng giữa kết quả mô phỏng của mô hình với các tính chất của đối
tượng thực.
- Đưa ra kết luận và các phương án điều chỉnh cho đối tượng thực dựa trên kết quả mô phỏng sau khi phân tích chúng.
II. Yêu cầu thực hành
- Máy vi tính cài đặt sẵn phần mềm Matlab với đầy đủ các ứng dụng của Math Work cung cấp.
- Yêu cầu sinh viên được trang bị phần lý thuyết môn Tin học chuyên ngành, hết chương 4 Mô phỏng hệ ngẫu nhiên
môn Mô hình hóa.
- Tuân thủ mọi nội quy, quy định khi thực hành tại phòng thí nghiệm.
III. Nội dung thực hành
3.1 Bài toán yêu cầu
Mô phỏng trạm xe bus sinh viên. Sinh viên đi từ ký túc xá đến trường bằng xe bus, mỗi xe chứa được 60 sinh viên.
Thời gian đi đến trường bắt đầu từ 6h đến 7h. Sinh viên đi đến trạm xe bus được mô tả bằng một dòng tối giản với
cường độ λ = 0.8 SV/s. Cứ sau Txe = 15 phút có một chuyến xe bus đi đến trường. Nếu số sinh viên chờ xe <60 SV xe
chạy đúng giờ. Nếu số SV chờ xe >60SV thì số SV thừa sẽ phải chờ đến chuyến xe sau.
Xây dựng mô hình mô phỏng hệ thống trên. Kiểm tra xem sau 7h30 thì còn bao nhiêu SV bị kẹt xe tại bến xe bus.
Để đảm bảo tất cả sinh viên đi học đúng giờ thì Txe = ??
3.2. Các bước tiến hành


Bước 1. Thuật toán mô phỏng:
B1. Xây dựng mô hình dòng sinh viên đi đến trạm xe bus. Khoảng thời gian giữa các sinh viên đi đến trạm xe bus
là ti = (-1/λ)ln(Ui) với Ui ~ U(0,1).
B2. Thời gian mô phỏng là từ 6h đến 7h tức t = 0 đên t = 3600s.
t = t + ti khi t = 3600s thì dừng mô phỏng.
B3. Nếu t <3600s thì số sinh viên ở trạm c = c +1 khi có 1 sinh viên đến trạm.
B4. Khi t = Tx, t = 2Tx, t = 3Tx… (tức t/Tx không có dư). Kiểm tra số sinh viên ở trạm là c.


- Nếu c<60 (Tức 1 xe 60 chỗ có thể cho hết số sinh viên trong trạm lên) khi đó sau khi xe đi thì không có sinh
viên còn phải chờ ở chạm tức c = 0.
- Nếu c>60 khi đó số sinh viên còn chờ ở bến xe bus là c = c - 60.
B5. Khi t = 3600s thì đếm c = ???. Khi đó ta thay đổi Tx để c = 0.


Bước 2. Xây dựng giao diện mô phỏng hệ thống điều khiển tự động
(1) Khởi tạo guide trong phần mềm Matlab


(2) Thiết lập bảng điều khiển trên giao diện
- Chọn công cụ Panel


- Thay đổi tên cho Panel bằng cách kích đúp vào Panel và thay tên trong phần Title của nó từ tên
- Panel sang tên “Bang dieu
khien”

-


- Chọn công cụ Push Button để khai báo nút khảo sát hệ thống


Đổi tên PushButton trong String ở Inspector của nút sang tên mới là khảo sát hệ thống


+ Kết quả thu được



+ Tương tự như vậy ta lấy nút có tên gọi là “Thoát khỏi hệ thống”


- Chọn công cụ Panel để khai báo bảng nhập giá trị


- Chọn công cụ Static Text để định nghĩa các ô nhập giá trị từ bàn phím


+ Kích đúp vào Static Text để thay đổi tên Static Text trong String sang tên “Nhap so ghe ngoi cua xe N =”

+Tương tự như vậy ta khai báo nhập giá trị cho Txe, lamda, T.
- Chọn công cụ Edit Text để khai báo ô nhập và xuất giá trị trên màn hình
+ Kích đúp vào Edit1 để khai báo giá trị mặc định cho hệ số N = 60 trong phần String từ chuỗi ký tự “ Edit Text” sang
số 60. Làm tương tự chọn Edit2, Edit3,Edit4 cho các biến TxTe, lamda, với mặc định lamda = 0.8; và T = 3600. Txe =
900



+ Tương tự ta lập bảng xuất giá trị tính toán ra:
> Số sinh viên đợi ở bến trong thời gian khảo sát xuất ra Edit5 > Số sinh viên đến trường đúng giờ xuất ra Edit6 >
> Số sinh viên đến trường muộn giờ xuất ra Edit7



Bước 3 Viết Code cho các nút điều khiển
(1) Nút thoát khỏi hệ thống
Nhấn chuột phải vào Nút “ Thoat khoi he thong” chọn View callBacks/Callback



+ Ta lập trình như sau:
hoi=questdlg('Ban muon thoat khoi chuong trinh?',...
'THUC SU MUON THOAT?','Yes','No','No');
if strcmp(hoi,'Yes')
close
if strcmp(hoi,'No')
return;
end
end



(2) Nút “Khao sat he thong”
Nhấn chuột phải vào Nút “Khao sat he thong” chọn View callBacks/Callback


+ Ta lập trình:
function varargout = Bai4(varargin)
% BAI4 MATLAB code for Bai4.fig
%
BAI4, by itself, creates a new BAI4 or raises the existing
%
singleton*.
%
%
H = BAI4 returns the handle to a new BAI4 or the handle to
%
the existing singleton*.
%
%

BAI4('CALLBACK',hObject,eventData,handles,...) calls the local
%
function named CALLBACK in BAI4.M with the given input arguments.
%
%
BAI4('Property','Value',...) creates a new BAI4 or raises the
%
existing singleton*. Starting from the left, property value pairs are
%
applied to the GUI before Bai4_OpeningFcn gets called. An
%
unrecognized property name or invalid value makes property application
%
stop. All inputs are passed to Bai4_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 Bai4
% Last Modified by GUIDE v2.5 06-Mar-2013 19:38:03
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',
mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @Bai4_OpeningFcn, ...
'gui_OutputFcn', @Bai4_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 Bai4 is made visible.
function Bai4_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 Bai4 (see VARARGIN)
% Choose default command line output for Bai4
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes Bai4 wait for user response (see UIRESUME)

% uiwait(handles.figure1);

% --- Outputs from this function are returned to the command line.
function varargout = Bai4_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;

% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject
handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles
structure with handles and user data (see GUIDATA)
% Ch??ng trình ví du 3.1 v? v? ?? th? c?a h? th?ng ?i?u khi?n t? ??ng
disp('MO PHONG HE THONG XE BUS PHUC VU - THM')
clc;
% Khai bao bien
syms N lamda T Txe Ts Ndg Nm
N = get(handles.edit1,'String');% Nhan chuoi ky tu trong edit1 gán cho N


N = str2num(N);


%Chuyen gia tri chuoi ký tu sang dang so

Txe = get(handles.edit2,'String');% Nh?n chu?i ký t? ? edit3 gán cho t1
Txe = str2num(Txe);
%Chuy?n ??i t? chu?i ký t? sang d?ng s?
lamda = get(handles.edit3,'String');% Nh?n chu?i ký t? ? edit2 gán cho k2
lamda = str2num(lamda);
%Chuy?n ??i t? chu?i ký t? sang d?ng s?
T = get(handles.edit4,'String');% Nh?n chu?i ký t? ? edit3 gán cho t1
T = str2num(T);
%Chuy?n ??i t? chu?i ký t? sang d?ng s?

sochuyen=floor(T/Txe)
set(handles.edit8,'String',sochuyen);
songuoiduocvanchuyenlt= sochuyen*N;
e=2.7182;
Ts = 0;
t=0;
ket=0;
Ndg=0;
for i= 1:1:T
U = rand(1);
ti = - log(U)/(lamda*log(e));
t = t+ti;
if tTs = Ts+1
else
end
end
set(handles.edit5,'String',Ts);


if Ts>songuoiduocvanchuyenlt
ket= Ts-songuoiduocvanchuyenlt
Ndg=songuoiduocvanchuyenlt
if Ts<=songuoiduocvanchuyenlt
ket=0;
Ndg=Ts;
end
end
set(handles.edit7,'String',ket);


set(handles.edit6,'String',Ndg);

% --- 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)
hoi=questdlg('Ban muon thoat khoi chuong trinh?',...
'THUC SU MUON THOAT?','Yes','No','No');
if strcmp(hoi,'Yes')
close
if strcmp(hoi,'No')
return;
end
end


function edit6_Callback(hObject, eventdata, handles)
% hObject
handle to edit6 (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 edit6 as text
%
str2double(get(hObject,'String')) returns contents of edit6 as a double

% --- Executes during object creation, after setting all properties.
function edit6_CreateFcn(hObject, eventdata, handles)
% hObject
handle to edit6 (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 edit7_Callback(hObject, eventdata, handles)


% hObject
% eventdata
% handles


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

% Hints: get(hObject,'String') returns contents of edit6 as text
%
str2double(get(hObject,'String')) returns contents of edit6 as a double

% --- Executes during object creation, after setting all properties.
function edit7_CreateFcn(hObject, eventdata, handles)
% hObject
handle to edit6 (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 edit8_Callback(hObject, eventdata, handles)
% hObject
handle to edit8 (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 edit8 as text

%
str2double(get(hObject,'String')) returns contents of edit8 as a double

% --- Executes during object creation, after setting all properties.
function edit8_CreateFcn(hObject, eventdata, handles)
% hObject
handle to edit8 (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 edit1_Callback(hObject, eventdata, handles)
% hObject
handle to edit1 (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 edit1 as text
%
str2double(get(hObject,'String')) returns contents of edit1 as a double

% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)

% hObject
handle to edit1 (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 edit2_Callback(hObject, eventdata, handles)
% hObject
handle to edit2 (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 edit2 as text
%
str2double(get(hObject,'String')) returns contents of edit2 as a double

% --- Executes during object creation, after setting all properties.
function edit2_CreateFcn(hObject, eventdata, handles)
% hObject
handle to edit2 (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


×