Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- % --- Executes on button press in eqBtn.
- function eqBtn_Callback(hObject, eventdata, handles)
- % Get the current text from typeBox
- lastText = get(handles.typeBox, 'String');
- if isfield(handles, 'lastAns')
- lastText = strrep(lastText, 'lastAns', num2str(handles.lastAns));
- end
- if contains(lastText, 'intg(') && contains(lastText, ')')
- % Extract the portion between 'intg(' and ')'
- equationPart = extractBetween(lastText, 'intg(', ')');
- if ~isempty(equationPart)
- parts = strsplit(equationPart{1}, ',');
- eqStr = strtrim(parts{1});
- syms x;
- symbolicExpr = str2sym(eqStr);
- minx = str2double(strtrim(parts{2})); % The lower limit
- maxx = str2double(strtrim(parts{3})); % The upper limit
- numericIntegral = integral(matlabFunction(symbolicExpr, 'Vars', x), minx, maxx);
- integralStr = sprintf('%f', numericIntegral);
- escapedEquationPart = regexptranslate('escape', equationPart{1});
- lastText = regexprep(lastText, ['intg\(' escapedEquationPart '\)'], integralStr);
- else
- set(handles.resultBox, 'String', 'Error with integration');
- return
- end
- end
- if contains(lastText, 'ddx(') && contains(lastText, ')')
- % Extract the portion between 'ddx(' and ')'
- equationPart = extractBetween(lastText, 'ddx(', ')');
- if ~isempty(equationPart)
- parts = strsplit(equationPart{1}, ',');
- eqStr = strtrim(parts{1}); % The equation
- evalPoint = str2double(strtrim(parts{2})); % The specific x-value
- syms x;
- % Compute the derivative symbolically
- symbolicExpr = str2sym(eqStr);
- derivativeExpr = diff(symbolicExpr, x);
- numericDerivative = double(subs(derivativeExpr, x, evalPoint));
- derivativeStr = sprintf('%f', numericDerivative);
- escapedEquationPart = regexptranslate('escape', equationPart{1});
- lastText = regexprep(lastText, ['ddx\(' escapedEquationPart '\)'], derivativeStr);
- else
- set(handles.resultBox, 'String', 'Error with derivative calculation');
- return
- end
- end
- if contains(lastText, 'logb(') && contains(lastText, ')')
- % Extract the portion between 'intg(' and ')'
- paramsPart = extractBetween(lastText, 'logb(', ')');
- if ~isempty(paramsPart)
- parts = strsplit(paramsPart{1}, ',');
- base = str2double(strtrim(parts{1}));
- value = str2double(strtrim(parts{2}));
- logRes = log(value) / log(base);
- logStr = sprintf('%f', logRes);
- escapedEquationPart = regexptranslate('escape', paramsPart{1});
- lastText = regexprep(lastText, ['logb\(' escapedEquationPart '\)'], logStr);
- else
- set(handles.resultBox, 'String', 'Error with log');
- return
- end
- end
- % Try to evaluate the expression
- try
- handles.lastAns = eval(lastText); % Store result in handles.lastAns
- % Update the resultBox with the result of the evaluation
- set(handles.resultBox, 'String', num2str(handles.lastAns));
- catch
- handles.lastAns = 0; % Reset lastAns to 0 in case of error
- set(handles.resultBox, 'String', 'Error');
- end
- % Update the handles structure with the modified handles
- guidata(hObject, handles);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement