Search code examples
matlabsymbolic-mathdifferentiation

Can I change the formula of a symbolic function in MATLAB?


I have the following code:

syms t x;
e=symfun(x-t,[x,t]);

In the problem I want to solve x is a function of t but I only know its value at the given t,so I modeled it here as a variable.I want to differentiate e with respect to time without "losing" x,so that I can then substitute it with x'(t) which is known to me. In another question of mine here,someone suggested that I write the following:

e=symfun(exp(t)-t,[t]);

and after the differentiation check if I can substitute exp(t) with the value of x'(t).

Is this possible?Is there any other neater way?


Solution

  • I'm really not sure I understand what you're asking (and I didn't understand your other question either), but here's an attempt.

    Since, x is a function of time, let's make that explicit by making it what the help and documentation for symfun calls an "abstract" or "arbitrary" symbolic function, i.e., one without a definition. In Matlab R2014b:

    syms t x(t);
    e = symfun(x-t,t)
    

    which returns

    e(t) =
    
    x(t) - t
    

    Taking the derivative of the symfun function e with respect to time:

    edot = diff(e,t)
    

    returns

    edot(t) =
    
    D(x)(t) - 1
    

    the expression for edot(t) is a function of the derivative of x with respect to time:

    xdot = diff(x,t)
    

    which is the abstract symfun:

    xdot(t) =
    
    D(x)(t)
    

    Now, I think you want to be able to substitute a specific value for xdot (xdot_given) into e(t) for t at t_given. You should be able to do this just using subs, e.g., something like this:

    sums t_given xdot_given;
    edot_t_given = subs(edot,{t,xdot},{t_given, xdot_given});
    

    You may not need to substitute t if the only parts of edot that are a function of time are the xdot parts.