Derivative of angle between two vectors singularity! I have been battling a problem of needing to know the derivative of the angle between two vectors, the vectors possibly being parallel at some points in time. I started off with:
$$\bf A \dot \bf B = \|A\|\|B\|\cos\theta \Rightarrow \theta=\arccos \left( \frac{\bf A \dot \bf B}{\|A\|\|B\|} \right)$$
Let's say that $\bf A$ and $\bf B$ are unit vectors for simplicity so:
$$
\theta=\arccos \left( \bf \hat A \dot \bf \hat B \right)
$$
To find the derivative of the angle $\theta$:
$$
\dot \theta = \frac{-1}{\sqrt{1-\left(\bf \hat A \dot \bf \hat B\right)^2}} \left[\bf \dot{\hat A}\dot \bf \hat B+\bf {\hat A}\dot \bf \dot{\hat B}\right]
$$
But, $\bf{\hat A} \dot \bf{\hat B}=$$1$ when $\bf \hat A \,\,\|\,\, \bf \hat B$ and the denominator of the expression for $\dot \theta$ becomes $0$. We hence get a division by $0$. Now I have tried other ways - by projecting $\bf{\hat B}$ onto $\bf{\hat A}$ and taking $\arctan$, by projecting $\bf{\hat B}$ onto a plane and forming a right-angle triangle and again taking $\arctan$. Every method I tried leads to the same singularity when $\bf \hat A \,\,\|\,\, \bf \hat B$.
Question: what method can I use such that I don't get a singularity for $\dot\theta$ when $\bf{\hat A}$ becomes parallel to $\bf{\hat B}$? Thank you!
 A: You cannot get rid of the singularity. Note that in ${\mathbb R}^d$ with $d\geq3$ the angle between two vectors ${\bf a}$, ${\bf b}$ is nonoriented, implying that we always have $$\angle({\bf a},{\bf b})\in[0,\pi]\ .$$
Consider the following example in ${\mathbb R}^3$:
$${\bf a}(t):=(1,0,0),\quad{\bf b}(t):=(\cos t,\sin t,0)\qquad(-1<t<1)\ .$$
Then
$$\phi(t):=\angle\bigl({\bf a}(t),{\bf b}(t)\bigr)=|t|\ ,$$
and this is not differentiable at $t=0$.
A: Consider the function $F:\mathbb R^n \setminus\{0\} \to \mathbb S_{n-1}$, defined by $F(u) := u/\|u\|$. So you seek $\dot\theta(t)$, where $\theta(t):= \arccos(r(t))$, and $r(t): = a(t)\cdot b(t)$, where $a(t):= F(x(t))$ and $b(t) := F(y(t))$
Now, by chain rule, $\dot{r}(t) = a(t).\dot{b}(t) + \dot{a}(t)\cdot b(t)$. Using a algebraic fact (see lemma below) and the chain rule again, we get
$$
\begin{split}
\dot{a}(t) &= \nabla F(x(t))\cdot\dot{x}(t) = \frac{I_n-F(x(t))\otimes F(x(t))}{\|x(t)\|}F(y(t)),\\
\dot{b}(t) &= \nabla F(y(t))\cdot\dot{y}(t) = \frac{I_n-F(y(t))\otimes F(y(t))}{\|y(t)\|}F(x(t)),
\end{split}
$$
where $\otimes$ denotes the outer-product of vectors (produces a rank-1 matrix) and $I$ is the identity matrix of size $n$.
Putting everything together should give you the correct formula for $\dot{r}(t)$. Now by using the chain rule one last time to get $\dot{\theta} = -\dfrac{1}{\sqrt{1-r(t)^2}}\dot{r}(t)$.

Lemma. If $u,v \in \mathbb R^n$ with $u \ne 0$, then $\nabla_u (F(u)\cdot v) = \dfrac{I_n-F(u)\otimes F(u)}{\|u\|}v$.

Proof. Chain rule and basic matrix calculus.
