Search code examples
matlabmupad

Matlab change in symbolic notation of derivative


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..."


Solution

  • 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)