4

Here is my Matlab program:

syms x(t) t;
f = x^2 + 5;

And the result of f:

f(t) = x(t)^2 + 5

Both f and x are of class symfun in Matlab. I want the result of df/dx, which is equal to 2*x(t). I tried this in Matlab:

diff(f, x);

and got the following errors:

Error using mupadmex
Error in MuPAD command: The variable is
invalid. [stdlib::diff]
Error in sym/diff (line 57)
    R = mupadmex('symobj::diff', S.s, x.s,
    int2str(n));

How can df/dx be obtained in Matlab?

horchler
  • 18,384
  • 4
  • 37
  • 73
Saking
  • 100
  • 1
  • 5

1 Answers1

6

In newer versions of Matlab (I'm using R2014b) the error message is clearer:

Error using sym/diff (line 26) All arguments, except for the first one, must not be symbolic functions.

So, it looks like sym/diff cannot take a derivative with respect to what the documentation calls an abstract or arbitrary symfun, i.e., one without a definition. This limitation is not explicitly mentioned in the documentation, but the various input patterns allude to it.


Workaround 1: Simple functions, independent variable t only appears in differentiated symfun

I haven't found any workaround that is particularly elegant. If t only appears in your variable of interest (here x(t)), and not on its own or in other abstract symbolic functions, you can differentiate with respect to time t and then cancel out the extra terms. For example:

syms t x(t) y(t) z
f1 = x^2+5;
f2 = x^2+z^2+5;
f3 = x^2+y^2+z^2+5+t;
dxdt = diff(x,t);
df1 = diff(f1,t)/dxdt
df2 = diff(f2,t)/dxdt
df3 = diff(f3,t)/dxdt

which returns the requisite 2*x(t) for the first two cases, but not the third, for the reasons stated above. You may need to apply simplify in some cases to fully divide out the D(x)(t) terms.


Workaround 2: More robust, but more complicated method

Using subs multiple times, you can replace the symbolic function in question with a standard symbolic variable, differentiate, and swap it back. For example:

syms t x(t) y(t) z
f3 = x^2+y^2+z^2+5+t;
xx = sym('x');
df3 = subs(diff(subs(f3,x,xx),xx),xx,x)

which also returns 2*x(t) for the third case above. But I think this is kind of an ugly hack.

It is kind of ridiculous that Matlab can't do this – Mathematica has no such problem. It appears that MuPAD itself has this limitation. You might consider filing a feature request with The MathWorks.

horchler
  • 18,384
  • 4
  • 37
  • 73
  • 1.The method by using `df = diff(f,t)diff(x,t)` is just suitable for calculating the function `f` of just variable `x(t)`. In the second example mentioned, where `f = x(t)^2+y(t)^2+5+t`, the `f` is the function of `x(t)`,`y(t)` and `t`, the result is not the one I want by using `df = diff(f,t)diff(x,t)`, so it's limited and not feasible for all possible functions. 2.I tried the second method of using `subs`, just as you said, it's kind of an ugly hack. However, it is a feasible way of addressing my problems.@horchler – Saking Nov 23 '14 at 09:31
  • @Saking: Yes, that is what I tried to say and is why I provided the second workaround. I've reworded the answer a bit and added a few more examples. Hopefully it will be a bit clearer. – horchler Nov 24 '14 at 23:34