# When are two matrices similar, and related by real matrices?

Let's say we have two real matrices $A$ and $B$, which have the same eigenvalues. My question is when the two matrices can be related as $$A=XBX^{-1},$$ where $X$ is a real matrix. If $A$ and $B$ are diagonalizable as $A=UDU^{-1}$ and $B=ODO^{-1}$, then obviously $X=UO^{-1}$. However, it is not clear when $X$ is real since $U$ and $O$ are not necessarily real.

This is a well-known exercise. Suppose the real matrices $A$ and $B$ are similar in ${\bf M}_n({\mathbb C})$. Let $P\in{\bf GL}_n({\mathbb C})$ be such that $PA=BP$. Decompose $P$ into real and imaginary parts, $P=R+iJ$. Then $RA=BR$ and $JA=BJ$. Consider $Q(t):=R+tJ\in{\bf M}_n({\mathbb C})$, which does satisfy $Q(t)A=BQ(t)$. The polynomial $f(t)=\det Q(t)$ is non-zero because $f(i)\ne0$. Therefore there exist (many) real values $t$ such that $f(t)\ne0$. Then $Q(t)\in{\bf GL}_n({\mathbb R})$ and $A$ and $B$ are similar in ${\bf M}_n({\mathbb R})$.
The arguments applies more generally when $A$ and $B$, with entries in a field $k$, are similar over some extension $K$. They actually are similar over $k$.