2. (20 points) Given $\sum_{i=1}^{n} a_{i} x_{i}=p, \sum_{i=1}^{n} a_{i}=q$, and $a_{i}>0(i=1,2, \cdots, n), p, q$ are constants. Find the minimum value of $\sum_{i=1}^{n} a_{i} x_{i}^{2}$.
2. Let $x_{i}=\lambda+y_{i}$ (where $\lambda$ is a parameter to be determined). Then
$$
\begin{array}{l}
\sum_{i=1}^{n} a_{i} x_{i}^{2}=\sum_{i=1}^{n} a_{i}\left(\lambda+y_{i}\right)^{2} \\
=\lambda^{2} q+2 \lambda \sum_{i=1}^{n} a_{i} y_{i}+\sum_{i=1}^{n} a_{i} y_{i}^{2} . \\
\text { and } \sum_{i=1}^{n} a_{i} x_{i}=\sum_{i=1}^{n} a_{i}\left(\lambda+y_{i}\right) \\
=\lambda q+\sum_{i=1}^{n} a_{i} y_{i}=p,
\end{array}
$$

Thus, taking $\lambda=\frac{p}{q}$, we have $\sum_{i=1}^{n} a_{i} y_{i}=0$.
Substituting into equation (1) gives
$$
\sum_{i=1}^{n} a_{i} x_{i}^{2}=\frac{p^{2}}{q}+\sum_{i=1}^{n} a_{i} y_{i}^{2} \geqslant \frac{p^{2}}{q},
$$

with equality holding if and only if $y_{1}=y_{2}=\cdots=y_{n}=0$, i.e., $x_{1}=x_{2}=\cdots=x_{n}=\lambda=\frac{p}{q}$.
Therefore, $\sum_{i=1}^{n} a_{i} x_{i}^{2}$ attains its minimum value $\frac{p^{2}}{q}$.