GUI derivative calculator, issues with coding portion
12 views (last 30 days)
Show older comments
Hello All,
I am trying to do my final MATLAB project and it involves creating a GUI that can handle second order differential equations. Firstl I have been trying to figure out how to make a simple derivative GUI calculator, one that can handle something from the user say 4X and when a button is pushed will spit out its derivite, 4. So far I have made the GUI and have been trying to add some code. I am new to MATLAB and some of the errors are confusing. I will post parts of the code along with my logic below:
let me define some things to make it easier to understand.
I have an edit text box whose tag line is FCN (this is the box the user will enter the derivative that needs to be taken)
I have a push button whose tag line is equal
I have a static text box whose tag line is first (this is where the answer will pop out)
I wrote a few lines of code under the equal callback:
function equalPushButton_Callback(hObject, eventdata, handles)
% hObject handle to equalPushButton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
syms x
a=get(handles.FCN,'string'));
b=str2sym(diff(a)); %Or b=double(b)?
set(handles.first,'string',sym2num(b));
I am wondering if someone can steer me in the right direction
Thanks
3 Comments
Murugan C
on 8 May 2019
Hi
I think, we shouldn't use "syms" for derivative .we have create one more edit box for getting derivative as like below,
f = get(handles.FCN,'String');
x = str2double(get(handles.derivative,'String')); % derivative order
der=diff(f,x);
set(handles.first,'String',char(der));
Answers (1)
Raag
on 5 Feb 2025 at 6:47
Hi William,
The issue seems to be arising from using the ‘syms’ function within the callback. This function modifies the base workspace in a manner that is incompatible with the static nature of the GUI workspace. As a workaround, ‘str2sym’ function can be used to resolve the workspace issue.
Follow these steps to resolve the workspace issue:
- Use ‘str2sym’ function: which converts the input string into a symbolic expression without the need to define ‘syms x’. Usage of this function resolves the workspace issue, while taking input from the user.
- Differentiation: use ‘diff(f_sym, 'x')’ to compute the derivative of the expression, where ‘f_sym’ is the symbolic form of the user's input.
Here is an example code snippet for the above-mentioned workaround:
function equalPushButton_Callback(hObject, eventdata, handles)
% Get the input from the edit text box
f = get(handles.FCN, 'String');
try
% Convert the input string to a symbolic expression
f_sym = str2sym(f);
% Calculate the derivative with respect to 'x'
der = diff(f_sym, 'x');
% Display the derivative in the static text box
set(handles.first, 'String', char(der));
catch
% Display an error message if the input is invalid
set(handles.first, 'String', 'Error: Invalid input');
end
end
For more information on ‘str2sym’ function, refer to this documentation:
0 Comments
See Also
Categories
Find more on Number Theory in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!