MATLAB: Derivative of a symbolic function with multiple dependent variables

derivativeMATLABsymbolictime

Hello everybody. I'm facing this problem. I have a variable q(t) time dependent. I have to derive it in MatLab and I want to substitute numerical values to q(t), qd(t) but if I do that, as one can expect, the derivative of a number is 0 and then my function is constantly equal to 0. i write here an example:
syms q(t)
f = 2*q^2;
fd = diff(f,t)
subs(fd,q,5).
The result is 4*q(t)*diff(q(t), t).
So fd is function of q and qd and I want to substitute both with numerical numbers. Do you know any solution?

Best Answer

If you want to substitute values for q(t) and dq(t), you may fisrt substitute the value for dq(t) followed by the value for q(t). Let the value to be substituted for dq(t) be 2.
syms q(t)
f = 2*q^2;
fd = diff(f,t)
a = subs(fd,diff(q(t),t),2)
which gives an output
fd(t) = 4*q(t)*diff(q(t), t)
a(t) = 8*q(t)
Now, you may substitute the value of q(t) in a(t)
subs(a(t),q(t),5)
to get the ouput as
ans = 40
Related Question