绘制y=sin(x)/x的图形

来源:互联网 发布:竞品分析ppt模板 知乎 编辑:程序博客网 时间:2024/05/29 11:31

  用Matlab里的GUI工具,可以绘制从函数y = sin(x)/x 在x∈【-100,100】,y∈【-0.24,1.1】下的曲线,并观察和推断当x –>+∞,x–>-∞,x–>∞, x –> 0-, x–> 0+, x –> 0时,函数的变化趋势。
  1)打开Matlab7.0,点击工具栏上的【File】–》 New –》GUI,命名为:jiXian.gif
  2)在jiXian.gif面板里,拖曳2个坐标系,3个按钮,如图(1)所示:

这里写图片描述
图(1) 用鼠标右键拖曳按钮,即为复制按钮,

  3)设置3个按钮的名称、标识
  三个按钮:comet、limit、close,如表(1)所示
  
表(1) 3个按钮

名称标识按钮1comet comet_pushButton按钮2limitlimit_pushButton按钮3closeclose_pushButton

  修改按钮的属性,这里以【comet】按钮为例。双击按钮comet, 弹出Property Inspector对话框,修改String属性为:comet,Tag属性为: comet_pushButton,点击Callback属性 —>OK,如图(2)所示:
  

这里写图片描述
图(2) 修改按钮的String、Tag和Callback属性

  4)添加1个一级菜单,3个二级菜单
  一级菜单:File,二级菜单:comet、limit、close,如表(2)所示:
表(2) 4个菜单

名称标识一级菜单File File_menu二级菜单cometcomet_menu二级菜单limitlimit_menu二级菜单closeclose_menu

  5)编写3个按钮和4个菜单的响应函数或脚本
  打开jiXian.m文件,编写按钮和菜单对应的函数
  代码如下:
  %%jiXian.m

function varargout = jiXian(varargin)% JIXIAN M-file for jiXian.fig%      JIXIAN, by itself, creates a new JIXIAN or raises the existing%      singleton*.%%      H = JIXIAN returns the handle to a new JIXIAN or the handle to%      the existing singleton*.%%      JIXIAN('CALLBACK',hObject,eventData,handles,...) calls the local%      function named CALLBACK in JIXIAN.M with the given input arguments.%%      JIXIAN('Property','Value',...) creates a new JIXIAN or raises the%      existing singleton*.  Starting from the left, property value pairs are%      applied to the GUI before jiXian_OpeningFunction gets called.  An%      unrecognized property name or invalid value makes property application%      stop.  All inputs are passed to jiXian_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% Copyright 2002-2003 The MathWorks, Inc.% Edit the above text to modify the response to help jiXian% Last Modified by GUIDE v2.5 24-Nov-2015 18:30:05% Begin initialization code - DO NOT EDITgui_Singleton = 1;gui_State = struct('gui_Name',       mfilename, ...                   'gui_Singleton',  gui_Singleton, ...                   'gui_OpeningFcn', @jiXian_OpeningFcn, ...                   'gui_OutputFcn',  @jiXian_OutputFcn, ...                   'gui_LayoutFcn',  [] , ...                   'gui_Callback',   []);if nargin && ischar(varargin{1})    gui_State.gui_Callback = str2func(varargin{1});endif 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 jiXian is made visible.function jiXian_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 jiXian (see VARARGIN)% Choose default command line output for jiXianhandles.output = hObject;% Update handles structureguidata(hObject, handles);% UIWAIT makes jiXian wait for user response (see UIRESUME)% uiwait(handles.figure1);% --- Outputs from this function are returned to the command line.function varargout = jiXian_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 structurevarargout{1} = handles.output;% --- Executes on button press in comet_pushButton.function comet_pushButton_Callback(hObject, eventdata, handles)% hObject    handle to comet_pushButton (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)axes(handles.axes1); x=-700:0.1:700;box on;hold on;axis([-700 700 -0.24 1.1]);comet(x,sin(x)./x);% --- Executes on button press in limit_pushButton.function limit_pushButton_Callback(hObject, eventdata, handles)% hObject    handle to limit_pushButton (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)axes(handles.axes2); fplot('sin(x)./x',[-100 100 -0.24 1.1]);text(30,0.5,'sin(x)/x');% --- 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% handles    structure with handles and user data (see GUIDATA)close% --------------------------------------------------------------------function comet_menu_Callback(hObject, eventdata, handles)% hObject    handle to comet_menu (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)comet_pushButton_Callback(hObject, eventdata, handles)% --------------------------------------------------------------------function limit_menu_Callback(hObject, eventdata, handles)% hObject    handle to limit_menu (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)limit_pushButton_Callback(hObject, eventdata, handles)% --------------------------------------------------------------------function close_menu_Callback(hObject, eventdata, handles)% hObject    handle to close_menu (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)close% --------------------------------------------------------------------function File_menu_Callback(hObject, eventdata, handles)% hObject    handle to File_menu (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)

  效果如下:

这里写图片描述
图(3) 绘制y = sin(x)/x ,x∈【-100,100】

  由上图,可知,
  limx0sinxx=1,
  limx0+sinxx=1
  limx0sinxx=1
  limxsinxx=0,
  limx+sinxx=0,
  limxsinxx=0

1 0