Reputation: 2980
I'm running into an intriguing problem in MATLAB where I can't figure out a clean way to run the jacobian on a symbolic equation containing symbolic functions.
Let's say I have this equation for kinetic energy
The variables x
and phi
are symbolic functions which when differentiated give diff(x(t),t)
and diff(phi(t),t)
If I want to take the partial derivative of the coordinates (x_dot and phi_dot) like so
I could do that if the variables were given as symbolic variables, however, in my case they are given as symbolic functions such as
diff(x(t),t)
diff(phi(t),t)
I could use the subs()
function to substitute in symbolic variables but that can get messy quickly. Especially in this next step:
This would mean I have to re-substitute all those variables as functions so I can take the time derivative.
Any ideas on how I can easily derive these equations with the symbolic toolbox without lines and lines of code?
Upvotes: 0
Views: 304
Reputation: 11064
I'm afraid the only thing you can do is using subs
for this, but you can wrap it in a function like this:
function df = my_jacobian(f, x)
x_ = sym('a', size(x));
f_ = subs(f, x, x_);
df_ = jacobian(f_, x_);
df = subs(df_, x_, x);
end
Using this function you can calculate your jacobian similar to the example below:
syms x(t) y(t)
f = 2*diff(x(t), t) + 5*diff(y(t), t) + diff(x(t), t) * diff(y(t), t);
df = my_jacobian(f, [diff(x(t), t) diff(y(t), t)])
Upvotes: 1