Example 7 Given $x \in\left(0, \frac{\pi}{2}\right)$, try to find the maximum value of the function $f(x)=3 \cos x + 4 \sqrt{1+\sin ^{2} x}$, and the corresponding $x$ value.
Given $x \in\left(0, \frac{\pi}{2}\right), \cos x>0$, it is easy to see that $f(x)>0$.
$$\begin{array}{l}
\therefore f^{2}(x)=\left(3 \cos x+4 \sqrt{1+\sin ^{2} x}\right)^{2} \\
\leqslant\left(3^{2}+4^{2}\right)\left[\cos ^{2} x+\left(1+\sin ^{2} x\right)\right]=50 . \\
\therefore f(x) \leqslant 5 \sqrt{2}
\end{array}$$

The equality holds if and only if $\frac{3}{\cos x}=\frac{4}{\sqrt{1+\sin ^{2} x}}$.
Solving this, we get $\cos x=\frac{3}{5} \sqrt{2}$, i.e., when $x=\arccos \frac{3}{5} \sqrt{2}$, the function $f(x)$ has a maximum value of $5 \sqrt{2}$.