Great Answers to
Questions About Everything


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

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

{ asked by George Mills }


The easiest way to get the Jacobian is


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.


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 /: 
  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]], 
     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.

{ answered by Jens }