⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 zxp.asv

📁 matlab中GUI界面来实现运筹学中常用的几种最优算法
💻 ASV
字号:
function varargout = zxp(varargin)
% ZXP M-file for zxp.fig
%      ZXP, by itself, creates a new ZXP or raises the existing
%      singleton*.
%
%      H = ZXP returns the handle to a new ZXP or the handle to
%      the existing singleton*.
%
%      ZXP('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in ZXP.M with the given input arguments.
%
%      ZXP('Property','Value',...) creates a new ZXP or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before zxp_OpeningFunction gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to zxp_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 zxp

% Last Modified by GUIDE v2.5 27-Dec-2007 12:24:32
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @zxp_OpeningFcn, ...
                   'gui_OutputFcn',  @zxp_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin & isstr(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 zxp is made visible.
function zxp_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 zxp (see VARARGIN)

% Choose default command line output for zxp
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes zxp wait for user response (see UIRESUME)
% uiwait(handles.figure1);
set(handles.edit1,'string',[0.0001])
set(handles.edit3,'string',[0])
set(handles.edit4,'string',[0])
set(handles.edit5,'string',[0])
set(handles.edit6,'visible','off','string',[0.618])
set(handles.text11,'visible','off')


% --- Outputs from this function are returned to the command line.
function varargout = zxp_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)% --- 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)% --- Executes on button press in pushbutton3.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 GUIDATA)% --- 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    set(hObject,'BackgroundColor','white');else    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));endfunction 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 popupmenu1_CreateFcn(hObject, eventdata, handles)% hObject    handle to popupmenu1 (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    empty - handles not created until after all CreateFcns called% Hint: popupmenu 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% --- Executes on selection change in popupmenu1.function popupmenu1_Callback(hObject, eventdata, handles)% hObject    handle to popupmenu1 (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)% Hints: contents = get(hObject,'String') returns popupmenu1 contents as cell array%        contents{get(hObject,'Value')} returns selected item from popupmenu1% --- Executes on button press in pushbutton4.function pushbutton4_Callback(hObject, eventdata, handles)% hObject    handle to pushbutton4 (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 pushbutton5.function pushbutton5_Callback(hObject, eventdata, handles)% hObject    handle to pushbutton5 (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 pushbutton6.function pushbutton6_Callback(hObject, eventdata, handles)% hObject    handle to pushbutton6 (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 pushbutton7.function pushbutton7_Callback(hObject, eventdata, handles)% hObject    handle to pushbutton7 (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)% --- Executes during object creation, after setting all properties.function edit2_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    set(hObject,'BackgroundColor','white');else    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));endfunction edit2_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 edit3_CreateFcn(hObject, eventdata, handles)% hObject    handle to edit3 (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    set(hObject,'BackgroundColor','white');else    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));endfunction edit3_Callback(hObject, eventdata, handles)% hObject    handle to edit3 (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 edit3 as text%        str2double(get(hObject,'String')) returns contents of edit3 as a double% --- Executes on button press in pushbutton9.function pushbutton9_Callback(hObject, eventdata, handles)% hObject    handle to pushbutton9 (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)s=get(handles.edit1,'string')
delta=str2num(s)


fun1=inline('8*x.^3-2*x.^2-7*x+3')
fun2=inline('24*x.^2-4*x-7')
popup_value=get(handles.popupmenu1,'value')
switch popup_value
    case 1 
        [y,x,time,count]=quanju(fun1,0,1,delta)
    case 2
        [y,x,time,count]=seperate(fun1,fun2,0,1,delta)
    case 3
        %[y,x,time,count]=fabonacci(fun1,0,1,delta)
      
        set(handles.text11,'visible','on')
        set(handles.edit6,'visible','on')
        da=get(handles.edit6,'string')
        dt=str2num(da)
        if dt<1&dt>0
           [y,x,time,count]=fabonacci(fun1,0,1,delta,dt)
       end
    case 4
        [y,x,time,count]=golden(fun1,0,1,delta)
end

set(handles.edit3,'string',x)
set(handles.edit4,'string',count)
set(handles.edit5,'string',time)

axes(handles.axes1)
cla
t=0:0.001:1;
yy=feval(fun1,t);
plot(t,yy,x,y,'o')
        % --- Executes during object creation, after setting all properties.function edit4_CreateFcn(hObject, eventdata, handles)% hObject    handle to edit4 (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    set(hObject,'BackgroundColor','white');else    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));endfunction edit4_Callback(hObject, eventdata, handles)% hObject    handle to edit4 (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 edit4 as text%        str2double(get(hObject,'String')) returns contents of edit4 as a double% --- Executes during object creation, after setting all properties.function edit5_CreateFcn(hObject, eventdata, handles)% hObject    handle to edit5 (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    set(hObject,'BackgroundColor','white');else    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));endfunction edit5_Callback(hObject, eventdata, handles)% hObject    handle to edit5 (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 edit5 as text%        str2double(get(hObject,'String')) returns contents of edit5 as a doublefunction [y,x,time,count]=quanju(fun,a,b,delta)

size=(b-a)/delta
y_min=feval(fun,a);
x_min=a;
k=0;
tic
for i=1:size
  if y_min>feval(fun,a+i*delta)
      y_min=feval(fun,a+i*delta);
      x_min=a+i*delta;
  end
  k=k+1;
end

time=toc
count=k
y=y_min
x=x_min



function [y,x,time,count]=seperate(fun1,fun2,a,b,delta)

size=(b-a)/delta;

k=1
c=(a+b)/2;
tic
while k<size
    if feval(fun2,a)*feval(fun2,c)<0
        b=c;
        c=(a+b)/2;
    elseif feval(fun2,c)*feval(fun2,b)<0
        a=c;
        c=(a+b)/2;
    else
        x=c;
        break
    end

    k=k+1
end

time=toc;
count=k;
x=c;
y=feval(fun1,x);




function [y,x,time,count]=fabonacci(fun,a,b,delta,dt)

if dt>0.5
else dt=1-dt
end
size=(b-a)/delta;
t1=a+dt*(b-a);
t2=a+b-t1;

k=1
tic

while k<size
   
    if feval(fun,t1)>feval(fun,t2)
        
        if b-t1<delta
            x=t2
            break;
        else
           a=t1
           t1=t2;
           t2=a+(1-dt)*(b-a);
           k=k+1;
       end
       
   else
       
       if t2-a<delta
           x=t1
           break
       else
           b=t2
           t2=t1
           t1=a+dt*(b-a)
           k=k+1
       end
   end  
end 
count=k
time=toc
y=feval(fun,x)





function [y,x,time,count]=golden(fun,a,b,delta)

size=(b-a)/delta;
t1=a+0.382*(b-a);
t2=a+b-t1;

k=1
tic

while k<size
   
    if feval(fun,t1)>feval(fun,t2)
        
        if b-t1<delta
            x=t2
            break;
        else
           a=t1
           t1=t2;
           t2=a+0.618*(b-a);
           k=k+1;
       end
       
   else
       
       if t2-a<delta
           x=t1
           break
       else
           b=t2
           t2=t1
           t1=a+0.382*(b-a)
           k=k+1
       end
   end  
end 
count=k
time=toc
y=feval(fun,x)% --- Executes on button press in pushbutton10.function pushbutton10_Callback(hObject, eventdata, handles)% hObject    handle to pushbutton10 (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)
cla

set(handles.edit1,'string',[0.0001])
set(handles.edit3,'string',[0])
set(handles.edit4,'string',[0])
set(handles.edit5,'string',[0])
% --- 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    set(hObject,'BackgroundColor','white');else    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));endfunction 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

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -