báo cáo matlap

25 9 0
báo cáo matlap

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

MATLAB là phần mềm cung cấp môi trường tính toán số và lập trình, do công ty MathWorks thiết kế. MATLAB cho phép tính toán số với ma trận, vẽ đồ thị hàm số hay biểu đồ thông tin, thực hiện thuật toán, tạo các giao diện người dùng và liên kết với những chương trình máy tính viết trên nhiều ngôn ngữ lập trình khác.

HỌC VIỆN CƠNG NGHỆ BƯU CHÍNH VIỄN THƠNG CƠ SỞ TẠI THÀNH PHỚ HỜ CHÍ MINH KHOA KỸ TḤT ĐIỆN TỬ BÁO CÁO BÀI TẬP LỚN MÔN: Matlab Và Ứng Dụng Sinh viên thực hiện: Phan Hoàng Anh Tuấn N16DCDT120 D16CQDT02-N Nguyễn Anh Thảo N16DCDT056 D16CQDT02-N Đồn Cơng Danh N17DCDT014 D17CQDT01-N Giáo viên hướng dẫn: ThS Trần Quang Thuận - - Câu 7: Viết đoạn chương trình (script) đọc giá trị sai số điện trở ba vòng màu Code: while true vong1=input('mau vong so 1: ','s'); flag1=1; vong1=lower(vong1); switch vong1 case 'den' a=0; a1='0'; case 'nau' a=1; a1='1'; case 'do' a=2; a1='2'; case 'cam' a=3; a1='3'; case 'vang' a=4; a1='4'; case 'luc' a=5; a1='5'; case 'lam' a=6; a1='6'; case 'tim' a=7; a1='7'; case 'xam' a=8; a1='8'; case 'trang' a=9; a1='9'; otherwise disp('mau nhap vao hien khong có bang mau Xin vui long nhap lai:'); flag1=0; end if flag1~=0; break; end end while true vong2=input('mau vong so 2: ','s'); flag2=1; vong2=lower(vong2); switch vong2 case 'den' b=0; b1='0'; case 'nau' b=1; b1='1'; case 'do' b=2; b1='2'; case 'cam' b=3; b1='3'; case 'vang' b=4; b1='4'; case 'luc' b=5; b1='5'; case 'lam' b=6; b1='6'; case 'tim' b=7; b1='7'; case 'xam' b=8; b1='8'; case 'trang' b=9; b1='9'; otherwise disp('mau nhap vao hien khong có bang mau Xin vui long nhap lai:') flag2=0; end if flag2~=0 break; end end while true vong3 =input('mau vong so 3: ', 's'); vong3=lower(vong3); flag3=1; switch vong3 case 'den' c=10^0; c1='10^0'; case 'nau' c=10^1; c1='*10^1'; case 'do' c=10^2; c1='*10^2'; case 'cam' c=10^3; c1='*10^3'; case 'vang' c=10^4; c1='*10^4'; case 'luc' c=10^5; c1='*10^5'; case 'lam' c=10^6; c1='*10^6'; case 'tim' c=10^7; c1='*10^7'; case 'xam' c=10^8; c1='*10^7'; case 'trang' c=10^9; c1='*10^0'; otherwise disp('mau nhap vao hien khong có bang mau Xin vui long nhap lai:'); flag3=0; end if flag3~=0 break; end end if flag1*flag2*flag3==1 r=(a*10+b)*c; saiso=r*0.2; saiso=num2str(saiso); r=num2str(r); ketqua=[a1,b1,c1,'=',r,'±',saiso,'(Ohm)']; disp(ketqua); Kết quả: • Điện trở 1: vịng màu đỏ-đỏ-nâu có giá trị 22044 () • Điện trở 2: vịng màu nâu-đen-cam có giá trị 100002000 () • Điện trở 3: vịng màu vàng-vàng-đen có giá trị 448.8() Nhận xét: • Chương trình hiển thị kết rõ ràng cách tính sai số • Chương trình hoạt động tốt nhanh chóng Lưu ý: • Vì điện trở vịng màu nên sai số ln lớn (20%) khơng có vịng màu biểu thị cho sai số Câu 22: Viết chương trình tạo giao diện người dùng (GUI) giải phương trình tập số thực số phức với hệ số a, b, c người dùng nhập Code: function pbtgiai_Callback(hObject, eventdata, handles) % hObject handle to pbtgiai (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) hs(1) = str2num(get(handles.edta,'string')); hs(2) = str2num(get(handles.edtb,'string')); hs(3) = str2num(get(handles.edtc,'string')); hs(4) = str2num(get(handles.edtd,'string')); hs(5) = str2num(get(handles.edte,'string')); hs(6) = str2num(get(handles.edtf,'string')); hs(7) = str2num(get(handles.edtg,'string')); X = roots(hs); set(handles.edtx1,'string',num2str(X(1))); set(handles.edtx2,'string',num2str(X(2))); set(handles.edtx3,'string',num2str(X(3))); set(handles.edtx4,'string',num2str(X(4))); set(handles.edtx5,'string',num2str(X(5))); set(handles.edtx6,'string',num2str(X(6))); Kết quả: Ta có giao diện người dùng (GUI) giải phương trình nhập giá trị a, b, c, d, e, f, g tương ứng theo bậc Sau đó, nhấn “Giải phương trình” ta nhận được nghiệm tương ứng X1, X2, X3, X4, X5, X6 Với phương trình ta nhận nghiệm riêng biệt có: nghiệm thực ; nghiệm phức , , , Với phương trình ta nhận nghiệm riêng biệt có : thực nghiệm nghiệm phức , , , Nhận xét: • Chương trình hiển thị kết rõ ràng cho nghiệm phương trình • Giao diện đơn giản dễ sử dụng với người dùng • Chương trình hoạt động tốt nhanh chóng Lưu ý: • Các nghiệm bậc 5, 4, 2, ứng với b, c, e, f luôn nhập số Câu 35: Dùng Simulink mô phương trình sau: Quan sát f(t) scope nhận xét dạng sóng Code: Ta lấy khối: Khối Clock để cung cấp thời gian t cho phương trình Khối Fcn để nhập phương trình Khối Scope để thị kết Chú ý: có sử dụng thêm khối dashboard scope ( không sử dụng) để thị trực tiếp cửa sổ hình Kết quả: Ta cài đặt hai đường đường màu xanh chưa qua khối Fcn đường màu đỏ qua khối Fcn Ta nhận thấy đường màu xanh biểu thời gian tăng dần từ đến 10, đường màu đỏ giao động biên độ từ -100 đến 100 Lưu ý: ta thay đổi biên độ góc pha phương trình cho đồ thị thay đổi Câu làm thêm: Viết chương trình tạo giao diện người dùng (GUI) thực chức máy tính điện tử đơn giản với phép tốn cộng, trừ, nhân, chia, nghịch đảo, bậc 2, sin, cos, tập số thực Code: function varargout = MayTinhBoTui(varargin) % MAYTINHBOTUI MATLAB code for MayTinhBoTui.fig % MAYTINHBOTUI, by itself, creates a new MAYTINHBOTUI or raises the existing % singleton* % % H = MAYTINHBOTUI returns the handle to a new MAYTINHBOTUI or the handle to % the existing singleton* % % MAYTINHBOTUI('CALLBACK',hObject,eventData,handles, ) calls the local % function named CALLBACK in MAYTINHBOTUI.M with the given input arguments % % MAYTINHBOTUI('Property','Value', ) creates a new MAYTINHBOTUI or raises the % existing singleton* Starting from the left, property value pairs are % applied to the GUI before MayTinhBoTui_OpeningFcn gets called An % unrecognized property name or invalid value makes property application % stop All inputs are passed to MayTinhBoTui_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 MayTinhBoTui % Last Modified by GUIDE v2.5 06-Oct-2020 13:21:49 % Begin initialization code - DO NOT EDIT gui_Singleton = 1; gui_State = struct('gui_Name', mfilename, 'gui_Singleton', gui_Singleton, 'gui_OpeningFcn', @MayTinhBoTui_OpeningFcn, 'gui_OutputFcn', @MayTinhBoTui_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 MayTinhBoTui is made visible function MayTinhBoTui_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 MayTinhBoTui (see VARARGIN) % Choose default command line output for MayTinhBoTui handles.output = hObject; % Update handles structure guidata(hObject, handles); % UIWAIT makes MayTinhBoTui wait for user response (see UIRESUME) % uiwait(handles.figure1); global jj Shift set(handles.kq,'string','0.'); jj=0; shift=0; % - Outputs from this function are returned to the command line function varargout = MayTinhBoTui_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 GUIDATA) structure with handles and user data (see % Get default command line output from handles structure varargout{1} = handles.output; % - Executes on button press in pbtc function pbtc_Callback(hObject, eventdata, handles) % hObject handle to pbtc (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) set(handles.kq,'string','0.') % - Executes on button press in pbtcongtru function pbtcongtru_Callback(hObject, eventdata, handles) % hObject handle to pbtcongtru (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) tmp = get(handles.kq,'string'); if(strcmp(tmp,'0.')==1) set(handles.tmp,'string','0.'); else a = strread(tmp,'%f'); a = 0-a; set(handles.kq,'string',a) end % - Executes on button press in pbt1 function pbt1_Callback(hObject, eventdata, handles) % hObject handle to pbt1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) global jj tmp = get(handles.kq,'string'); if(strcmp(tmp,'0.')==1)&&(jj==0) set(handles.kq,'string','0.'); else ss=char(tmp); l=length(tmp); tmp=ss(1:l-1); set(handles.kq,'string',tmp) end jj=0; % - Executes on button press in pbt2 function pbt2_Callback(hObject, eventdata, handles) % hObject handle to pbt2 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) tmp = get(handles.kq,'string'); if(strcmp(tmp,'0.')==1) set(handles.kq,'string','('); else tmp = strcat(tmp,'('); set(handles.kq,'string',tmp) end % - Executes on button press in pbt3 function pbt3_Callback(hObject, eventdata, handles) % hObject handle to pbt3 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) tmp = get(handles.kq,'string'); if(strcmp(tmp,'0.')==1) set(handles.kq,'string',')'); else tmp = strcat(tmp,')'); set(handles.kq,'string',tmp) end % - Executes on button press in pbtso7 function pbtso7_Callback(hObject, eventdata, handles) % hObject handle to pbtso7 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) global jj tmp = get(handles.kq,'string'); if(strcmp(tmp,'0.')==1)&&(jj==0) set(handles.kq,'string','7'); else tmp = strcat(tmp,'7'); set(handles.kq,'string',tmp) end jj=0; % - Executes on button press in pbtso8 function pbtso8_Callback(hObject, eventdata, handles) % hObject handle to pbtso8 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) global jj tmp = get(handles.kq,'string'); if(strcmp(tmp,'0.')==1)&&(jj==0) set(handles.kq,'string','8'); else tmp = strcat(tmp,'8'); set(handles.kq,'string',tmp) end jj=0; % - Executes on button press in pbtso9 function pbtso9_Callback(hObject, eventdata, handles) % hObject handle to pbtso9 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) global jj tmp = get(handles.kq,'string'); if(strcmp(tmp,'0.')==1)&&(jj==0) set(handles.kq,'string','9'); else tmp = strcat(tmp,'9'); set(handles.kq,'string',tmp) end jj=0; % - Executes on button press in pbtchia function pbtchia_Callback(hObject, eventdata, handles) % hObject handle to pbtchia (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) tmp = get(handles.kq,'string'); tmp = strcat(tmp,'/'); set(handles.kq,'string',tmp) % - Executes on button press in pbtcan function pbtcan_Callback(hObject, eventdata, handles) % hObject handle to pbtcan (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) tmp = get(handles.kq,'string'); if(strcmp(tmp,'0.')==1) set(handles.kq,'string','sqrt'); else tmp = strcat(tmp,'sqrt'); set(handles.kq,'string',tmp) end % - Executes on button press in pbtso4 function pbtso4_Callback(hObject, eventdata, handles) % hObject handle to pbtso4 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) global jj tmp = get(handles.kq,'string'); if(strcmp(tmp,'0.')==1)&&(jj==0) set(handles.kq,'string','4'); else tmp = strcat(tmp,'4'); set(handles.kq,'string',tmp) end jj=0; % - Executes on button press in pbtso5 function pbtso5_Callback(hObject, eventdata, handles) % hObject handle to pbtso5 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) global jj tmp = get(handles.kq,'string'); if(strcmp(tmp,'0.')==1)&&(jj==0) set(handles.kq,'string','5'); else tmp = strcat(tmp,'5'); set(handles.kq,'string',tmp) end jj=0; % - Executes on button press in pbtso6 function pbtso6_Callback(hObject, eventdata, handles) % hObject handle to pbtso6 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) global jj tmp = get(handles.kq,'string'); if(strcmp(tmp,'0.')==1)&&(jj==0) set(handles.kq,'string','6'); else tmp = strcat(tmp,'6'); set(handles.kq,'string',tmp) end jj=0; % - Executes on button press in pbtnhan function pbtnhan_Callback(hObject, eventdata, handles) % hObject handle to pbtnhan (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) tmp = get(handles.kq,'string'); tmp = strcat(tmp,'*'); set(handles.kq,'string',tmp) % - Executes on button press in pbtsin function pbtsin_Callback(hObject, eventdata, handles) % hObject handle to pbtsin (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) global shift tmp = get(handles.kq,'string'); switch shift case if(strcmp(tmp,'0.')==1) set(handles.kq,'string','sin'); else tmp = strcat(tmp,'sin'); set(handles.kq,'string',tmp) end case if(strcmp(tmp,'0.')==1) set(handles.kq,'string','asin') else tmp = strcat(tmp,'asin'); set(handles.kq,'string',tmp) end end % - Executes on button press in pbtso1 function pbtso1_Callback(hObject, eventdata, handles) % hObject handle to pbtso1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) global jj tmp = get(handles.kq,'string'); if(strcmp(tmp,'0.')==1)&&(jj==0) set(handles.kq,'string','1'); else tmp = strcat(tmp,'1'); set(handles.kq,'string',tmp) end jj=0; % - Executes on button press in pbtso2 function pbtso2_Callback(hObject, eventdata, handles) % hObject handle to pbtso2 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) global jj tmp = get(handles.kq,'string'); if(strcmp(tmp,'0.')==1)&&(jj==0) set(handles.kq,'string','2'); else tmp = strcat(tmp,'2'); set(handles.kq,'string',tmp) end jj=0; % - Executes on button press in pbtso3 function pbtso3_Callback(hObject, eventdata, handles) % hObject handle to pbtso3 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) global jj tmp = get(handles.kq,'string'); if(strcmp(tmp,'0.')==1)&&(jj==0) set(handles.kq,'string','3'); else tmp = strcat(tmp,'3'); set(handles.kq,'string',tmp) end jj=0; % - Executes on button press in pbttru function pbttru_Callback(hObject, eventdata, handles) % hObject handle to pbttru (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) tmp = get(handles.kq,'string'); tmp = strcat(tmp,'-'); set(handles.kq,'string',tmp) % - Executes on button press in pbtcos function pbtcos_Callback(hObject, eventdata, handles) % hObject handle to pbtcos (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) tmp = get(handles.kq,'string'); if(strcmp(tmp,'0.')==1) set(handles.kq,'string','cos'); else tmp = strcat(tmp,'cos'); set(handles.kq,'string',tmp) end % - Executes on button press in pbtso0 function pbtso0_Callback(hObject, eventdata, handles) % hObject handle to pbtso0 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) global jj tmp = get(handles.kq,'string'); if(strcmp(tmp,'0.')==1)&&(jj==0) set(handles.kq,'string','0'); else tmp = strcat(tmp,'0'); set(handles.kq,'string',tmp) end jj=0; % - Executes on button press in pbtcham function pbtcham_Callback(hObject, eventdata, handles) % hObject handle to pbtcham (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) global jj tmp = get(handles.kq,'string'); if(strcmp(tmp,'0.')==1) set(handles.kq,'string','0.'); jj=1; else tmp = strcat(tmp,'.'); set(handles.kq,'string',tmp) end % - Executes on button press in pbtshift function pbtshift_Callback(hObject, eventdata, handles) % hObject handle to pbtshift (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % - Executes on button press in pbtcong function pbtcong_Callback(hObject, eventdata, handles) % hObject handle to pbtcong (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) tmp = get(handles.kq,'string'); tmp = strcat(tmp,'+'); set(handles.kq,'string',tmp) % - Executes on button press in pbtbang function pbtbang_Callback(hObject, eventdata, handles) % hObject handle to pbtbang (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) tmp = get(handles.kq,'string'); ans = eval(tmp); set(handles.kq,'string',ans) Kết quả: Ta có giao diện người dùng (GUI) máy tính điện tử Nhập liệu cần tính tốn giống máy tính bỏ túi, kết hiển thị hình Muốn tính bậc 2, sin, cos ta bấm ‘shift+sqrt’, ‘shift+sin’, ‘shift+cos’ Tính 5+5 Kết hiển thị 10 Tính Kết hiển thị 10 Tính Kết hiển thị 0.877583 Nhận xét: • • • • Chương trình hiển thị kết rõ ràng Giao diện đơn giản nhỏ gọn dễ hiểu, dễ sử dụng với người dùng Chương trình hoạt động tốt nhanh chóng Đầy đủ chức để tính tốn Lưu ý: Nút shift phát triển thêm nhiều phép tính thêm em chưa phát triển thêm

Ngày đăng: 14/06/2021, 10:40

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan