So I have two sets of points, $P$ and $Q$, in the 3D Euclidian space. Set $P$ has $N$ points and set $Q$ has $M$ points. It is possible that $N=M$ but not necessarily. Now I define quantity $S$ as such:
$$ S = \sum_{i}^{N} \sum_j^{M} |\vec{x}_i-\vec{x}_j|^2 $$
By rotating $Q$ around a fixed axis by and angle $\theta$ as $P$ remains stationary I want to find the angle $\theta_0$ that maximizes $S$.
Question: Is there a closed-form solution to the correct rotation matrix in terms of $\theta_0$?
I'm assuming you are given the axis of the rotation, say along unit vector $u$, and are looking for the optimal angle $\theta_0$ as a function of $u$.
I'm going to change your notation a little bit, so that I don't get confused between the points in $P$ and those in $Q$. Let $$P=\{x_i \text{ s.t. } 1\leq i\leq N\} \text{ and } P=\{y_j\text{ s.t. } 1\leq j\leq M\}$$ Now let $R$ be a rotation matrix. Then the effect of rotating $Q$ using $R$ would be $$\begin{split} S_R &=\sum_i \sum_j \|x_i-Ry_j\|^2\\ &= \sum_i \sum_j \left(\|x_i\|^2 +\|Ry_j\|^2-2\langle x_i, Ry_j\rangle\right)\\ &= \sum_i \sum_j \left(\|x_i\|^2 +\|y_j\|^2-2\langle x_i, Ry_j\rangle\right)\\ \end{split}$$ where the last equality holds because $\|Ry_j\|=\|y_j\|$. Therefore, the rotation must minimize $$T_R=\sum_i \sum_j \langle x_i, Ry_j\rangle$$ Now, decompose each vector into its component along and perpendicular to axis $u$: $$x_i=\langle x_i, u\rangle u + x_i^\perp \text{ and } y_j=\langle y_j, u\rangle u + y_j^\perp$$ With this, and noting that $Ru=u$, $$T_R = \sum_i \sum_j \left(\langle x_i, u\rangle \langle y_j, u\rangle + \langle x_i^\perp , Ry_j^\perp\rangle\right)$$ Let $\alpha_{ij}$ be the angle between $x_i^\perp$ and $y_j^\perp$. Then $$\begin{split} \langle x_i^\perp , Ry_j^\perp\rangle &= \|x_i^\perp\|\cdot\|y_j^\perp\|\cdot\cos(\alpha_{ij}+\theta)\\ &=\|x_i^\perp\|\cdot\|y_j^\perp\|\cdot(\cos(\alpha_{ij})\cos(\theta)-\sin(\alpha_{ij})\sin(\theta)) \end{split}$$ Therefore, differentiating $T_R$ w.r.t $\theta$ yields $$\frac{\partial T_R}{\partial \theta}=-\sum_i \sum_j \|x_i^\perp\|\cdot\|y_j^\perp\|\cdot(\cos(\alpha_{ij})\sin(\theta)+\sin(\alpha_{ij})\cos(\theta))$$ And equating to $0$ gives $$\tan \theta_0 = -\frac{\sum_i \sum_j \|x_i^\perp\|\|y_j^\perp\|\sin(\alpha_{ij})}{\sum_i \sum_j \|x_i^\perp\|\|y_j^\perp\|\cos(\alpha_{ij})}$$ This can be further simplified by noting that $$ \|x_i^\perp\|\|y_j^\perp\|\sin(\alpha_{ij}) = \langle x_i \times y_j, u\rangle = \det (x_i, y_j, u)$$ $$\|x_i^\perp\|\|y_j^\perp\|\cos(\alpha_{ij}) = \langle x_i, y_j\rangle - \langle x_i, u\rangle\langle y_j, u\rangle$$ Finally $$\boxed{\theta_0 = -\arctan \frac{\sum_{i=1}^N \sum_{j=1}^M \det (x_i, y_j, u)}{\sum_{i=1}^N \sum_{j=1}^M \left(\langle x_i, y_j\rangle - \langle x_i, u\rangle\langle y_j, u\rangle\right)}}$$ Equivalently, with $$x = \sum_{i=1}^N x_i \text { and } y = \sum_{j=1}^M y_j$$ $$\boxed{\theta_0 = -\arctan \frac{\langle x\times y, u\rangle}{\langle x, y\rangle -\langle x,u\rangle \langle y,u\rangle}}$$