Skip to main content
2 of 6
Stylistic changes, added method to generate vector of variables symbolically
Michael Wijaya
  • 2.2k
  • 16
  • 29

From what I understand, you want to compute the Jacobian matrix of a function $f$ at $(x_1,x_2,\ldots,x_n)$ and evaluate it at a given point, say $(2,2,\dots,2)$.

This seems to be the function you are interested in:

f[{X__}] := {X} RotateLeft[{X}, 1] - 1;

In[2]:= f[{x1,x2,x3}]
Out[2]= {-1+x1 x2,-1+x2 x3,-1+x1 x3}

Since the dimension may change, we want to use SetDelay or := so that the Jacobian function recomputes the matrix every time it is called.

J[{X__}] := D[f[{X}], {{X}}]

In[4]:= J[{x1,x2,x3}]
Out[4]= {{x2,x1,0},{0,x3,x2},{x3,0,x1}}

If you want to Jacobian matrix of $f$ at the point $(2,3,4)$, you can do the subsitution

In[5]:= J[{x1,x2,x3}] /. Thread[{x1,x2,x3}->{2,3,4}]
Out[5]= {{3,2,0},{0,4,3},{4,0,2}}

Of course you can also package this into a new function if you prefer.

I would also recommend that you avoid using subscripts. Here is one way to generate a vector of variables such as {x1,x2,x3,x4,x5} programatically:

In[6]:= ToExpression[Table["x"~~ToString[k],{k,1,5}]]
Out[6]= {x1,x2,x3,x4,x5}
Michael Wijaya
  • 2.2k
  • 16
  • 29