Effacer les filtres
Effacer les filtres

How to convert a differential equation str to a equation for dsolve to work with

3 vues (au cours des 30 derniers jours)
Carlos
Carlos le 2 Déc 2023
Commenté : John D'Errico le 3 Déc 2023
Im new to matlab and i am trying to make a differential equation solver, i have a textfield where i input the equation in, and i have tried str2sym but it doesnt seem to work, what am i doing wrong? should i try with another input form?. I tried with inputs: "dy/dt + 2*y == 0", "diff(y,t) + 2*y == 0" and none of them worked.
% Button pushed function: SolveButton
function SolveEDO(app, event)
syms t y(t)
% Get the differential equation from the text field
equation = str2sym(app.EquationField.Value);
try
% Solve the differential equation
solution = dsolve(equation);
% Display the solution in the label
app.ResultLabel.Text = char(solution);
catch
% Handle errors
app.ResultLabel.Text = 'Error: Unable to solve the differential equation.';
end
end

Réponses (2)

Dyuman Joshi
Dyuman Joshi le 2 Déc 2023
str2sym only evaluates the input provided, it does not account for symbolic variables/functions defined in the workspace, thus you need to specify the dependence of variables in the expression itself -
%Output for the inputs provided earlier
eq = str2sym("diff(y,t) + 2*y == 0" )
eq1 = 
%Output for the modified input
eq_new = str2sym("diff(y(t),t) + 2*y(t) == 0" )
eq_new = 
  2 commentaires
Carlos
Carlos le 3 Déc 2023
So there is no way of doing this with a textfield?
Dyuman Joshi
Dyuman Joshi le 3 Déc 2023
What I meant was that you will have to provide the input as shown in the 2nd example above.

Connectez-vous pour commenter.


Walter Roberson
Walter Roberson le 3 Déc 2023
So there is no way of doing this with a textfield?
No, there is not. The problem is not the uitextarea or uieditfield: the problem is that str2sym() does not know that you want it to deduce contextually that "diff(y,t)" is intended to imply that y is a function of t that needs to be internally rewritten as diff(y(t),t) .
Like, how would that work? Is there a list of letters that it should automatically transform as being functions, but leaving other ones alone? Like diff(f,t) or diff(h,t) should be assumed to imply f(t) and g(t), but diff(c,t) should be assumed that c is a constant because using "c" as a constant is probably more common than using c(t) even though c(t) does certainly occur as well?
If you want to force "diff(y,t)" to be treated as "diff(y(t),t)" when converting from character vector or string scalar into symbolic, then at present you need to preprocess the user input before you ask to convert it to symbolic.
Question:
If the user had entered "diff(y) - diff(y,t)^2 + 2*y == 0" then is it safe to assume that y is a univarate function in t? What if it is intended as y(u,t) in which case diff(y) is implied to be diff(y(u,t),u) ? What if it is intended as y(x) and diff(y) is intended as diff(y(x),x) with the diff(y,t) intended to resolve to 0 because y(x) is constant in t?
Guessing user intentions is pretty risky.
  1 commentaire
John D'Errico
John D'Errico le 3 Déc 2023
Is there no way... ?
Well, @Carlos could write a parser code, that would take the string and try to figure out the function name, and variable name, in this case y and t. There could be some rules, that y is always the name of a function, that t is always a independent variable name. Then the code would modify the string on the fly. It would be a miserable code to write, and one would expect bugs by the boatload.
Far better to learn what syms expects, and just follow that set of rules.

Connectez-vous pour commenter.

Produits


Version

R2023b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by