3

I have an equation for which I would like to compute the Jacobian symbolically.

$$f(x)=Ax-diag(x)(Ax+b)$$, where $x\in \mathbb{R}^n$, $A\in \mathbb{R}^{n\times n}$ and $b\in \mathbb{R}^n$.

I am new to Mathematica, and I have seen this relevant question, but I am not aware of how to automatically ensure Mathematica recognises the different dimensions. Can someone help me how to code that in Mathematica?

Bravo
  • 283
  • 1
  • 11

1 Answers1

4

Define the elements and the function f:

n=2;
aMat = Array[a, {n, n}];
xVec = Array[x, n];
bVec = Array[b, n];
f[xVec_] := aMat.xVec - DiagonalMatrix[xVec].(aMat.xVec + bVec);

The Jacobian is:

D[f[xVec], {xVec}]
MatrixForm[%]

enter image description here

It seems to work fine for fairly large values of n.

bill s
  • 68,936
  • 4
  • 101
  • 191
  • just to note the JacobianMatrix defined in the linked answer works fine with these defs JacobianMatrix[f[xVec], xVec] - now what would be really nice is to do this symbolically (i.e. w/o pre defining the matrix terms) – george2079 Jan 16 '14 at 15:40