Suppose I have three constant symmetric matrix $\mathbf{M}_{n\times n}$, $\mathbf{C}_{n\times n}$ and $\mathbf{D}_{n\times n}$ and two variable vectors $\mathbf{q}_{n\times 1}$ and $\mathbf{p}_{n\times 1}$. Also I have two function. The first is a matrix that depends only on $\mathbf{q}_{n\times 1}$:
$\mathbf{G}_{n\times n}(\mathbf{q})=\mathbf{M}+\mathbf{C}(\mathbf{q}^T\mathbf{D}\mathbf{q})$
and the second is a scalar function that depends on both $\mathbf{q}_{n\times 1}$ and $\mathbf{p}_{n\times 1}$:
$J(\mathbf{q},\mathbf{p})=\mathbf{p}^T\mathbf{G}^{-1}\mathbf{p}$
I want to find an expression for $\frac{\partial J}{\partial \mathbf{q}}$ in terms of $\mathbf{M}$, $\mathbf{C}$, $\mathbf{D}$, $\mathbf{G}^{-1}$, $\mathbf{q}$ and $\mathbf{p}$.
If I use the formula $d(A^{-1})=-A^{-1}(dA)A^{-1}$, the result will be:
$$\frac{\partial J}{\partial \mathbf{q}}=-\mathbf{p}^T\mathbf{G}^{-1}\frac{\partial \mathbf{G}}{\partial \mathbf{q}}\mathbf{G}^{-1}\mathbf{p}$$
but I can't compute $\frac{\partial \mathbf{G}}{\partial \mathbf{q}}$. Is there another way to solve this problem?
We can salvage your result, if you revert to the differential for a moment: $$ dJ = - p^TG^{-1}dG\,G^{-1}p $$ and expand $dG = C d(q^TDq) = 2C(q^TDdq)$. The term in parentheses is a scalar and doesn't participate in the matrix product, i.e. $$ dJ = -2 p^TG^{-1}C\,G^{-1}p\,\,(q^TDdq) $$ Now we can identify the derivative wrt $q$ as $$ \frac {\partial J} {\partial q} = -2 p^TG^{-1}C\,G^{-1}p\,\,(q^TD) $$