TITLE: how to find out a matrix for a given minimal polynomial
QUESTION [6 upvotes]: I know how to find out the the minimal polynomial for a given matrix.
But I am stuck to do the reverse process. For example how to find out a
 $3\times3$ matrix, whose minimal polynomial is $x^2$.

REPLY [4 votes]: For a general monic polynomial $p(x)=a_0+a_1x+a_2x^2+\cdots +a_{n-1}x^{n-1}+x^n$ there is the so called companion matrix which looks like this
$$
C_p:=\begin{pmatrix}
0&-a_0 \\ I & -\mathfrak a
\end{pmatrix}
$$
where $I$ is the $(n-1)\times (n-1)$ identity matrix and $\mathfrak a=(a_1,...,a_{n-1})^\top$. $C_p$ is of dimensions $n\times n$ and has the characteristic and minimal polynomial equal to $p$.

Example. Let's take your example $p(x)=x^2$. We write this polynomial in the form
$$p(x)=x^2=0+0\cdot x+1\cdot x^2.$$
We have $a_0=0$ and $\mathfrak a=(0)^\top$. Because $n=2$, this will give us a $2\times 2$ companion matrix
$$C_p=\begin{pmatrix}0&0\\1&0\end{pmatrix}.$$
We can boost this onto a $3\times 3$ matrix by adding a zero line and a zero column:
$$C_p^{3\times 3}=\begin{pmatrix}0&0&\color{lightgray}0\\1&0&\color{lightgray}0\\\color{lightgray}0&\color{lightgray}0&\color{lightgray}0\end{pmatrix}.$$
The charcteristic polynomial will be $-x^3$, but it is not hard to see that the minimal polynomial is still $x^2$ as $[C_p^{3\times 3}]^2=0$.

Fun fact
From what I learned in my lectures on numerical mathematics, we have much better (faster, more stable, ...) algorithms for computing eigenvalues of matrices than for computing roots of polynomials directly. Therefore, in order to compute the roots of a polynomial $p$, we instead create the matrix $C_p$ and use our eigenvalue finding algorithms for these. These eigenvalues are exactly the roots of $p$.