77 views (last 30 days)

Show older comments

How do you obtain the jacobian of a symbolic function with respect to another symbolic function?

MathWorks Support Team
on 2 Sep 2021

Edited: MathWorks Support Team
on 8 Oct 2021

It is not possible to calculate the jacobian of a symbolic function w.r.t. another symbolic function directly. According to the "jacobian" documentation page here:

the second parameter of the Jacobian command is expected to be either a scalar or a vector of symbolic variables. Therefore it is not possible to directly take the Jacobian of a symbolic function w.r.t. another symbolic function. For example:

>> syms t a(t) x(t)>> jacobian(a(t)*x(t), x)

Error using sym/jacobian (line 44)

Second argument must be a vector of variables.

As you can see, "x" is a symbolic function rather than a symbolic variable and results in an error during jacobian calculation mentioned above.

As a workaround, you will have to:

1. Perform a substitution of all symbolic functions by symbolic variables,

2. Calculate the Jacobian w.r.t to the symbolic variables

3. Perform a substitution of the Jacobian with all symbolic variables being replaced by the symbolic functions

As a more comprehensive example, consider the jacobian of:

a(t) * x(t)^2 , w.r.t x(t)

You can calculate the jacobian w.r.t x(t) using the following commands

>> syms t a(t) x(t) x_var

>> substituted = subs(a(t)*x(t)^2, x, x_var);

>> jac = jacobian(substituted, [x_var]);

>> subs(jac, x_var, x)

ans = 2*a(t)*x(t)

More information on 'subs' can be found here:

Note that in the general case, this kind of substitution into variables and manipulating the variables and substituting back, is not valid. It is only valid if the functions involved are independent of each other, but in the general case you cannot know that to be true.

Dan Hendrickson
on 27 May 2020

Walter,

Thanks for the note, I have updated the MathWorks answer with your comment.

Best,

Dan

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!