TITLE: Apply chain rule and product rule on matrix differentiation
QUESTION [5 upvotes]: I have a question on taking the time derivative (i.e. w.r.t. t variable in the following function) of a quadratic function $V=x(t)^TPx(t)$. $t\in R^+$ has the physical meaning of time. $x(t)\in R^n$, $P\in R^{n\times n}$ is a positive definite matrix. x(t) has its own linear dynamics:$\dot{x}(t)=Ax(t),A\in R^{n\times n}$. I ran in to this problem when I was reading materials of Lyapunov function of linear systems, and from the materials I knew the answer should be $\dot{V}=x^T(A^TP+PA)x$.
The above correct answer can be obtained by applying the product rule:
$\dot{V}=\dot{x}^TPx+x^TP\dot{x}=(Ax)^TPx+x^TP(Ax)=x^T(A^TP+PA)x$.
But when I was deriving it, I use the chain rule, and got different answer:
$\dot{V}=\frac{\partial V}{\partial x}\dot{x}=\frac{\partial x^TPx}{\partial x}\dot{x}=x^T(P+P^T)(Ax)=x^T(PA+P^TA)x=2x^TPAx$, the last step was becasue of the symmetric property of $P$ being positive definite. 
Was there anything wrong I did with applying the chain rule? or should product rule being applied before chain rule? Thank you for your answer in advance!

REPLY [2 votes]: The two derivations agree.  
Note first that $\mathbf x^TM\mathbf x=\mathbf x^TM^T\mathbf x$ for any $M$ regardless of symmetry or definiteness. Applying the chain rule, $${dV\over dt}={dV\over d\mathbf x}{d\mathbf x\over dt}=\mathbf x^T(P+P^T)A\mathbf x=x^T(PA+P^TA)\mathbf x=\mathbf x^T(PA+A^TP)\mathbf x.$$ In the last step of your derivation, you replaced $P^T$ with $P$ instead of replacing $P^TA$ with $(P^TA)^T=A^TP$.  
Applying the product rule instead (which is a special case of the chain rule), $$\begin{align}{dV\over dt}&=\mathbf x^T{d(P\mathbf x)\over dt}+{d(\mathbf x^T)\over dt}P\mathbf x\\&=\mathbf x^TP{d\mathbf x\over dt}+\left({d\mathbf x\over dt}\right)^TP\mathbf x\\&=\mathbf x^TPA\mathbf x+(A\mathbf x)^TP\mathbf x\\&=\mathbf x^T(PA+A^TP)\mathbf x.\end{align}$$ Note that this derivation itself required two uses of the chain rule.  
If in addition $P$ is symmetric, then this expression can be further simplified to $2\mathbf x^TPA\mathbf x$, as you found.