How to partial derive a matrix with multiple variables

Hello everybody,
I just have some trouble by finding the partial derivative of a matrix with multiple variables.
Let's say I have a [nXn]-matrix (i.e ), which is a function of three time dependent variables (i.e. ) and I need to find the partial derivative , where q is the vector .
Moreover, I need to find the time derivative .
I know this question might be a very simple one but I just find examples, which show how to build the partial drivative of only one variable as diff(Mgb,Z_A) and not for multiple time dependent variables.
It would be very greatful if somebody could give me a comment on this topic.
Many thanks in advance!

 Accepted Answer

If your matrix is symbolic, see if the jacobian (link) function will do what you want. The gradient (link) function is also an option, as is the gradient (link) function for numeric vectors and matrices.

4 Comments

Hey Star Strider,
many thanks for your answer. Yes, my first idea was to find the analytic solution using the symbolic toolbox. By using the jacobian function I get the error message below. I just created a simple example, where M is a [3x3] matrix. Maybe, the issue becones a little bit more clear.
syms t a(t) b(t) c(t) d(t)
M = [sin(a)+b, b^2*c/d, cos(a)-c; c^3*a, a-c, 25; 18*c, d^2-b, a^2]
M(t) =
[ sin(a(t)) + b(t), (b(t)^2*c(t))/d(t), cos(a(t)) - c(t)]
[ a(t)*c(t)^3, a(t) - c(t), 25]
[ 18*c(t), d(t)^2 - b(t), a(t)^2]
q = [a; b]
q(t) =
a(t)
b(t)
dM_dq = diff(M,q)
Error using sym/diff (line 26)
Arguments, except for the first, must not be symbolic functions.
dM_dq = jacobian(M,q)
Error using sym/jacobian (line 26)
First argument must be scalar or vector.
I need to use this to solve an ode, where , and needs to be updated every time step. So, to make a numerical calculation of and using the gradient function could also ab an option, but at the moment I have no idea, how I could do this.
My pleasure.
If you have R2015a or later, you need to use the functionalDerivative (link) function, not diff or jacobian.
It did not like the matrix, and I could not address individual elements of it, so I resorted to this:
syms t a(t) b(t) c(t) d(t)
% M = [sin(a)+b, b^2*c/d, cos(a)-c; c^3*a, a-c, 25; 18*c, d^2-b, a^2];
M(1,1) = sin(a)+b;
M(1,2) = b^2*c/d;
M(1,3) = cos(a)-c;
M(2,1) = c^3*a;
M(2,2) = a-c;
M(2,3) = 25;
M(3,1) = 18*c;
M(3,2) = d^2-b;
M(3,3) = a^2;
q = [a, b];
for k1 = 1:size(M,1)
for k2 = 1:size(M,2)
dM_dq{k1,k2} = functionalDerivative(M(k1,k2),[a,b]);
end
end
That was successful, however introduced the complication of creating a cell array. To address the elements of the cell array, do something like this:
dMdq33 = dM_dq{3,3}
producing:
dMdq33(t) =
2*a(t)
0
That is the best the Symbolic Math Toolbox (and I) can do. You will need to reconstruct the matrix of the derivatives.
If you are using one of the numerical ODE solvers such as ode45, the output of your ODE function has to be a column vector, so you would need to multiply your matrix by a (3x1) vector inside the function. In that event, you might not need to symbolic derivative calculations.
Hey,
right, this looks good for me. Many thanks again for giving me your support. I will check this later and will give feedback afterwards.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!