May I suggest you check out the Matrix Cookbook by Peterson and Pedersen (available free online - just Google). An analytical solution to your problem is p39, Equation 325 (2008 edition).
We donβt even need Matlab for this!
EDIT: As follows from YBE, perhaps I should include the solution in my answer. So, let p (x) denote a multidimensional Gaussian pdf characterized by the mean vector m and the covariance matrix S. Then:
dp (x) / dx = -p (x) * S ^ (- 1) * (x - m)
and
d ^ 2p / dx dx '= p (x) * (S ^ (- 1) (x - m) (x - m)' S ^ (- 1) - S ^ (- 1))
If you want the Matlab function, then:
function Gradient = MultNormD1(x, Mu, Sigma) Gradient = -1 * mvnpdf(x, Mu, Sigma) * (Sigma \ (x - Mu));
source share