Search code examples
matlabsymbolic-math

How to differentiate a function w.r.t another symbolic function in MATLAB?


Using the code,

syms x(t) 
y=x^2
diff(y,t)
diff(y,x)

I get the following error:

2*D(x)(t)*x(t)
Error using sym/diff (line 26)
All arguments, except for the first one, must not be symbolic functions.

Is there a way to tackle this? Thanks for your time.


Solution

  • I dont know much about the Symbolic Math Toolbox, but taking a derivative wrt to a function does not seem to be supported (at least in a direct fashion) for diff.

    You can substitute a variable, compute a derivative, and substitute the function back. Like so:

    syms z
    subs(diff(subs(y,x,z),z),z,x)
    
    ans(t) = 2*x(t)