Advertisement
NaroxEG

matlab calc equal button

Dec 9th, 2024
1,489
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 3.34 KB | None | 0 0
  1. % --- Executes on button press in eqBtn.
  2. function eqBtn_Callback(hObject, eventdata, handles)
  3. % Get the current text from typeBox
  4. lastText = get(handles.typeBox, 'String');
  5.  
  6. if isfield(handles, 'lastAns')
  7.     lastText = strrep(lastText, 'lastAns', num2str(handles.lastAns));
  8. end
  9.  
  10. if contains(lastText, 'intg(') && contains(lastText, ')')
  11.         % Extract the portion between 'intg(' and ')'
  12.         equationPart = extractBetween(lastText, 'intg(', ')');
  13.         if ~isempty(equationPart)
  14.             parts = strsplit(equationPart{1}, ',');
  15.             eqStr = strtrim(parts{1});
  16.             syms x;
  17.             symbolicExpr = str2sym(eqStr);
  18.             minx = str2double(strtrim(parts{2}));  % The lower limit
  19.             maxx = str2double(strtrim(parts{3}));  % The upper limit
  20.             numericIntegral = integral(matlabFunction(symbolicExpr, 'Vars', x), minx, maxx);
  21.  
  22.             integralStr = sprintf('%f', numericIntegral);
  23.             escapedEquationPart = regexptranslate('escape', equationPart{1});
  24.             lastText = regexprep(lastText, ['intg\(' escapedEquationPart '\)'], integralStr);
  25.         else
  26.             set(handles.resultBox, 'String', 'Error with integration');
  27.             return
  28.         end
  29. end
  30. if contains(lastText, 'ddx(') && contains(lastText, ')')
  31.     % Extract the portion between 'ddx(' and ')'
  32.     equationPart = extractBetween(lastText, 'ddx(', ')');
  33.     if ~isempty(equationPart)
  34.         parts = strsplit(equationPart{1}, ',');
  35.         eqStr = strtrim(parts{1});  % The equation
  36.         evalPoint = str2double(strtrim(parts{2}));  % The specific x-value
  37.        
  38.         syms x;
  39.         % Compute the derivative symbolically
  40.         symbolicExpr = str2sym(eqStr);
  41.         derivativeExpr = diff(symbolicExpr, x);
  42.    
  43.         numericDerivative = double(subs(derivativeExpr, x, evalPoint));
  44.        
  45.         derivativeStr = sprintf('%f', numericDerivative);
  46.         escapedEquationPart = regexptranslate('escape', equationPart{1});
  47.         lastText = regexprep(lastText, ['ddx\(' escapedEquationPart '\)'], derivativeStr);
  48.     else
  49.         set(handles.resultBox, 'String', 'Error with derivative calculation');
  50.         return
  51.     end
  52. end
  53.  
  54. if contains(lastText, 'logb(') && contains(lastText, ')')
  55.         % Extract the portion between 'intg(' and ')'
  56.         paramsPart = extractBetween(lastText, 'logb(', ')');
  57.         if ~isempty(paramsPart)
  58.             parts = strsplit(paramsPart{1}, ',');
  59.             base = str2double(strtrim(parts{1}));
  60.             value = str2double(strtrim(parts{2}));
  61.             logRes = log(value) / log(base);
  62.  
  63.             logStr = sprintf('%f', logRes);
  64.             escapedEquationPart = regexptranslate('escape', paramsPart{1});
  65.             lastText = regexprep(lastText, ['logb\(' escapedEquationPart '\)'], logStr);
  66.         else
  67.             set(handles.resultBox, 'String', 'Error with log');
  68.             return
  69.         end
  70. end
  71. % Try to evaluate the expression
  72. try
  73.     handles.lastAns = eval(lastText); % Store result in handles.lastAns
  74.     % Update the resultBox with the result of the evaluation
  75.     set(handles.resultBox, 'String', num2str(handles.lastAns));
  76. catch
  77.     handles.lastAns = 0; % Reset lastAns to 0 in case of error
  78.     set(handles.resultBox, 'String', 'Error');
  79. end
  80.  
  81. % Update the handles structure with the modified handles
  82. guidata(hObject, handles);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement