Example 4 Given the function $f(x)$ satisfies: $f(0)=1, f\left(\frac{\pi}{2}\right)=2$, and for any $x, y \in \mathbf{R}$, we have
$$
f(x+y)+f(x-y) \leqslant 2 f(x) \cos y
$$

Try to find $f(x)$.
Given $f(x+y)+f(x-y)=2 f(x) \cos y+g(x, y), \forall g(x, y) \leqslant 0$,
Let $x=0, y=t$ and substitute into (1) to get
$$
f(t)+f(-t)=2 f(0) \cos t+g(0, t)=2 \cos t+g(0, t),
$$

Let $x=\frac{\pi}{2}+t, y=\frac{\pi}{2}$, and substitute into (2) to get
$$
f(t+\pi)+f(t)=g\left(\frac{\pi}{2}+t, \frac{\pi}{2}\right)
$$

Let $x=\frac{\pi}{2}, y=\frac{\pi}{2}+t$, and substitute into (1) to get
$$
f(t+\pi)+f(-t)=-2 f\left(\frac{\pi}{2}\right) \sin t+g\left(\frac{\pi}{2}, \frac{\pi}{2}+t\right)=-4 \sin t+g\left(\frac{\pi}{2}, \frac{\pi}{2}+t\right),
$$
(2) + (3) - (4), we get
$$
2 f(t)=2 \cos t+4 \sin t+g(0, t)+g\left(\frac{\pi}{2}+t, \frac{\pi}{2}\right)-g\left(\frac{\pi}{2}, \frac{\pi}{2}+t\right) .
$$

Thus, $f(t)=\cos t+2 \sin t+\frac{1}{2}\left[g(0, t)+g\left(\frac{\pi}{2}+t, \frac{\pi}{2}\right)-g\left(\frac{\pi}{2}, \frac{\pi}{2}+t\right)\right]$,
Therefore, $f(x)=\cos x+2 \sin x+\frac{1}{2}\left[g(0, x)+g\left(\frac{\pi}{2}+x, \frac{\pi}{2}\right)-g\left(\frac{\pi}{2}, \frac{\pi}{2}+x\right)\right]$.
For $\forall x, y \in \mathbf{R}$, we have $g(x, y) \leqslant 0$.
Analysis (1) The function $g(x, y)$ in this problem is a bivariate function. Since we use the method of specialization, the solution to the functional inequality is obtained under non-equivalent transformations, which is just a "formal solution" of the functional inequality. For any given $g(x, y)$, the derived $f(x)$ must be substituted back into the original inequality for verification.
(2) If the conditions $f(0)=a, f\left(\frac{\pi}{2}\right)=b, g(x, y)=0$ are given,
then the functional inequality becomes a functional equation
$$
f(x+y)+f(x-y)=2 f(x) \cos y,
$$

whose solution is
$$
f(x)=a \cos x+b \sin x .
$$

Verification: If $f(x)=a \cos x+b \sin x$, then
$$
\begin{aligned}
& f(x+y)+f(x-y) \\
= & {[a \cos (x+y)+b \sin (x+y)]+[a \cos (x-y)+b \sin (x-y)] } \\
= & a[\cos (x+y)+\cos (x-y)]+b[\sin (x+y)+\sin (x-y)] \\
= & 2 a \cos x \cos y+2 b \sin x \cos y \\
= & 2(a \cos x+b \sin x) \cos y \\
= & 2 f(x) \cos y
\end{aligned}
$$

Therefore, (5) is a solution to the functional equation (5).