MATLAB Answers

How can I differentiate an abstract symbolic function?

1 view (last 30 days)
c2332
c2332 on 4 Aug 2021
Commented: Star Strider on 4 Aug 2021
Hi, I would like to differentiate an abstract symbolic function in MATLAB.
To make sure we're all on the same page, let's say I have a function of two variables what's this where f is any function of one variable that is differentiable across its domain. Using partial differentiation, we should expect to see:
and
I've tried to use syms to observe this behaviour in MATLAB:
syms x y f(k) g(x,y)
g(x,y) = f(x - 3*y);
g_x = diff(g,x);
g_y = diff(g,y);
In effect, we find that
g_y = -3*D(f)(x - 3*y)
Which translates to after using the latex function.
But we also find that
g_x = diff(f(x - 3*y), x)
which translates to .
I have tried to use the expand and simplify functions on this result, but they don't seem to transform it into the expected . Is there a way for me to do this?
Note that if the coefficient of x was anything other than 1 or 0, then we have
g_x = c*D(f)(c*x - 3*y)

Answers (2)

Star Strider
Star Strider on 4 Aug 2021
I am not certrain that I understand what you want to do.
See if the diff documentation section on Differentiation With Respect to Function and Derivative will do what you want. It requires a slightly different syntax.
.
  2 Comments
Star Strider
Star Strider on 4 Aug 2021
I do not understand what you want to do. It appears that you want a specific format, and that format may not be available, unless you create it yourself.
.

Sign in to comment.


Paul
Paul on 4 Aug 2021
When I run this code (2019a) on my computer, I get:
syms x y f(k) g(x,y)
g(x,y) = f(x - 3*y);
g_x = diff(g,x)
g_y = diff(g,y)
g_x(x, y) =
D(f)(x - 3*y)
g_y(x, y) =
-3*D(f)(x - 3*y)
Is that not the form of the expected result? I'm confused if what you're asking about is the form of the result as sym objects, or the form of the typeset output from the Live Editor. If the latter, in my local version of Matlab I do get , although that desn't seem to be the case in 2021a, at least as run here:
g_x
g_x(x, y) = 
  4 Comments
Paul
Paul on 4 Aug 2021
Something changed since 2019a. I just ran in 2020b and got:
g_x(x, y) =
diff(f(x - 3*y), x)

Sign in to comment.

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!