Conversion of Cartesian Coordinate System Unit Vectors to Spherical Coordinate System Unit Vectors
Formulas
The expression for converting the unit vectors from the Cartesian coordinate system to the spherical coordinate system is as follows. $$ \begin{align*} \hat{ \mathbf{x} }&= \cos \phi \sin \theta \hat{ \mathbf{r} } + \cos \phi \cos \theta \hat{ \boldsymbol{\theta} } - \sin\phi\hat{ \boldsymbol{\phi} } \\ \hat{ \mathbf{y} } &= \sin\phi\sin\theta \hat{ \mathbf{r} } + \sin\phi\cos\theta\hat{ \boldsymbol{\theta} } + \cos\phi\hat{ \boldsymbol{\phi} } \\ \hat{ \mathbf{z} } &= \cos\theta\hat{ \mathbf{r} } - \sin\theta\hat{ \boldsymbol{\theta} } \end{align*} $$
The expression for converting the unit vectors from the spherical coordinate system to the Cartesian coordinate system is as follows. (Relation between spherical and Cartesian coordinate systems)
$$ \begin{align*} \hat{ \mathbf{r} } &= \cos\phi \sin\theta \hat{ \mathbf{x} } + \sin\phi \sin\theta\hat{ \mathbf{y} } + \cos\theta\hat{ \mathbf{z} } \\ \hat{ \boldsymbol{\theta} } &= \cos\phi \cos\theta \hat{ \mathbf{x} } + \sin\phi \cos\theta\hat{ \mathbf{y} } - \sin\theta\hat{ \mathbf{z} } \\ \hat{ \boldsymbol{\phi} } &= -\sin\phi \hat{ \mathbf{x} } + \cos\phi \hat{ \mathbf{y} } \end{align*} $$
Derivation
Unit vector $\hat{\mathbf{x}}$
To find $\hat{ \mathbf{x} }$, you need to multiply certain terms by $\hat{ \mathbf{r} } ,\ \hat{ \boldsymbol{\theta} } ,\ \hat{ \boldsymbol{\phi} }$ and add them so that the $\hat{ \mathbf{y} } ,\ \hat{ \mathbf{z} }$ term disappears. It can be seen that multiplying $\hat{ \mathbf{r} }$ by $\sin \theta$ and adding that to $\hat{ \boldsymbol{\theta} }$ multiplied by $\cos \theta$ makes the $\hat{ \mathbf{z} }$ term vanish.
$$ \begin{align*} & \sin \theta \hat{ \mathbf{r} } + \cos \theta \hat{ \boldsymbol{\theta} } \\ &= (\cos \phi \sin^2 \theta + \cos \phi \cos^2\theta)\hat{ \mathbf{x} } + (\sin \phi \sin^2 \theta + \sin \phi \cos^2 \theta)\hat{ \mathbf{y} } \\ &= \cos \phi \hat{ \mathbf{x} } +\sin \phi \hat{ \mathbf{y} } \end{align*} $$
Further, by multiplying this result by $\cos \phi$ and adding it to $\hat{ \boldsymbol{\phi} }$ multiplied by $-\sin \phi$, you can neatly obtain $\hat{ \mathbf{x} }$.
$$ \begin{align*} & \cos \phi (\sin \theta \hat{ \mathbf{r} } + \cos \theta \hat{ \boldsymbol{\theta} })-\sin \phi \hat{ \boldsymbol{\phi} } \\ &= (\cos^2\phi \hat{ \mathbf{x} } + \cos \phi \sin \phi \hat{ \mathbf{y} } ) + (\sin^2\phi \hat{ \mathbf{x} } -\sin \phi \cos \phi \hat{ \mathbf{y} } ) \\ &= \hat{ \mathbf{x} } \end{align*} $$
Therefore, it is summarized as follows.
$$ \hat{ \mathbf{x} } = \cos \phi \sin \theta \hat{ \mathbf{r} } + \cos \phi \cos \theta \hat{ \boldsymbol{\theta} } - \sin\phi\hat{ \boldsymbol{\phi} } $$
Unit vector $\hat{\mathbf{y}}$
The method is the same as in the case of $\hat{ \mathbf{x} }$, so I will just write the equations without explanation. Removing the $\hat{ \mathbf{z} }$ term is the same as in the case of $\hat{ \mathbf{x} }$.
$$ \begin{align*} &&\sin \theta \hat{ \mathbf{r} } + \cos \theta \hat{ \boldsymbol{\theta} } &= \cos \phi \hat{ \mathbf{x} } + \sin \phi \hat{ \mathbf{y} } \\ \implies&& \sin\phi (\sin\theta\hat{ \mathbf{r} }+\cos\theta \hat{ \boldsymbol{\theta} } ) &= \sin \phi \cos \phi \hat{ \mathbf{x} } + \sin^2\phi \hat{ \mathbf{y} } \end{align*} $$
And
$$ \cos \phi \hat{ \boldsymbol{\phi} }=-\sin\phi\cos\phi\hat{ \mathbf{x} }+\cos^2\phi\hat{ \mathbf{y} } $$
Therefore, it is as follows.
$$ \begin{align*} && \sin\phi (\sin\theta\hat{ \mathbf{r} }+\cos\theta \hat{ \boldsymbol{\theta} } )+\cos \phi \hat{ \boldsymbol{\phi} } &= (\cos^2\phi + \sin^2\phi )\hat{ \mathbf{y} } \\ && &= \hat{ \mathbf{y} } \end{align*} $$
$$ \therefore \hat{ \mathbf{y} } = \sin\phi\sin\theta \hat{ \mathbf{r} } + \sin\phi\cos\theta \hat{ \boldsymbol{\theta} } + \cos\phi \hat{ \boldsymbol{\phi} } $$
Unit vector $\hat{\mathbf{z}}$
Since the $\hat{ \mathbf{z} }$ term must remain, multiplying $\hat{ \mathbf{r} }$ by $\cos\theta$ and adding that to $\hat{ \boldsymbol{\theta} }$ multiplied by $-\sin\theta$ results in the following.
$$ \begin{align*} \cos\theta\hat{ \mathbf{r} } -\sin\theta \hat{ \boldsymbol{\theta} } &=\cos\theta (\cos \phi \sin \theta \hat{\mathbf{x}}+\sin \phi \sin \theta \hat{\mathbf{y}}+\cos\theta\hat{\mathbf{z}}) \\ &\quad -\sin \theta (\cos \phi \cos \theta \hat{\mathbf{x}} + \sin \phi \cos \theta \hat{\mathbf{y}} -\sin \theta \hat{\mathbf{z}}) \\ &= (\sin\theta\cos\theta\cos\phi \hat{ \mathbf{x} } + \sin\theta\cos\theta\sin\phi \hat{ \mathbf{y} }+ \cos^2\theta \hat{ \mathbf{z} }) \\ &\quad +(-\sin\theta\cos\theta\cos\phi \hat{ \mathbf{x} }-\sin\theta\cos\theta\sin\phi \hat{ \mathbf{y} } + \sin^2{\theta} \hat{ \mathbf{z} }) \\ &= (\cos^2\theta + \sin ^2 \theta ) \hat{ \mathbf{z} } \\ &= \hat{ \mathbf{z} } \end{align*} $$
$$ \therefore \hat{ \mathbf{z} } = \cos\theta\hat{ \mathbf{r} } - \sin\theta \hat{ \boldsymbol{\theta} } $$