GUI - ODE problem, can someone please help me solve an ode system using guide?

2 ビュー (過去 30 日間)
Jani
Jani 2021 年 11 月 24 日
編集済み: Cris LaPierre 2021 年 12 月 29 日
I want to make a user interface with gui, which solves a sysmet of differential equations, and some of the variables (f1 and f2) in the equations can be manually written in the user interface, but if I write the f1 and f2 before the ode solver it gives me this error:
"Unrecognized function or variable 'f1'."
If I write the f1 and f2 in the ode sovler function it gives me this error:
"Unable to resolve the name handles.f1_input."
It also gives me this error, if I take out the f1 and f2 from the equations and replace them with numbers.
Here is the part where's my ode solver function
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)
tspan = [0 1000];
y0 = [0.1 25 25];
[t,y] = ode45(@(t,y) odefcn(t,y), tspan, y0);
function dydt = odefcn(t,y)
f1 = str2double(get(handles.f1_input,'String'));
f2 = str2double(get(handles.f2_input,'String'));
T_0 = 17;
T_ain = 25;
a3 = 12.6;
a4 = 393.2;
a5 = 5.2e-4;
dydt = zeros(3,1);
dydt(1) = -a5*(y(3)-T_0)*y(1);
dydt(2) = -a3*(y(2)-T_ain)+a4;
dydt(3)=f1*(y(2)-y(3)) + f2*dydt(1);
plot(handles.moisture_axes,t,y(:,1));
And here is my full script and a picture of the guide:
function varargout = wface(varargin)
% WFACE MATLAB code for wface.fig
% WFACE, by itself, creates a new WFACE or raises the existing
% singleton*.
%
% H = WFACE returns the handle to a new WFACE or the handle to
% the existing singleton*.
%
% WFACE('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in WFACE.M with the given input arguments.
%
% WFACE('Property','Value',...) creates a new WFACE or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before wface_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to wface_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 wface
% Last Modified by GUIDE v2.5 24-Nov-2021 19:20:02
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @wface_OpeningFcn, ...
'gui_OutputFcn', @wface_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 wface is made visible.
function wface_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 wface (see VARARGIN)
% Choose default command line output for wface
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes wface wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = wface_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 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)
tspan = [0 1000];
y0 = [0.1 25 25];
[t,y] = ode45(@(t,y) odefcn(t,y), tspan, y0);
function dydt = odefcn(t,y)
f1 = str2double(get(handles.f1_input,'String'));
f2 = str2double(get(handles.f2_input,'String'));
T_0 = 17;
T_ain = 25;
a3 = 12.6;
a4 = 393.2;
a5 = 5.2e-4;
dydt = zeros(3,1);
dydt(1) = -a5*(y(3)-T_0)*y(1);
dydt(2) = -a3*(y(2)-T_ain)+a4;
dydt(3)=f1*(y(2)-y(3)) + f2*dydt(1);
plot(handles.moisture_axes,t,y(:,1));
function f1_input_Callback(hObject, eventdata, handles)
% hObject handle to f1_input (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 f1_input as text
% str2double(get(hObject,'String')) returns contents of f1_input as a double
% --- Executes during object creation, after setting all properties.
function f1_input_CreateFcn(hObject, eventdata, handles)
% hObject handle to f1_input (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 f2_input_Callback(hObject, eventdata, handles)
% hObject handle to f2_input (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 f2_input as text
% str2double(get(hObject,'String')) returns contents of f2_input as a double
% --- Executes during object creation, after setting all properties.
function f2_input_CreateFcn(hObject, eventdata, handles)
% hObject handle to f2_input (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

回答 (1 件)

Cris LaPierre
Cris LaPierre 2021 年 12 月 29 日
編集済み: Cris LaPierre 2021 年 12 月 29 日
Consider variable scope. Your odefcn function does not have a variable handles passed in as input, nor created inside it. Therefore, handles does not exist in this scope.
There are at least two fixes. The simplest is to make your odefcn a nested function of plot_button_Callback. Do this by adding end to all functions in your m-file, and be sure to put plot_button_Callack's end after the odefcn function.
Note that your plot command should probably be in the button callback just after the call to ode45 and not in your odefcn.
% --- 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)
tspan = [0 1000];
y0 = [0.1 25 25];
[t,y] = ode45(@odefcn, tspan, y0,[],handles);
plot(handles.moisture_axes,t,y(:,1));
function dydt = odefcn(t,y,handles)
f1 = str2double(get(handles.f1_input,'String'));
f2 = str2double(get(handles.f2_input,'String'));
T_0 = 17;
T_ain = 25;
a3 = 12.6;
a4 = 393.2;
a5 = 5.2e-4;
dydt = zeros(3,1);
dydt(1) = -a5*(y(3)-T_0)*y(1);
dydt(2) = -a3*(y(2)-T_ain)+a4;
dydt(3)=f1*(y(2)-y(3)) + f2*dydt(1);
end
end
The second option is to pass the handles structure into odefucn as an additional input.
% --- 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)
tspan = [0 1000];
y0 = [0.1 25 25];
[t,y] = ode45(@odefcn, tspan, y0,[],handles);
plot(handles.moisture_axes,t,y(:,1));
end
function dydt = odefcn(t,y,handles)
...
end
By making those changes, your app runs without error and creates a plot (it takes a few seconds).

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by