How can one assigning an input on a GUI edit text box as a function for integration?

I need to create a basic GUI that will provide a definite integral for a function the user defines in an edit text box and selected upper and lower limits of integration.
I have tried the str2func function to convert the string from the box into a function, but I receive the following errors:
* Warning: The input to STR2FUNC "(x)x.^2+2*x" is not a valid function name. This will generate an error in a future release.
* > In HW9_gui_JTW>compute_Callback at 85
* In gui_mainfcn at 96
* In HW9_gui_JTW at 42
* In @(hObject,eventdata)HW9_gui_JTW('compute_Callback',hObject,eventdata,guidata(hObject))
* Undefined function '(x)x.^2+2*x' for input arguments of type
* 'double'.
*
* Error in quad (line 72)
* y = f(x, varargin{:});
*
* Error in HW9_gui_JTW>compute_Callback (line 86)
* s = quad(f,a,b);
*
* Error in gui_mainfcn (line 96)
* feval(varargin{:});
*
* Error in HW9_gui_JTW (line 42)
* gui_mainfcn(gui_State, varargin{:});
*
* Error in
* @(hObject,eventdata)HW9_gui_JTW('compute_Callback',hObject,eventdata,guidata(hObject))
*
*
* Error while evaluating uicontrol Callback
My GUI consists of 3 input text boxes (lower_lim, upper_lim, function_input) and a push button (compute)
Here is the code to the compute_Callback, The only part of the code I have added to:
*% --- Executes on button press in compute.
function compute_Callback(hObject, eventdata, handles)
% hObject handle to compute (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
a = str2num(get(handles.lower_lim,'String'));
b = str2num(get(handles.upper_lim,'String'));
y = get(handles.enter_function,'String');
d = ['(x)' y];
f = str2func(d);
s = quad(f,a,b);
sol = num2str(s);
set(handles.solution,'String',sol);*
Thanks!

 采纳的回答

更多回答(1 个)

Works like a charm. I had a feeling it was something silly like that.
Thanks!

类别

帮助中心File Exchange 中查找有关 Numerical Integration and Differentiation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by