Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function pushbutton39_Callback(hObject, eventdata, newHandles)
- % Get the input equation and condition from edit boxes
- edit23 = findobj(0, 'tag', 'edit23');
- edit26 = findobj(0, 'tag', 'edit26');
- eqn = get(edit23, 'String'); % Equation input
- condStr = get(edit26, 'String'); % Condition input
- % Ensure the output text field exists
- text14 = findobj(0, 'tag', 'text14');
- syms x y(x)
- % Parse the ODE input
- parsedOde = parseUserInput(eqn);
- try
- % Convert ODE to symbolic expression
- ode = str2sym(parsedOde);
- catch
- errordlg('Invalid ODE format. Please use the correct notation.', 'Error');
- return;
- end
- % Parse conditions if provided
- if isempty(condStr)
- conditions = []; % No conditions provided
- else
- try
- conditions = parseConditions(condStr);
- catch
- errordlg('Invalid conditions format. Please use the correct notation.', 'Error');
- return;
- end
- end
- % Attempt to solve the ODE with conditions
- try
- if isempty(conditions)
- % Solve without conditions
- solution = dsolve(ode);
- displayText = ['Solution (with constants): ', char(solution)];
- else
- % Solve with conditions
- solution = dsolve(ode, conditions);
- % If the solution is empty, try solving without conditions
- if isempty(solution)
- solution = dsolve(ode); % Solve without conditions
- displayText = ['Solution (with constants): ', char(solution)];
- else
- % Display the solution with conditions
- displayText = ['Solution: ', char(solution)];
- end
- end
- % Display the solution
- set(text14, 'String', displayText);
- catch ME
- % If solving fails, show error
- errordlg(['Unable to solve the ODE. ', ME.message], 'Error');
- end
- function parsedConditions = parseConditions(condStr)
- % Split conditions by semicolons
- condList = split(condStr, ';');
- parsedConditions = cell(size(condList));
- syms x y(x) % Ensure symbolic variables are defined
- for i = 1:length(condList)
- cond = strtrim(condList{i}); % Remove spaces
- if isempty(cond)
- continue; % Skip empty conditions
- end
- try
- % Prepare the condition
- if contains(cond, '=')
- parts = split(cond, '=');
- leftSide = strtrim(parts{1}); % Left side of condition
- rightSide = strtrim(parts{2}); % Right side of condition
- % Normalize condition syntax
- if contains(leftSide, '''')
- % For derivative conditions (e.g., y' = 1)
- cond = ['subs(diff(y(x), x), x, 0) == ', rightSide];
- else
- % For value conditions (e.g., y(0) = 1)
- cond = ['subs(y(x), x, 0) == ', rightSide];
- end
- end
- % Convert to symbolic form
- parsedConditions{i} = eval(cond);
- catch ME
- % If parsing fails, show error
- errordlg(['Error parsing condition: ', cond, newline, ME.message], 'Condition Parsing Error');
- parsedConditions{i} = [];
- end
- end
- % Remove empty cells and flatten
- parsedConditions = parsedConditions(~cellfun('isempty', parsedConditions));
- % Combine all conditions into a single expression
- if ~isempty(parsedConditions)
- parsedConditions = [parsedConditions{:}];
- else
- parsedConditions = [];
- end
- function parsedStr = parseUserInput(userInput)
- % Replace y(5) with diff(y, x, 5)
- parsedStr = regexprep(userInput, 'y\((\d+)\)', 'diff(y, x, $1)');
- % Handle other derivatives and equality signs
- parsedStr = regexprep(parsedStr, "y''''", "diff(y, x, 4)");
- parsedStr = regexprep(parsedStr, "y'''", "diff(y, x, 3)");
- parsedStr = regexprep(parsedStr, "y''", "diff(y, x, 2)");
- parsedStr = regexprep(parsedStr, "y'", "diff(y, x)");
- parsedStr = strrep(parsedStr, "=", "=="); % Ensure equality symbol is MATLAB compatible
- parsedStr = strrep(parsedStr, 'y', 'y(x)');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement