Answer
See the detailed answer below.
Work Step by Step
a)
We can assume that the mass is uniformly distributed along the rod. The density of the rod is given by
$$\rho=\dfrac{M}{V}=\dfrac{M}{AL}$$
where $A$ is the cross-sectional area of the rod, and $L$ is its length.
and the density of the small shrink of it $dm$, as seen below,
$$\rho=\dfrac{dm}{Adr}$$
where $dr$ is the length of $dm$.
So from these two formulas,
$$\dfrac{M}{ \color{red}{\bf\not}AL}=\dfrac{dm}{ \color{red}{\bf\not}Adr}$$
Hence,
$$dm=\dfrac{Mdr}{L}\tag 1$$
We know that the gravitational potential energy between the two masses $m$ and $dm$ is given by
$$dU=\dfrac{-Gmdm}{r}$$
Plugging from (1);
$$dU=\dfrac{-Gm Mdr}{rL}=\dfrac{-GmM}{L}\dfrac{dr}{r}$$
Integrating both sides;
$$\int_0^UdU=\int_{r_i}^{r_f}\left[\dfrac{-GmM}{L}\dfrac{dr}{r}\right]$$
$$U=\dfrac{-GmM}{L}\int_{x-\frac{L}{2}}^{x+\frac{L}{2}}\dfrac{dr}{r}=\dfrac{-GmM}{L}\ln r\bigg|_{x-\frac{L}{2}}^{x+\frac{L}{2}}$$
$$\boxed{U =\dfrac{-GmM}{L}\ln \left[\dfrac{x+\frac{L}{2}}{x-\frac{L}{2}}\right] }$$
---
b)
We know that the force is given by
$$F=\dfrac{-dU}{dx}$$
Plugging from the boxed formula above;
$$F=\dfrac{-d}{dx}\left(\dfrac{-GmM}{L}\ln \left[\dfrac{x+\frac{L}{2}}{x-\frac{L}{2}}\right]\right)$$
$$F=\dfrac{GmM}{L}\dfrac{ d}{dx}\left(\ln \left[\dfrac{x+\frac{L}{2}}{x-\frac{L}{2}}\right]\right)$$
$$F=\dfrac{GmM}{L}\dfrac{ d}{dx}\left(\ln \left[ x+\frac{L}{2}\right]-\ln\left[ x-\frac{L}{2 }\right] \right)$$
$$F=\dfrac{GmM}{L} \left( \dfrac{ 1}{x+\frac{L}{2} }- \dfrac{ 1}{x-\frac{L}{2} } \right)$$
$$F=\dfrac{GmM}{L} \left( \dfrac{ x-\frac{L}{2}-\left[x+\frac{L}{2}\right]}{x^2-\frac{L^2}{4} } \right)$$
$$F=\dfrac{GmM}{L} \left( \dfrac{ x-\frac{L}{2}- x-\frac{L}{2} }{x^2-\frac{L^2}{4} } \right)$$
$$F=\dfrac{ GmM}{ \color{red}{\bf\not}L} \left( \dfrac{ - \color{red}{\bf\not}L }{x^2-\frac{L^2}{4} } \right)$$
$$\boxed{F= \dfrac{ - GmM }{x^2-\frac{L^2}{4} }} \tag {where $x\gt \dfrac{L}{2}$} $$