13. Given the sequence $\left\{a_{n}\right\}$ where $a_{1}=\frac{1}{2}, a_{n+1}=\frac{1}{2} a_{n}+\frac{2 n+1}{2^{n+1}}\left(n \in \mathbf{N}^{*}\right)$.
(1) Find the general term formula of the sequence $\left\{a_{n}\right\}$;
(2) Find the sum of the first $n$ terms $S_{n}$ of the sequence $\left\{a_{n}\right\}$.
(1) From $a_{n+1}=\frac{1}{2} a_{n}+\frac{2 n+1}{2^{n+1}}\left(n \in \mathbf{N}^{*}\right)$, we know: $2^{n+1} a_{n+1}=2^{n} a_{n}+2 n+1\left(n \in \mathbf{N}^{*}\right)$.
Let $b_{n}=2^{n} a_{n}$, then $b_{1}=1$ and $b_{n+1}=b_{n}+2 n+1\left(n \in \mathbf{N}^{*}\right)$.
By $b_{n}=\left(b_{n}-b_{n-1}\right)+\left(b_{n-1}-b_{n-2}\right)+\cdots+\left(b_{2}-b_{1}\right)+b_{1}=(2 n-1)+\cdots+3+1=n^{2}$, we get
$$
a_{n}=\frac{n^{2}}{2^{n}}
$$
(2) From the problem, we know:
$$
S_{n}=\frac{1^{2}}{2^{1}}+\frac{2^{2}}{2^{2}}+\frac{3^{2}}{2^{3}}+\frac{4^{2}}{2^{4}}+\cdots+\frac{(n-1)^{2}}{2^{n-1}}+\frac{n^{2}}{2^{n}}
$$

So
$$
\frac{1}{2} S_{n}=\frac{1^{2}}{2^{2}}+\frac{2^{2}}{2^{3}}+\frac{3^{2}}{2^{4}}+\frac{4^{2}}{2^{5}}+\cdots+\frac{(n-1)^{2}}{2^{n}}+\frac{n^{2}}{2^{n+1}}
$$

Subtracting the two equations, we get
$$
\frac{1}{2} S_{n}=\frac{1}{2}+\frac{3}{2^{2}}+\frac{5}{2^{3}}+\frac{7}{2^{4}}+\frac{9}{2^{5}}+\cdots+\frac{(2 n-1)}{2^{n}}-\frac{n^{2}}{2^{n+1}}
$$

Let
$$
T_{n}=\frac{1}{2}+\frac{3}{2^{2}}+\frac{5}{2^{3}}+\frac{7}{2^{4}}+\frac{9}{2^{5}}+\cdots+\frac{(2 n-1)}{2^{n}}
$$

Using the method of differences, we find $T_{n}=3-\frac{2 n+3}{2^{n}}$, so
$$
S_{n}=6-\frac{2 n+3}{2^{n-1}}-\frac{n^{2}}{2^{n}}=6-\frac{n^{2}+4 n+6}{2^{n}}
$$