How to access "D" notation for symbolic derivatives

20 次查看(过去 30 天)
This question involves how to access certain dervatives of arbitrary (i.e., unknown) functions using the symbolic toolbox. This is an issue that arises when you try to take derivatives of a composite function, where both functions that make up the composite one are arbitrary/unknown. I'm using R2018b.
My question is best illustrated with an example:
syms f(x) g(x)
dgfx = diff(g(f(x)),x)
returns
dgfx =
D(g)(f(x))*diff(f(x), x)
This expression has two different notations for derivatives. diff(f(x), x) is just the representation of the derivative of f, which is fine. This is the format I expect, and I can "access" this derivative representation and, for example, replace it with a variable using subs, such as:
syms a
subs(dgfx,diff(f(x), x),a)
which returns
ans =
a*D(g)(f(x))
dgfx also includes the derivative notation D(g)(f(x)), which captures the derivative of the g function. The problem I'm having is that I cannot figure out how to access this representation in the same way as with the diff notation in order to replace this derivative with a variable. For example,
subs(dgfx,D,a)
subs(dgfx,D(g),a)
both return the error, "Undefined function or variable 'D'." So it seems neither D nor D(g) are functions I can access. However, Matlab does seem to recognize D as a function of some kind, since, for example, taking derivatives again:
diff(dgfx,x)
yields
ans =
D(D(g))(f(x))*diff(f(x), x)^2 + D(g)(f(x))*diff(f(x), x, x)
We now have a term involving D(D(g))(f(x)), which captures the second-order derivative of g. So clearly Matlab recognizes D(g) as a function whose derivative can be taken. The question I have is, how can I access this in a way that will allow me to replace D with a variable?

采纳的回答

Dana
Dana 2019-5-17
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).

更多回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by