0

我是编程新手,我必须使用常微分方程 (ODE) 创建一个 matlab gui 代码,然后绘制所述方程。我遇到的问题是 ode 的函数在一个 .m 文件中定义并在另一个 .m 文件中调用的格式,如下所示:

% First part
% This is the ode .m file
function dydt=project(t,y)
 dydt=zeros(3,1);
 y(1);
 y(2);
 y(3);
 dydt(1) = (some math);
 dydt(2) = (some math);
 dydt(3) = (some math);
end

% Second part
% This is a call for the ode function defined in the above .m file
% This can be in another .m file or called from command window
[t,y]=ode45('project',[0 10],[9.8 6591 61.3]);

我的代码要求我创建一个 gui,它会在 gui 程序启动后立即初始化 ode 函数(第一部分),然后将接受初始条件并在按下按钮后立即计算对函数的调用(第二部分)。

我的问题是:我必须调用 .m 文件还是有办法将上述 ode 函数编码到 gui 代码中?如何将第二部分输入到 gui 代码中?

感谢所有做出贡献的人。

编辑:下面的代码是 GUI 的 GUIDE 代码,您可以随意测试它并确认错误。

function varargout = reaction_kinetics(varargin)
% REACTION_KINETICS MATLAB code for reaction_kinetics.fig
%REACTION_KINETICS, by itself, creates a new REACTION_KINETICS or raises the existing
%      singleton*.
%
%H = REACTION_KINETICS returns the handle to a new REACTION_KINETICS or the handle to
%      the existing singleton*.
%
%REACTION_KINETICS('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in REACTION_KINETICS.M with the given input arguments.
%
%REACTION_KINETICS('Property','Value',...) creates a new REACTION_KINETICS or raises   
%the existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before reaction_kinetics_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to reaction_kinetics_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 reaction_kinetics

% Last Modified by GUIDE v2.5 16-Apr-2013 18:44:46

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @reaction_kinetics_OpeningFcn, ...
                   'gui_OutputFcn',  @reaction_kinetics_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 reaction_kinetics is made visible.
function reaction_kinetics_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 reaction_kinetics (see VARARGIN)

% Perform first set of ODE's
function dydt=project(t,y)
dydt=zeros(3,1);
glc=y(1);
gsh=y(2);
gsg=y(3);
dydt(1)= (-5400*((924*glc)-(gsh/5600)))/((22*300)+(glc*300)+924*22*(1+(glc/22)+(gsh/30)));
dydt(2)=(5400*((924*glc)-(gsh/5600)))/((22*300)+(glc*300)+924*22*(1+(glc/22)+(gsh/30)))-(150*gsh/(150+gsh))-(1100*(gsh^3)/((3000^3)+(gsh^3)))-((2*4500*.01*gsh)/((1330+gsh)*(0.09+0.01)))+((2*8925*gsg*50)/((107+gsg)*(10.4+50)))-0.002*gsh;
dydt(3)=(4500*.01*gsh)/((1330+gsh)*(0.09+0.01))- ((8925*gsg*50)/((107+gsg)*(10.4+50)))-((40*gsg)/(1250+gsg))-((4025*gsg)/(7100+gsg))-0.1*gsg;
end

handles.project=project;
handles.current_data=handles.project;



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

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes reaction_kinetics wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = reaction_kinetics_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 selection change in popupmenu2.
function popupmenu2_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu2 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu2

val=get(hObject,'Value');
str=get(hObject,'String');
switch str{val}
    case 'GSH Half Life' % User selects GSH Half Life
        handles.current_data=handles.plot(t,y);
    case 'GSH v Glc' % User selects GSH v Glc
        handles.current_data=handles.plot(y(2),y(1));
    case 'GSH v GSSG' % User selects GSH v GSSG
        handles.current_data=handles.plot(y(2),y(3));
end
guidata(hObject,handles);


% --- Executes during object creation, after setting all properties.
function popupmenu2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu2 (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in plot_button.
function plot_button_Callback(hObject, eventdata, handles)
% hObject    handle to plot_button (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Plot using the popup menu
plot(handles.current_data);


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

% Convert string to numeric value for gsh_value
gsh_ivalue = str2double(get(hObject,'string'));
if isnan(gsh_ivalue)
  errordlg('You must enter a numeric value','Bad Input','modal')
  uicontrol(hObject)
    return
end


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

gssg_ivalue = str2double(get(hObject,'string'));
if isnan(gssg_ivalue)
  errordlg('You must enter a numeric value','Bad Input','modal')
  uicontrol(hObject)
    return
end


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

glc_ivalue = str2double(get(hObject,'string'));
if isnan(glc_ivalue)
  errordlg('You must enter a numeric value','Bad Input','modal')
  uicontrol(hObject)
    return
end


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


% --- Executes on button press in calculate_button.
function calculate_button_Callback(hObject, eventdata, handles)
% hObject    handle to calculate_button (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Calculate the ode
[t,y]=ode45('project',[0 10],[glc_ivalue gsh_ivalue gssg_ivalue]);
4

1 回答 1

0

Matlab GUI 是事件驱动的,这意味着做事意味着设置图形对象的回调, 例如uicontrol使用样式pushbutton(例如)来实现或调用您的自定义代码。这意味着:每次您单击按钮时,您的自定义代码都会运行。

于 2013-04-16T20:32:48.050 回答