(4) (30 points) As shown in the figure, $M$ is the midpoint of the median $A D$ of $\triangle A B C$, a line through point $M$ intersects sides $A B$ and $A C$ at points $P$ and $Q$, respectively. Let $\overrightarrow{A P}=x \overrightarrow{A B}, \overrightarrow{A Q}=y \overrightarrow{A C}$, and denote $y=f(x)$.
(1) Find the expression for the function $y=f(x)$;
(2) Let $g(x)=x^{3}+3 a^{2} x+2 a, x \in[0,1]$. If for any $x_{1} \in \left[\frac{1}{3}, 1\right]$, there always exists $x_{2} \in[0,1]$ such that $f\left(x_{1}\right)=g\left(x_{2}\right)$ holds, find the range of the real number $a$.
(4) (1) Since the line through point $M$ intersects sides $A B$ and $A C$, we have $x>0, y>0$, thus $\overrightarrow{A B}=\frac{1}{x} \overrightarrow{A P}, \overrightarrow{A C}=\frac{1}{y} \overrightarrow{A Q}$. Therefore,
$$
\overrightarrow{A M}=\frac{1}{2} \overrightarrow{A D}=\frac{1}{4}(\overrightarrow{A B}+\overrightarrow{A C})=\frac{1}{4 x} \overrightarrow{A P}+\frac{1}{4 y} \overrightarrow{A Q}
$$

Since points $P, M, Q$ are collinear, we have $\frac{1}{4 x}+\frac{1}{4 y}=1$, which gives $f(x)=y=\frac{x}{4 x-1}$.
From $0<y \leqslant 1$, we get $0<\frac{x}{4 x-1} \leqslant 1$. Solving this, we find $x \geqslant \frac{1}{3}$.
Thus, $y=f(x)=\frac{x}{4 x-1}, x \in\left[\frac{1}{3}, 1\right]$.
(2) Since $g^{\prime}(x)=3 x^{2}+3 a^{2} \geqslant 0$, $g(x)$ is an increasing function on $[0,1]$, so $g(x)_{\text {min }}=g(0)=2 a, g(x)_{\text {max }}=g(1)=3 a^{2}+2 a+1$, which means the range of the function $y(x)$ is $\left[2 a, 3 a^{2}+2 a+1\right]$.

Also, $f(x)=\frac{x}{4 x-1}=\frac{1}{4}+\frac{\frac{1}{4}}{4 x-1}$ is a decreasing function on $\left[\frac{1}{3}, 1\right]$, so $f(x)_{\text {min }}=f(1)=\frac{1}{3}, f(x)_{\text {max }}=f\left(\frac{1}{3}\right)=1$, which means the range of the function $f(x)$ is $\left[\frac{1}{3}, 1\right]$.
By the problem's condition, $\left[\frac{1}{3}, 1\right] \subseteq\left[2 a, 3 a^{2}+2 a+1\right]$, thus $\left\{\begin{array}{l}2 a \leqslant \frac{1}{3}, \\ 3 a^{2}+2 a+1 \geqslant 1 \text {. }\end{array}\right.$

Solving this, we find the range of $a$ is $\left(-\infty,-\frac{2}{3}\right] \cup\left[0, \frac{1}{6}\right]$.