Question

I'm trying to derive Lagrangian equations of motion in Matlab using the symbolic toolbox. This involves partial derivatives of a function and your coordinates, but matlab seems to not accept this.

So I would do this in Matlab:

syms t x(t)             % t: time, x(t) position dependent on time
m = sym('m');           % mass, a constant parameter
T = m/2*diff(x,t)^2;    % kinetic energy
dTdx = diff(T,x);
ddTdxDotdt = diff( diff(T,diff(x,t)), t);

But as soon as I try to differentiate anything in x (or diff(x,t)), Matlab complains:

Error using mupadmex
Error in MuPAD command: The variable is invalid. [stdlib::diff]

Error in sym/diff (line 44)
R = mupadmex('symobj::diff', S.s, x.s, int2str(n));

Does anyone know the proper way of handling this?

Était-ce utile?

La solution

Matlab ought to be able to do this as you have it written, but I think that it doesn't like taking derivatives with respect to a symfun. Type whos in the command window and you'll see that x is listed as a symfun while t is just a sym. The help for diff kind of indicates this limitation. It won't event try to take the derivative of a constant with respect to x(t): diff(1,x) "complains" just the same. Unless newer versions of Matlab fix this (I'm on R2012b) I think you only option may be to come up with a scheme using two instances of x.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top