I seem to have found a solution to this problem on my own. I'll post it here in case it's of help to anyone else. The key discovery was that there is a MuPAD function called rewrite that will do the trick.
Suppose I want to replace the derivative of f with a and the derivative of g with b. So in the end, I'd like to have d[g(f(x))]/dx = a*b. Here's how you can do this:
syms f(x) g(x) a b
dgfx = diff(g(f(x)),x);
dgfx = subs(dgfx,diff(f(x),x),a); % replace f' with a
dgfx = subs(dgfx,f,x); % replace the symfun f with the symvar x
dgfx = feval(symengine,'rewrite',dgfx,'diff'); % replace D notation with diff
dgfx = subs(dgfx,diff(g(x),x),b);
The final result is
dgfx =
a*b
as desired.
A couple of notes:
- Importantly, while rewrite can also be called directly from the command line (i.e., without using feval with symengine), the command-line version is not able to perform the required task. In particular, 'diff' is only a valid argument when using the feval approach.
- The step of first replacing the symfun f with the symvar x before using rewrite is necessary in order to turn the derivative of the composite function (i.e., D(g)(f(x))) into the derivative of a non-composite function (i.e., D(g)(x)), so that it can be cast in diff(g(x),x) form (diff(g(f(x)),f) is not a valid Matlab expression, since you can't take a derivative w.r.t. a function).