Hello!
I have a string input that i transform into a symbolic equation. I need to translate "D(___)" into differentiation, but i can't make it work
syms t x x(t) D(x)
eq_str = 'D(x)'; % input string equation
eq_sym = str2sym(eq_str); % transform equation to symbolic
eq_sym2 = subs(eq_sym,x,x(t)) % make x into a function of time
Dif = @(f) diff(f,t) % make Dif into a function handle
subs(eq_sym2,D,Dif) % returns 0, i want "diff(x(t),t)"
I dont think that "subs" is the correct way to do it, but i have not been able to find any other way
Thanks!

 Akzeptierte Antwort

Walter Roberson
Walter Roberson am 10 Mai 2020

0 Stimmen

syms t x(t) D(x) x1(t) x2(t)
eq_str = '0 == x1 + D(x2)'; % example of input string equation
eq_sym = subs(str2sym(eq_str)); % transform equation to symbolic
eq_str2 = '0 == x1 + D(D(x2))'; % example of input string equation
eq_sym2 = subs(str2sym(eq_str2)); % transform equation to symbolic
eq_sym = mapSymType(eq_sym, 'D_Var', @fixD);
disp(eq_sym);
eq_sym2 = mapSymType(eq_sym2, 'D_Var', @fixD);
disp(eq_sym2)
function in = fixD(in)
syms t
if hasSymType(in, 'D_Var')
in = diff(mapSymType(children(in), 'D_Var', @fixD),t);
end
end

Weitere Antworten (1)

Steven Lord
Steven Lord am 9 Mai 2020

1 Stimme

>> syms x t x(t)
>> dxdt = diff(x, t)
dxdt(t) =
diff(x(t), t)
To substitute a function in for x(t):
>> subs(dxdt, x, sin(t))
ans(t) =
cos(t)

7 Kommentare

William Alberg
William Alberg am 10 Mai 2020
Bearbeitet: William Alberg am 10 Mai 2020
Thank you for your response!
eq_str comes from a file, and i have no control over the format.
I still tried to apply your solution, and i found i could come closer with a small change, but now the result is a bit weird. It writes " diff(x2(t)(t), t)"
syms t x(t) D(x) x1 x1(t) x2 x2(t)
eq_str = '0 == x1 + D(x2)'; % example of input string equation
eq_sym = str2sym(eq_str); % transform equation to symbolic
eq_sym = subs(eq_sym,x2,x2(t)); % make x into a function of time
eq_sym = subs(eq_sym,x1,x1(t)); % make x1 into a function of time
% Dif = @(f) diff(f,t); % make Dif into a function handle
dxdt = diff(x(t), t)
subs(eq_sym,D,dxdt) % returns "0 == diff(x2(t)(t), t) + x1(t)", i want "0 == diff(x2(t), t) + x1(t)"
I also changed eq_str to something more realistic
I hope you can help me again :)
syms t x(t) D(x) x1(t) x2(t)
eq_str = '0 == x1 + D(x2)'; % example of input string equation
eq_sym = str2sym(eq_str); % transform equation to symbolic
subs( subs(eq_sym, 'D', 'diff') )
This will result in 0 == diff(x2(t)) + x1(t) which is not exactly what was asked for. The additional step of forcing the derivative to be with respect to t is much more tricky.
William Alberg
William Alberg am 10 Mai 2020
Hello Walter, thank you for responding!
It appers this replaces "D" with a function named "diff", and not derivative. (The difference can be seen if you use the latex command)
syms t x(t) D(x) x1(t) x2(t)
eq_str = '0 == x1 + D(x2)'; % example of input string equation
eq_sym = str2sym(eq_str); % transform equation to symbolic
subs(mapSymType(eq_sym, 'D_Var', @(e) diff(subs(children(e)),t)))
It's a trick!
str2sym() tries to avoid mapping 'D' to the derivative function, by mapping D to D_Var. You have to know to look for that specifically.
William Alberg
William Alberg am 10 Mai 2020
Thank you so much!
I have a few followup questions, that i hope you can help me with :)
  1. Where did you found out about the 'D_Var' mapping?
  2. Any idea of what i can do if: eq_str = '0 == x1 + D(D(x2))'
  3. How do i mark your comment as the "accepted answer"
ch1 = children(eq_sym);
ch2 = children(eq_sym(2));
symFunType(ch2(2))
ch2(2) displays as D(x2) but symFunType shows that it is D_Var that is the function.
Walter Roberson
Walter Roberson am 10 Mai 2020
I am running into some difficulties in generalizing the solution. Something like this cries out for a recursive function, but you have to be able to do the subs() at arbitrary nesting level then, which is a problem because the subs() has to be done in the current workspace...

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Symbolic Math Toolbox finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by