5. Find all functions $f:(0,+\infty) \rightarrow \mathbf{R}$, such that for all $x, y>0$, we have
$$
\left(x+\frac{1}{x}\right) f(y)=f(x y)+f\left(\frac{y}{x}\right) .
$$
5. $f(x)=c_{1} x+\frac{c_{2}}{x}\left(c_{1} 、 c_{2}\right.$ are arbitrary constants $)$.

For a fixed real number $a>1$ and a new variable $t$, in equation (1), take
$$
\begin{array}{l}
x=y=t ; x=\frac{t}{a}, y=a t ; \\
x=a^{2} t, y=t ; x=y=a t,
\end{array}
$$

to obtain the system of equations involving $f(t) 、 f\left(t^{2}\right) 、 f(a t) 、 f\left(a^{2} t^{2}\right)$:
$$
\left\{\begin{array}{l}
\left(t+\frac{1}{t}\right) f(t)=f\left(t^{2}\right)+f(1), \\
\left(\frac{t}{a}+\frac{a}{t}\right) f(a t)=f\left(t^{2}\right)+f\left(a^{2}\right), \\
\left(a^{2} t+\frac{1}{a^{2} t}\right) f(t)=f\left(a^{2} t^{2}\right)+f\left(\frac{1}{a^{2}}\right), \\
\left(a t+\frac{1}{a t}\right) f(a t)=f\left(a^{2} t^{2}\right)+f(1) .
\end{array}\right.
$$

From equations (2) and (3), eliminate $f\left(t^{2}\right)$ to get
$$
\left(t+\frac{1}{t}\right) f(t)-\left(\frac{t}{a}+\frac{a}{t}\right) f(a t)=f(1)-f\left(a^{2}\right) \text {. }
$$

From equations (4) and (5), eliminate $f\left(a^{2} t^{2}\right)$ to get
$$
\left(a^{2} t+\frac{1}{a^{2} t}\right) f(t)-\left(a t+\frac{1}{a t}\right) f(a t)=f\left(\frac{1}{a^{2}}\right)-f(1) \text {. }
$$

From the above two equations, eliminate $f(a t)$ to get
$$
\begin{array}{l}
\left(\left(a t+\frac{1}{a t}\right)\left(t+\frac{1}{t}\right)-\left(\frac{t}{a}+\frac{a}{t}\right)\left(a^{2} t+\frac{1}{a^{2} t}\right)\right) f(t) \\
=\left(a t+\frac{1}{a t}\right)\left(f(1)-f\left(a^{2}\right)\right)- \\
\quad\left(\frac{t}{a}+\frac{a}{t}\right)\left(f\left(\frac{1}{a^{2}}\right)-f(1)\right) . \\
\text { Since }\left(a t+\frac{1}{a t}\right)\left(t+\frac{1}{t}\right)-\left(\frac{t}{a}+\frac{a}{t}\right)\left(a^{2} t+\frac{1}{a^{2} t}\right) \\
=a+\frac{1}{a}-\left(a^{3}+\frac{1}{a^{3}}\right)<0,
\end{array}
$$

then $f(t)=c_{1} t+\frac{c_{2}}{t}$,
where $c_{1} 、 c_{2}$ are expressions in terms of $a 、 f(1) 、 f\left(a^{2}\right) 、 f\left(\frac{1}{a^{2}}\right)$, and do not depend on the variable $t$.
If $f$ is a function of the form (6), then
$$
\begin{array}{l}
\left(x+\frac{1}{x}\right) f(y)=\left(x+\frac{1}{x}\right)\left(c_{1} y+\frac{c_{2}}{y}\right) \\
=\left(c_{1} x y+\frac{c_{2}}{x y}\right)+\left(c_{1} \frac{y}{x}+c_{2} \frac{x}{y}\right) \\
=f(x y)+f\left(\frac{y}{x}\right) .
\end{array}
$$

Therefore, $f(x)=c_{1} x+\frac{c_{2}}{x}$ is a solution to the original equation (1).