1、MATLAB课程设计报告MATLAB课程设计报告模板MATLAB课程设计报告设计题目: 函数的定积分与图像的保存 学生姓名: 陈秀 学生学号: 2009045444014 专业班级: 09通信工程(师范) 答辩时间: 2011年6月3日 指导教师: 冯明库 广东技术师范学院电子与信息学院一、设计目的及意义 运用函数quad8 求已知函数在不同上下限内的定积分,并在坐标轴上显示出函数图形,利用这种方法有利于我们更加方便的计算定积分,且准确地画出函数图像。二、设计任务及指标(1)运用函数quad8 求y=sin(x)2在不同的区间的积分值;(2)在坐标轴画出函数y=sin(x)2的图像;(3)实现
2、保存函数图像的功能。三、设计过程 3.1设计主界面介绍建立一个默认的图形用户界面,保存为jifen.fig文件,在布局编辑器中布置控件:(1)建立一个坐标轴对象,显示函数图像;(2)建立三个按钮,分别用来求函数积分,保存图像和关闭程序;(3)建立二个可编辑文本框,分别用来输入积分的上下限a和b;(4)建立五个静态文本框,显示相应控件的提示和积分结果;(5)建立一个panel控件。界面设计如图1所示图1控件布局设置控件的相关属性:(1)设置三个按钮的Tag标识分别为:jifen_pushbutton, close_pushbutton, save_pushbutton;(2)设置两个可编辑文本框
3、的Tag的标识分别为:a_edit,b_edit,用来输入积分的上下限;(3)设置界面最底下的静态文本标签Tag标识为jifen_text,用来显示积分结果。最终界面显示如图2所示图2最终界面显示3.2添加菜单3.2.1添加文件菜单建立一级菜单“文件”,在其下设置两个子菜单“积分”和“退出”。“积分”的Tag设置为jifen_menu,“退出”的Tag设置为close_menu。3.2.2添加选择菜单建立一级菜单“选择”在其下设置三个子菜单“网格”,“边框”,“颜色”(1)在“网格”下设置二个三级菜单“显示”,“隐藏”,“显示”的Callback设置为grid on,“隐藏“设置”为grid
4、off;(2)在“边框”下设置二个三级菜单“显示”,“隐藏”,“显示”的Callback设置为box on,“隐藏“设置”为box off;(3)在“颜色”下设置三个三级菜单“蓝色”,“黄色”,“绿色”,“蓝色”的Callback设置为set(gcf,Color,b),“黄色”的Callback设置为set(gcf,Color,y);“绿色”的Callback设置为set(gcf,Color,g)。如图3所示图3添加菜单3.3编写代码(1)赋予a,b初始值function jifen_OpeningFcn(hObject, eventdata, handles, varargin)% This
5、 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 jifen (see VARARGIN)% Choose default command line output for jifens
6、et(handles.b_edit,String,0);set(handles.a_edit,String,1);handles.output = hObject;(2)运用函数quad8 求函数在不同上下限内的定积分,其中a,b通过编辑文本框输入function jifen_pushbutton_Callback(hObject, eventdata, handles)% hObject handle to jifen_pushbutton (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB
7、% handles structure with handles and user data (see GUIDATA)a=str2num(get(handles.a_edit,String);b=str2num(get(handles.b_edit,String);dx=0.01;x=a:dx:b;y = sin(x).2;ff=inline(sin(x).2,x);q8_ax=quad8(ff,a,b);set(handles.jifen_text,String,num2str(q8_ax);(3)在坐标轴上画出函数图形plot(x,y,r);legend(f(x),3);title(si
8、n(x).2,Fontsize,10,FontWeight,bold);(4)赋予“保存”键保存坐标轴上图像的功能,并且图像可以保存为jpg和bmp的格式function pushbutton3_Callback(hObject, eventdata, handles)% hObject handle to pushbutton3 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDA
9、TA)new_f_handle=figure(visible,off);new_axes=copyobj(handles.axes1,new_f_handle); %axes1是GUI界面绘图的坐标系set(new_axes,units,default,position,default);filename,pathname fileindex=uiputfile(*.jpg;*.bmp,save picture as);if filename returnelse file=strcat(pathname,filename); switch fileindex %根据不同的选择保存为不同的类型
10、 case 1 print(new_f_handle,-djpeg,file); case 2 print(new_f_handle,-dbmp,file); endenddelete(new_f_handle);(4)赋予菜单栏中“积分”,“退出”功能退出:function close_menu_Callback(hObject, eventdata, handles)Close积分:function jifen_menu_Callback(hObject, eventdata, handles)jifen_pushbutton_Callback(hObject, eventdata, ha
11、ndles)四、结论及分析(1)主界面图4主界面(2)输入a,b,运行结果图5试运行(3)运用图像保存功能图6图像保存保存结果:图7保存的图像五、设计体会通过这次课程设计,我进一步了解MATLAB的程序设计过程,比较深层次地认识可视化图形用户界面设计的方法以及好处,并且复习了之前学习的MATLAB基础知识,虽说此次实训对我的难度较大,但也是一次很好的锻炼机会参考文献:1陈垚光,毛涛涛,王正林,王玲编著.精通MATLAB GUI.北京:电子工业出版社,2008.2.程序清单:function varargout = jifen(varargin)% JIFEN M-file for jifen.
12、fig% JIFEN, by itself, creates a new JIFEN or raises the existing% singleton*.% H = JIFEN returns the handle to a new JIFEN or the handle to% the existing singleton*.% JIFEN(CALLBACK,hObject,eventData,handles,.) calls the local% function named CALLBACK in JIFEN.M with the given input arguments.% JIF
13、EN(Property,Value,.) creates a new JIFEN or raises the% existing singleton*. Starting from the left, property value pairs are% applied to the GUI before jifen_OpeningFunction gets called. An% unrecognized property name or invalid value makes property application% stop. All inputs are passed to jifen
14、_OpeningFcn via varargin.% *See GUI Options on GUIDEs Tools menu. Choose GUI allows only one% instance to run (singleton).% See also: GUIDE, GUIDATA, GUIHANDLES% Copyright 2002-2003 The MathWorks, Inc.% Edit the above text to modify the response to help jifen% Last Modified by GUIDE v2.5 02-Jun-2011
15、 16:17:04% Begin initialization code - DO NOT EDITgui_Singleton = 1;gui_State = struct(gui_Name, mfilename, . gui_Singleton, gui_Singleton, . gui_OpeningFcn, jifen_OpeningFcn, . gui_OutputFcn, jifen_OutputFcn, . gui_LayoutFcn, , . gui_Callback, );if nargin & ischar(varargin1) gui_State.gui_Callback
16、= str2func(varargin1);endif nargout varargout1:nargout = gui_mainfcn(gui_State, varargin:);else gui_mainfcn(gui_State, varargin:);end% End initialization code - DO NOT EDIT% - Executes just before jifen is made visible.function jifen_OpeningFcn(hObject, eventdata, handles, varargin)% This function h
17、as 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 jifen (see VARARGIN)% Choose default command line output for jifenset(handles.
18、a_edit,String,0);set(handles.b_edit,String,1);handles.output = hObject;% Update handles structureguidata(hObject, handles);% UIWAIT makes jifen wait for user response (see UIRESUME)% uiwait(handles.figure1);% - Outputs from this function are returned to the command line.function varargout = jifen_Ou
19、tputFcn(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
20、 structurevarargout1 = handles.output;% - Executes on button press in jifen_pushbutton.function jifen_pushbutton_Callback(hObject, eventdata, handles)% hObject handle to jifen_pushbutton (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and
21、user data (see GUIDATA)a=str2num(get(handles.a_edit,String);b=str2num(get(handles.b_edit,String);dx=0.01;x=a:dx:b;y = sin(x).2;ff=inline(sin(x).2,x);q8_ax=quad8(ff,a,b);set(handles.jifen_text,String,num2str(q8_ax);plot(x,y,r);legend(f(x),3);title(sin(x)2,Fontsize,10,FontWeight,bold);% - Executes on
22、button press in save_pushbutton.function save_pushbutton_Callback(hObject, eventdata, handles)% hObject handle to save_pushbutton (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)new_f_handle=figure(visible,off);n
23、ew_axes=copyobj(handles.axes1,new_f_handle); %axes1是GUI界面绘图的坐标系set(new_axes,units,default,position,default);filename,pathname fileindex=uiputfile(*.jpg;*.bmp,save picture as);if filename returnelse file=strcat(pathname,filename); switch fileindex %根据不同的选择保存为不同的类型 case 1 print(new_f_handle,-djpeg,fil
24、e); case 2 print(new_f_handle,-dbmp,file); endenddelete(new_f_handle);% - Executes on button press in close_pushbutton.function close_pushbutton_Callback(hObject, eventdata, handles)% hObject handle to close_pushbutton (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% han
25、dles structure with handles and user data (see GUIDATA)closefunction a_edit_Callback(hObject, eventdata, handles)% hObject handle to a_edit (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,Str
26、ing) returns contents of a_edit as text% str2double(get(hObject,String) returns contents of a_edit as a double% - Executes during object creation, after setting all properties.function a_edit_CreateFcn(hObject, eventdata, handles)% hObject handle to a_edit (see GCBO)% eventdata reserved - to be defi
27、ned 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 set(hObject,BackgroundColor,white);else set(hObject,BackgroundColor,get(0,defaultUicontrolBackgroun
28、dColor);endfunction b_edit_Callback(hObject, eventdata, handles)% hObject handle to b_edit (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 b_edit as text% str2doub
29、le(get(hObject,String) returns contents of b_edit as a double% - Executes during object creation, after setting all properties.function b_edit_CreateFcn(hObject, eventdata, handles)% hObject handle to b_edit (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles empty
30、- handles not created until after all CreateFcns called% Hint: edit controls usually have a white background on Windows.% See ISPC and COMPUTER.if ispc set(hObject,BackgroundColor,white);else set(hObject,BackgroundColor,get(0,defaultUicontrolBackgroundColor);end% -function jifen_menu_Callback(hObject, eventdata, handles)% hObject handle to jifen_menu (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1