5. 1.5 *夫 Given the complex number $z=2 \cos ^{2} \theta+\mathrm{i}+(\sin \theta+\cos \theta)^{2} \mathrm{i}$, where $\theta \in\left[0, \frac{\pi}{2}\right]$. Find the maximum and minimum values of $|z|$.
Parse $z=2 \cos ^{2} \theta+(2+\sin 2 \theta) \mathrm{i}$, so $|z|^{2}=(1+\cos 2 \theta)^{2}+(2+\sin 2 \theta)^{2}=$ $6+4 \sin 2 \theta+2 \cos 2 \theta=6+2 \sqrt{5} \sin (2 \theta+\varphi)$, where $\varphi=\arctan \frac{1}{2}$.
(1) When $2 \theta+\varphi=\frac{\pi}{2}$, $\theta=\frac{\pi}{4}-\frac{\varphi}{2} \in\left[0, \frac{\pi}{2}\right]$. At this time, $|z|_{\text {max }}^{2}=6+2 \sqrt{5}$, i.e., $|z|_{\max }=\sqrt{6+2 \sqrt{5}}=\sqrt{5}+1$;
(2) $|z|^{2} \geqslant 6+2 \cos 2 \theta \geqslant 6-2=4,|z| \geqslant 2$. When $\theta=\frac{\pi}{2}$, $|z|_{\min }=2$.