45
$\begingroup$

If we have two vectors, $a$ and $b$, how can I make Jacobian matrix automatically in Mathematica?

$$ a=\left( \begin{array}{c} x_1^3+2x_2^2 \\ 3x_1^4+7x_2 \end{array} \right);b=\left( \begin{array}{c} x_1 \\ x_2 \end{array} \right);J=\left( \begin{array}{cc} \frac{\partial \left(x_1^3+2x_2^2\right)}{\partial x_1} & \frac{\partial \left(x_1^3+2x_2^2\right)}{\partial x_2} \\ \frac{\partial \left(3x_1^4+7x_2\right)}{\partial x_1} & \frac{\partial \left(3x_1^4+7x_2\right)}{\partial x_2} \end{array} \right); $$

$\endgroup$

3 Answers 3

61
$\begingroup$

The easiest way to get the Jacobian is

D[a,{b}]

To get the format of a matrix, you would do MatrixForm[D[f, {x}], or D[f, {x}]//MatrixForm, as the comment by azdahak says.

There is no special matrix type in MMA - it's internally always stored as a list of lists.

Edit

Since this question is partly about the format of the matrix and its elements, I thought it's worth adding a definition that makes calculus output look prettier, and in the case of the Jacobian lets you write symbolic matrices like this:

$\left( \begin{array}{cc} \frac{\partial f_{\text{x}}}{\partial x} & \frac{\partial f_{\text{x}}}{\partial y} \\ \frac{\partial f_{\text{y}}}{\partial x} & \frac{\partial f_{\text{y}}}{\partial y} \\ \end{array} \right)$

The definition was initially posted as a comment on the Wolfram Blog:

Derivative /: 
 MakeBoxes[Derivative[α__][f1_][vars__Symbol], 
  TraditionalForm] := 
 Module[{bb, dd, sp}, 
  MakeBoxes[dd, _] ^= 
   If[Length[{α}] == 1, "\[DifferentialD]", "\[PartialD]"];
  MakeBoxes[sp, _] ^= "\[ThinSpace]";
  bb /: MakeBoxes[bb[x__], _] := RowBox[Map[ToBoxes[#] &, {x}]];
  FractionBox[ToBoxes[bb[dd^Plus[α], f1]], 
   ToBoxes[Apply[bb, 
     Riffle[Map[bb[dd, #] &, 
       Select[({vars}^{α}), (# =!= 1 &)]], sp]
   ]
  ]
 ]
]

With this, you can get the above matrix form with traditional partial derivatives like this:

First define the vector components with subscripts as is conventional. To avoid confusion between subscripts and variable names, use strings for the subscripts:

fVector = Array[Subscript[f, {"x", "y"}[[#]]][x, y] &, 2]

Then form the Jacobian and display it in TraditionalForm:

D[fVector, {{x, y}}] // MatrixForm // TraditionalForm

The result is as shown above.

Edit

In this answer to How to make traditional output for derivatives I posted a newer version of the derivative formatting that contains an InterpretationFunction which allows you to evaluate the derivatives despite their condensed displayed form.

$\endgroup$
5
  • $\begingroup$ but output is vector not Matrix? $\endgroup$ Commented May 19, 2012 at 14:34
  • $\begingroup$ How to print matrix J, to see output from Jarga? $\endgroup$ Commented May 19, 2012 at 14:35
  • 1
    $\begingroup$ Like this: D[{x^3+2y^2,3x^4+7y},{{x,y}}]//MatrixForm $\endgroup$
    – azdahak
    Commented May 19, 2012 at 14:40
  • $\begingroup$ Elegant solution. $\endgroup$
    – Jagra
    Commented May 19, 2012 at 15:29
  • 1
    $\begingroup$ this makes me so. happy. +1 $\endgroup$
    – Ghersic
    Commented Jun 8, 2013 at 6:02
25
$\begingroup$

See: Jacobian matrix

The Jacobian matrix and determinant can be computed using the Mathematica commands:

JacobianMatrix[f_List?VectorQ, x_List] :=
    Outer[D, f, x] /; Equal@@(Dimensions/@{f,x})

JacobianDeterminant[f_List?VectorQ, x_List] :=
    Det[JacobianMatrix[f, x]] /;
      Equal @@ (Dimensions /@ {f, x})

Some additional info.

a = {x1^3 + 2 x2^2, 3 x1^4 + 7 x2}
b = {x1, x2}
JacobianMatrix[a, b] // MatrixForm

output

Or maybe what you want looks like this:

 JacobianMatrix[b, a] // MatrixForm

enter image description here

$\endgroup$
4
  • $\begingroup$ it is not working as JacobianMatrix[a_List?VectorQ, b_List] := Outer[D, a, b] /; Equal @@ (Dimensions /@ {a, b}) $\endgroup$ Commented May 19, 2012 at 14:29
  • $\begingroup$ How to print matrix J, to see output? $\endgroup$ Commented May 19, 2012 at 14:31
  • $\begingroup$ it is not working as Needs["VectorAnalysis`"] a = ( { {x1^3 + 2 x2^2}, {3 x1^4 + 7 x2} } ); b = ( { {x1}, {x2} } ); JacobianMatrix[a, b] // MatrixForm $\endgroup$ Commented May 19, 2012 at 14:56
  • $\begingroup$ Your comment code, a = ( { {x1^3 + 2 x2^2}, {3 x1^4 + 7 x2} } ); b = ( { {x1}, {x2} } ); defines 2 matrixes. Placing the terms in vectors they should look like this: a ={x1^3 + 2 x2^2,3 x1^4 + 7 x2}; b = {x1, x2}; $\endgroup$
    – Jagra
    Commented May 19, 2012 at 15:03
22
$\begingroup$

Grad[a,b] also produces the Jacobian.

a = {x1^3 + 2 x2^2, 3 x1^4 + 7 x2};
b = {x1, x2};
Grad[a, b] // MatrixForm

This has the added advantage of letting you compute the Jacobian in different coordinate systems.

enter image description here

$\endgroup$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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