I have a function f(x(t))=sin(x(t))
that I would like to differentiate in MATLAB sometimes with respect to t
, sometimes with respect to x(t)
. In MATLAB, I enter:
>> syms x(t);
>> f=sin(x(t));
>> diff(f,t)
ans =
cos(x(t))*diff(x(t), t)
However when I differentiate with respect to x(t)
I get:
>> diff(f,x)
Error using sym/diff (line 26)
All arguments, except for the first one, must not be symbolic functions.
A workaround is to do:
>> syms temp;
>> subs(diff(subs(f,{x},{temp}),temp),{temp},{x})
ans =
cos(x(t))
However for large functions that I actually deal with in my code, subs
is very slow - it's the bottleneck in my code. Surely there is a way to directly do diff(f,x)
?! I mean the developers at MathWorks can't have just left such a huge tail hanging, right?
I really appreciate your help. Thank you!