Search code examples
matlabsymbolic-mathderivative

How to get symbolic partial derivative with respect to time


Let's say I have this function

f(t) = 4*sin(a(t)) + x(t)*y(t) + h + cos(y(t))*sin(x(t))

How would I compute its derivative with respect to time?


Solution

  • You need to declare the variables and the functions inside it as being symbolic and then use diff:

    clear
    clc
    
    syms a x y t h
    
    a(t) = symfun(sym('a(t)'), t)
    x(t) = symfun(sym('x(t)'), t)
    y(t) = symfun(sym('y(t)'), t)
    
    F = 4*sin(a(t)) + x(t)*y(t) + h + cos(y(t))*sin(x(t))
    
    DerF_t = diff(F,t)
    

    Giving the following (messy) output:

    F =   h + 4*sin(a(t)) + cos(y(t))*sin(x(t)) + x(t)*y(t)
    DerF_t =   x(t)*diff(y(t), t) + y(t)*diff(x(t), t) + 4*cos(a(t))*diff(a(t), t) + cos(x(t))*cos(y(t))*diff(x(t), t) - sin(x(t))*sin(y(t))*diff(y(t), t)
    

    Note that since a(t),x(t) and y(t) are simply defined as functions of 't' we are stuck with their 'symbolic' derivative (I don't know the term for that sorry)...i.e. diff(a(t)) for instance.

    Hope it's what you were after!