0

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?

Leo
  • 45
  • 1
  • 6

2 Answers2

5

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]
bill s
  • 68,936
  • 4
  • 101
  • 191
  • Thanks! As you can tell, I'm new to Mathematica :) – Leo Feb 18 '17 at 00: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 00:49
3

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.

Itai Seggev
  • 14,113
  • 60
  • 84