Search code examples
matlabsymbolic-math

substitute the derivative of a function in a symbolic expression


I have the same question as here.

In Matlab the derivative of a function can be represented symbolically as

>> syms t
>> syms x(t)
>> diff(x,t)

ans(t) =

D(x)(t)

But how can I substitute in an expression if, say, I know the derivative.

>> subs(ans,D(x)(t),3)
Error: ()-indexing must appear last in an index expression.

Solution

  • Let's work through an example:

    syms t x(t) y
    f = x^2+y
    dfdt = diff(f,t) % returns 2*D(x)(t)*x(t)
    dxdt = diff(x,t) % returns D(x)(t)
    subs(dfdt,dxdt,3)
    

    which returns the 6*x(t). The key is that D(x)(t) is just the printed representation of the derivative with respect to time, not the actual value. You need to assign it to an actual variable. In your example you'd need to do what @rayryeng suggests, but it it's much more flexible and clearer if you assign names to your outputs.

    Both x and dxdt are what the help and documentation for symfun call an "abstract" or "arbitrary" symbolic function, i.e., one without a definition. These behave a bit differently from regular symbolic variables of type sym. Type class(x) or whos in your command window to see your variable types.