Advertisement
NaroxEG

MatLab - ODE Solver

Dec 9th, 2024 (edited)
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 4.33 KB | None | 0 0
  1. function pushbutton39_Callback(hObject, eventdata, newHandles)
  2.     % Get the input equation and condition from edit boxes
  3.     edit23 = findobj(0, 'tag', 'edit23');
  4.     edit26 = findobj(0, 'tag', 'edit26');
  5.     eqn = get(edit23, 'String'); % Equation input
  6.     condStr = get(edit26, 'String'); % Condition input
  7.  
  8.     % Ensure the output text field exists
  9.     text14 = findobj(0, 'tag', 'text14');
  10.     syms x y(x)
  11.    
  12.     % Parse the ODE input
  13.     parsedOde = parseUserInput(eqn);
  14.    
  15.     try
  16.         % Convert ODE to symbolic expression
  17.         ode = str2sym(parsedOde);
  18.     catch
  19.         errordlg('Invalid ODE format. Please use the correct notation.', 'Error');
  20.         return;
  21.     end
  22.    
  23.     % Parse conditions if provided
  24.     if isempty(condStr)
  25.         conditions = []; % No conditions provided
  26.     else
  27.         try
  28.             conditions = parseConditions(condStr);
  29.         catch
  30.             errordlg('Invalid conditions format. Please use the correct notation.', 'Error');
  31.             return;
  32.         end
  33.     end
  34.    
  35.     % Attempt to solve the ODE with conditions
  36.     try
  37.         if isempty(conditions)
  38.             % Solve without conditions
  39.             solution = dsolve(ode);
  40.             displayText = ['Solution (with constants): ', char(solution)];
  41.         else
  42.             % Solve with conditions
  43.             solution = dsolve(ode, conditions);
  44.            
  45.             % If the solution is empty, try solving without conditions
  46.             if isempty(solution)
  47.                 solution = dsolve(ode); % Solve without conditions
  48.                 displayText = ['Solution (with constants): ', char(solution)];
  49.             else
  50.                 % Display the solution with conditions
  51.                 displayText = ['Solution: ', char(solution)];
  52.             end
  53.         end
  54.        
  55.         % Display the solution
  56.         set(text14, 'String', displayText);
  57.     catch ME
  58.         % If solving fails, show error
  59.         errordlg(['Unable to solve the ODE. ', ME.message], 'Error');
  60.     end
  61.  
  62.  
  63. function parsedConditions = parseConditions(condStr)
  64.     % Split conditions by semicolons
  65.     condList = split(condStr, ';');
  66.     parsedConditions = cell(size(condList));
  67.    
  68.     syms x y(x)  % Ensure symbolic variables are defined
  69.    
  70.     for i = 1:length(condList)
  71.         cond = strtrim(condList{i}); % Remove spaces
  72.         if isempty(cond)
  73.             continue; % Skip empty conditions
  74.         end
  75.  
  76.         try
  77.             % Prepare the condition
  78.             if contains(cond, '=')
  79.                 parts = split(cond, '=');
  80.                 leftSide = strtrim(parts{1});  % Left side of condition
  81.                 rightSide = strtrim(parts{2}); % Right side of condition
  82.  
  83.                 % Normalize condition syntax
  84.                 if contains(leftSide, '''')
  85.                     % For derivative conditions (e.g., y' = 1)
  86.                     cond = ['subs(diff(y(x), x), x, 0) == ', rightSide];
  87.                 else
  88.                     % For value conditions (e.g., y(0) = 1)
  89.                     cond = ['subs(y(x), x, 0) == ', rightSide];
  90.                 end
  91.             end
  92.            
  93.             % Convert to symbolic form
  94.             parsedConditions{i} = eval(cond);
  95.         catch ME
  96.             % If parsing fails, show error
  97.             errordlg(['Error parsing condition: ', cond, newline, ME.message], 'Condition Parsing Error');
  98.             parsedConditions{i} = [];
  99.         end
  100.     end
  101.    
  102.     % Remove empty cells and flatten
  103.     parsedConditions = parsedConditions(~cellfun('isempty', parsedConditions));
  104.  
  105.     % Combine all conditions into a single expression
  106.     if ~isempty(parsedConditions)
  107.         parsedConditions = [parsedConditions{:}];
  108.     else
  109.         parsedConditions = [];
  110.     end
  111.  
  112.  
  113. function parsedStr = parseUserInput(userInput)
  114.     % Replace y(5) with diff(y, x, 5)
  115.     parsedStr = regexprep(userInput, 'y\((\d+)\)', 'diff(y, x, $1)');
  116.     % Handle other derivatives and equality signs
  117.     parsedStr = regexprep(parsedStr, "y''''", "diff(y, x, 4)");
  118.     parsedStr = regexprep(parsedStr, "y'''", "diff(y, x, 3)");
  119.     parsedStr = regexprep(parsedStr, "y''", "diff(y, x, 2)");
  120.     parsedStr = regexprep(parsedStr, "y'", "diff(y, x)");
  121.     parsedStr = strrep(parsedStr, "=", "=="); % Ensure equality symbol is MATLAB compatible
  122.     parsedStr = strrep(parsedStr, 'y', 'y(x)');
  123.  
  124.  
  125.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement