Bar Magnet as an Equivalent Solenoid —
Let us consider a solenoid consisting of $n$ turns per unit length, having total length of $2 l$ and radius $a$ as shown in figure.
Let $I$ be the current through the solenoid. Consider a small circular element of thickness $d x$ of the solenoid at a distance $x$ from the center of the solenoid. Let number of turns in the element $d x$ be $n d x$. The magnetic field at point $P$ on the axial line at a distance $r$ from the center $O$ of the solenoid, due to the circular element will be :
$d B=\frac{\mu_0 n d x I a^2}{2\left[(r-x)^2+a^2\right]^{3 / 2}}$
The magnitude of the net magnetic field $B$ at point $P$ due to the entire solenoid,
$B=\frac{\mu_0 n I a^2}{2} \int_{-l}^{+l} \frac{d x}{\left[(r-x)^2+a^2\right]^{3 / 2}}$ .......(i)
If point $P$ lies very far from the center of the solenoid $(r>>a$ and $r>>x)$ then we can approximate
$\left[(r-x)^2+a^2\right]^{3 / 2} \approx r^3$ ......... (ii)
From equation (i) and (ii), we get
$B=\frac{\mu_0 n I a^2}{2 r^3} \int_{-l}^{+l} d x=\frac{\mu_0 n I a^2}{2 r^3} \times 2 l$ ........ (iii)
Magnetic moment, $m=$ total number of turns $\times$ current $\times$ area of cross-section
$\therefore \quad m=(n \times 2 l) \times(I) \times\left(\pi a^2\right)$ ......(iv)
From equation (iii) and (iv), we get
$B=\frac{\mu_0}{4 \pi} \times \frac{2 m}{r^3}$