# Computing Jacobian matrix

I am trying to compute the Jacobian matrix J. In my case, f and x are 3-vectors (my vectors are named g and a instead), so J should be a 3X3 matrix.

Here's the code I'm using:

a = {{a1}, {a2}, {a3}} // MatrixForm
g = {{Subscript[l, 1]*Cos[a1] + Subscript[l, 2]*Cos[a1 + a2] +
Subscript[l, 3]*Cos[a1 + a2 + a3]}, {Subscript[l, 1]*Sin[a1] +
Subscript[l, 2]*Sin[a1 + a2] + Subscript[l, 3]*Sin[a1 + a2 + a3]},
{a1 + a2 + a3}   } //   MatrixForm
J = D[g, a] // MatrixForm


This is the output I get in Mathematica: Based on the suggestions from another discussion, I also tried

J = D[g, {a}]


and

J = JacobianMatrix[g, a]


but neither worked. Can someone tell me why I'm not getting the correct Jacobian matrix, and share code for the correct method?

• MatrixForm is a display command. Remove it from your defintions of a and g. – bill s Feb 18 '17 at 0:20
• Ok, I did that, but I still don't get the answer. – Bourbaki Feb 18 '17 at 0:24

Drop all the extra { }'s, remove the MatrixForm:

a = {a1, a2, a3};
g = {l1 Cos[a1] + l2 Cos[a1 + a2] + l3 Cos[a1 + a2 + a3],
l1 Sin[a1] + l2 Sin[a1 + a2] + l3 Sin[a1 + a2 + a3], a1 + a2 + a3};
j = D[g, {a}];
MatrixForm[j]

• Thanks! As you can tell, I'm new to Mathematica :) – Bourbaki Feb 18 '17 at 0:33
• Mathematica doesn't have a notion of row or column, so all the { } were just confusing. Also, the syntax of D[ ] requires the brackets around the second argument (or else it is interpreted as multiple derivatives). Good luck! – bill s Feb 18 '17 at 0:49

The problem isn't with MatrixForm as such but where you're placing it. You are assigning a value to a and g containing a MatrixForm, rather than displaying the MatrixForm of an assignment. So the following would work

(a = {a1, a2, a3}) // MatrixForm
(g = {l1 Cos[a1] + l2 Cos[a1 +  a2] + l3 Cos[a1 + a2 + a3],
l1 Sin[a1] + l2 Sin[a1 + a2] + l3 Sin[a1 + a2 + a3],
a1 + a2 + a3}) // MatrixForm
(j = Grad[g, a]) // MatrixForm


Also note the use above of the a built-in Grad command, that supports arbitrary rank arrays.