0
$\begingroup$

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: enter image description here

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?

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

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]
$\endgroup$
  • $\begingroup$ Thanks! As you can tell, I'm new to Mathematica :) $\endgroup$ – Bourbaki Feb 18 '17 at 0:33
  • $\begingroup$ 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! $\endgroup$ – bill s Feb 18 '17 at 0:49
2
$\begingroup$

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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.