Search code examples
matlab

The subs function returns zero instead of a term


I was following this tutorial by the MathWorks

The part of the code that is the same as in the video behaves differently that expected. I'm trying to replace partial differential expressions in a simple equation with a symbolic expressions from the HOLDER_list. It seems to work for the first pair: THE_X and x, but when it comes to the second therm, diff(x,t) is substituted with 0 instead of THE_XD as I would expect.

Here is the code:

clear
syms m k b F
syms t x(t)
syms THE_X THE_XD THE_XDD
HOLDER_list = [THE_X,THE_XD,THE_XDD];
actual_list = [x, diff(x,t), diff(x,t,2)];

v =diff(x,t)
v_new = subs(v, actual_list, HOLDER_list)

Solution

  • There seems to be an issue with the ordering specified in the HOLDER_list and actual_list. Also, the variables appearing in the subs function must be in a cell array. This works for me:

    clear
    syms m k b F
    syms t x(t)
    syms THE_X THE_XD THE_XDD
    HOLDER_list = {THE_X,THE_XD,THE_XDD};
    actual_list = {x, diff(x,t), diff(x,t,2)};
    
    v = diff(x,t)
    v_new = subs(v, fliplr(actual_list), fliplr(HOLDER_list))