# What is average distance from center of square to some point?

How can I calculate average distance from center of a square to points inside the square?

-

For the average distance, what you want is the expected value of the distance from the center over the set of all points in the square, which is the sum of the probability of each point times the distance to that point. The problem is (more or less) that there are a whole lot of points and each point has infinitesimal probability. One way to deal with this is to use calculus.

Consider the square with sides of length 1 parallel to the axes and centered at the origin. This square is the set of points $(x,y)$ with $-\frac{1}{2}\le x\le\frac{1}{2}$ and $-\frac{1}{2}\le y\le\frac{1}{2}$. The total area of the square is 1. The sum of the probabilities of each point (not dealing with distance yet) is $\int_{-\frac{1}{2}}^{\frac{1}{2}}\int_{-\frac{1}{2}}^{\frac{1}{2}}\;dx\;dy=1$. Now, at each point, multiply the probability by the distance to get the average distance: $\int_{-\frac{1}{2}}^{\frac{1}{2}}\int_{-\frac{1}{2}}^{\frac{1}{2}}\sqrt{x^2+y^2}\;dx\;dy$.

-
In other words this is $E\left[\sqrt{X^2+Y^2} \right]$. –  PEV Dec 26 '10 at 21:35
@Trevor: I edited the first sentence to try to better cover the jump I'd made. –  Isaac Dec 26 '10 at 21:40
Can I just assume that average x is 1/4 and average y is 1/4, so the average distance is square(1/16+1/16)? (as a square is symmetric) –  Newcommer Dec 26 '10 at 21:47
@Newcommer: because the relationship between the distance to a point in the square and the horizontal (x-direction) distance is non-linear, and similarly for the vertical, it won't work. –  Isaac Dec 26 '10 at 22:04
And one can possibly use Green's Theorem to simplify the calculation of the double integral. –  Aryabhata Dec 27 '10 at 3:30

Without loss of generality we find the average distance to the origin of a randomly selected point in the region $R:=\{(x,y): 0\leq y\leq x\leq L/2\}$. The region $R$ is a triangle that is one-eighth of the original square. The uniform density on this region is $$f(x,y)=\cases{8/L^2 &\text{if } (x,y)\in R\cr 0&\text{otherwise.}}$$

Therefore $$\begin{eqnarray} \text{average distance} &=& \int_0^{L/2} \int_0^x \sqrt{x^2+y^2}\,dy\,dx \ {8\over L^2} \\ &=& \int_0^{L/2}\int_0^1 x^2\sqrt{1+w^2}\,dw \,dx\ {8\over L^2} \\ &=& \int_0^1 \sqrt{1+w^2}\,dw\quad \int_0^{L/2} x^2\,dx \quad\ {8\over L^2} \\ &=& \int_0^1 \sqrt{1+w^2}\,dw\quad {1\over 3}\left({L\over 2}\right)^3 {8\over L^2} \\ &=& {L\over 6} \left(\sqrt{2}+\log(1+\sqrt{2})\right). \end{eqnarray}$$

Update: In the comments below, Isaac outlined a nice trig substitution to evaluate $I:=\int_0^1\sqrt{1+w^2}\,dw$. For completeness, here is another way. First do a change of variables $w=x/\sqrt{1-x^2}$, then use partial fractions to obtain $$\begin{eqnarray} I &=& \int_0^{1/ \sqrt{2}} {dx\over(1-x^2)^2} \\ &=& {1\over 4} \int_0^{1/ \sqrt{2}}\left[{1\over(1+x)^2}+{1\over(1-x)^2} +{1\over 1+x}+{1\over 1-x} \right]\, dx \\ &=& {1\over 4} \left[{1\over \sqrt{2}+1}+ {1\over \sqrt{2}-1} +(-\log(2)+\log(\sqrt{2}+2))+ (\log(2)-\log(2-\sqrt{2})) \right]\\ &=&{1\over 2}\left[\sqrt{2}+ \log(1+\sqrt{2}) \right]. \end{eqnarray}$$

-
I don't pretend to understand your last step, but it agrees with Wolfram Alpha to 30 decimal places... –  TonyK Jan 20 '12 at 21:21
I admit that for the last step I just plugged into Maple. This integral can also be found in standard reference books. –  Byron Schmuland Jan 20 '12 at 21:32
In fact, I've just converted it to an equally hard problem: what's the average distance from the origin to a point randomly selected on the perimeter of a square? I'll try to think of a better explanation... –  Byron Schmuland Jan 20 '12 at 21:46
@TonyK: I think if you let $w=\tan\theta$ so that $dw=\sec^2\theta d\theta$, the integral becomes $$\int\sqrt{1+w^2}\,dw=\int\sec^3\theta d\theta.$$ That integral can be done by using integration by parts, the identity $\tan^2x+1=\sec^2x$, and solving for $\int\sec^3\theta d\theta$. –  Isaac Jan 20 '12 at 21:54
@ByronSchmuland: Integrands with $\sqrt{1+x^2}$, $\sqrt{1-x^2}$, or $\sqrt{x^2-1}$ are prime candidates for this sort of trigonometric substitution ($x=\tan\theta$, $x=\sin\theta$, and $x=\sec\theta$, respectively, so that the square root expression becomes a single trig function of $\theta$). –  Isaac Jan 20 '12 at 22:07

I am not an expert, but my intuition and some empirical data tell me this is incorrect. The [final] integral you are calculating is the first step, but this is to summed distance from each point in the square to the center. To get the average, you must divide by the number of points, $L^2$, where $L$ is the side length. What you will find using a numeric approximation is that the average is $L/6 \times (\sqrt2+ln(1+\sqrt2))$. I have not checked via formal proof, but you will find that this passes a "gut check" when comparing two squares with side lengths $1$ and $3$. Using your integral on a square of side length $3$, the average is calculated near $10.33$, though the greatest distance possible is but $2.12$.

-
Simon, your intuition is correct. I've added a calculation in my answer. –  Byron Schmuland Jan 20 '12 at 18:47
Simon, I am curious to know how your intuition and some empirical data can yield a formula such as $(\sqrt2+\log(1+\sqrt2))/6$. –  Did Jan 21 '12 at 13:50
I used the numeric integration on my calculator to find the approximations for squares of side lengths 1 and 3. I saw that the ratio was linear with length L, so assumed that the integral was a constant. I then used google to search for the first six digits of the constant and found the reference. I wish I could claim something more impressive. –  Simon Jan 21 '12 at 18:12

I believe this can be easily generalized for squares of side L. Consider the square with sides of length L parallel to the axes and centered at the origin. This square is the set of points $(x,y)$ with $-\frac{L}{2}\le x\le\frac{L}{2}$ and $-\frac{L}{2}\le y\le\frac{L}{2}$. The total area of the square is L^2. The distance of a random point (x,y) from the center is $F(x,y)=\sqrt{x^2+y^2}$. The probability density function for the distance is $f(x,y)=f(x)f(y)=\frac{1}{L}*\frac{1}{L}=\frac{1}{L^2}$ as the random variables for each axis are independent.

The expected value for the distance is $E=\int_{-\frac{L}{2}}^{\frac{L}{2}}\int_{-\frac{L}{2}}^{\frac{L}{2}}f(x,y)*F(x,y)\;dx\;dy=\int_{-\frac{L}{2}}^{\frac{L}{2}}\int_{-\frac{L}{2}}^{\frac{L}{2}}\frac{1}{L^2}*\sqrt{x^2+y^2}\;dx\;dy$.

Substituting $x=uL$ and $y=vL$ we have $E=L\int_{-\frac{1}{2}}^{\frac{1}{2}}\int_{-\frac{1}{2}}^{\frac{1}{2}}\sqrt{u^2+v^2}\;du\;dv={L\over 6} \left(\sqrt{2}+\log(1+\sqrt{2})\right)$

-
Didn't you notice that this question has already been answered (and nearly three years ago)? –  TonyK Nov 22 '14 at 0:11