Matlab change in symbolic notation of derivative

210 views Asked by At

I've recently upgraded Matlab from 2013b to 2019a (yes, I know, a lot has changed....but not software costs). I have some "legacy code" which makes heavy use of the symbolic tool box. One thing that seems to be causing a big issue is the change in notation. I think this question is best presented through example:

2013

>> F(x) = sym('F(x)');
>> subs(diff(F,x),x,1)

ans(x) =

D(F)(1)

2019

>> F(x) = str2sym('F(x)');
>> subs(diff(F,x),x,1)

ans(x) =

subs(diff(F(x), x), x, 1)

In 2019, F(x) = sym('F(x)'); would not work and I was told to switch to F(x) = str2sym('F(x)'); which seems to perform the operation as intended, but the notation is killing my code.

Is there a way to salvage the old notation or do I have to rewrite my code?

EDIT

Good comment.....how is this breaking my code? I use the symbolic tools to solve equations then parse them based on their structure. For example, if an expression has a third derivative, I would put that in group A. If it has a 7 derivative, I would put that in group B. In 2013, it was easy to parse for high derivatives...a third derivative would look like this D(D(D(F)))(x) in 2019 it looks like this diff(F(x), x, x, x). I've also found that 2019 mixes its notation. For example

F(x) = str2sym('F(x+dx)')
F(x) =

F(dx + x)

>> diff(F,x,3)

ans(x) =

D(D(D(F)))(dx + x)

At this point, I'm thinking this is just going to end up being one patch fix after another. Probably not the best idea to be parsing symbolic expressions if the notation is volatile. I was hoping there might be a "yeah, go to preferences and select..."

2

There are 2 answers

1
bg2b On BEST ANSWER

If adding something like the +dx makes 2019 switch notation to D, maybe you can just do something like that always and then substitute out the extra stuff? I don't have the symbolic toolbox so can't experiment, but just a thought.

Example

change F(x) = str2sym('F(x)'); to

F(x) = str2sym('F(x+dx)');

then use

subs(diff(F,x),x + dx,1)

which returns D(F)(1) and has the same meaning as subs(diff(F(x), x), x, 1)

0
Daniel On

(requries MATLAB 2019a or later)

Not an answer on how to restore the old syntax, but to the problem you described. Here is how I would solve it:

%some example input
syms x f(x)
g=diff(f,x,3)+8
%find the expression with `diff` and get its arguments
a=children(findSymType(g,'diff'))

This will return [ f(x), x, x, x], which means all you have to do is to throw away the first element and count the x