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 the Jacobian matrix of $f$ at the point $(2,3,4)$, you can do the substitution
In[5]:= J[{x1,x2,x3}] /. Thread[{x1,x2,x3}->{2,3,4}]
Out[5]= {{3,2,0},{0,4,3},{4,0,2}}
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}
variableVector[n_] := ToExpression[Table["x" ~~ ToString[k], {k, 1, n}]];
Now we can write a function which computes the Jacobian at a given point as follows:
Jvalue[{Y__}] := With[
{X=variableVector[Length[{Y}]]},
J[X] /. Thread[X->{Y}]
]
In[9]:= Jvalue[{2,3,4}]
Out[9]= {{3,2,0},{0,4,3},{4,0,2}}
Update
Here is a a function which produces the Jacobian matrix at a given vector:
sparse[v_] := (
Clear[x];
xVariables = Map[x, Range[10000]];
Evaluate[xVariables] = v;
SparseArray[{{10000, 10000} -> x[1], {10000, 1} -> x[10000], {i_, i_} -> x[i + 1], ({i_, j_} /; j - i == 1) -> x[i]}, {10000, 10000}]
)
Now you should be able to use it in a loop:
In[2]:= sparseTest1=sparse[RandomInteger[{1,20},10000]];
sparseTest2=sparse[RandomInteger[{1,20},10000]];
In[3]:= sparseTest1[[1,2]]
Out[3]= 11
In[4]:= sparseTest2[[1,2]]
Out[4]= 8
J[X] = D[f[X], {X}];
to do? $\endgroup$J[Y0]
does not evaluate. $\endgroup$