Skip to content
scipython.com

The goat problem

Posted on

The goat grazing problem asks how long the rope that tethers a goat to the edge of a circular paddock must be for the goat to be able to graze exactly half of the paddock. This post derives an equation for the length of the rope, solves it numerically with SciPy and shows that Ullisch's exact solution, a ratio of contour integrals, reduces to the same equation.

A goat is tethered to a point on the perimeter of a circular paddock of grass by a rope of length \(r\). What should \(r\) be for the goat to be able to graze exactly half the area of the paddock?

This is a famous problem in recreational mathematics, the goat grazing problem, and is usually solved by approximate or numerical methods, despite being relatively easy to express in the form of a mathematical equation (derived below). The mathematician Ingo Ullisch [Math. Intelligencer 42, 12–16 (2020), doi:10.1007/s00283-020-09966-0] derived an exact solution in terms of the ratio of two contour integrals, which has been (generously) described as being in closed form:

\[r = 2 \cos\left(\frac{1}{2} \frac{\oint_{|z - 3 \pi / 8| = \pi / 4} z / (\sin z - z \cos z - \pi / 2) \,dz}{\oint_{|z - 3 \pi / 8| = \pi / 4} 1 / (\sin z - z \cos z - \pi / 2)\, dz}\right).\]

Here we take the numerical approach (and return to Ullisch's formula at the end). Let the paddock be a unit circle, with area \(\pi\), centred at O, and let the goat be tethered at the point T on its perimeter. The extent of the goat's grazing area is illustrated below. Our job is to find the value of \(r\) that makes this area equal to \(\pi/2\).

Two overlapping circles: the paddock, centred at O, and a circle of radius r centred at T, a point on the paddock's left edge, dashed outside the paddock. The lens-shaped region inside both is shaded. The circles cross at P, above the diameter through T and O, and at its mirror image below it; Q is where the circle about T crosses the diameter. Lines join T and O to both crossing points; TO and OP are labelled 1, TP is labelled r, and the angles OTP and TOP are labelled theta and alpha.
The goat's grazing area (shaded): the part of the paddock, a unit circle centred at O, within a distance \(r\) of the tether point T. The circle of radius \(r\) about T is dashed outside the paddock, where the fence keeps the goat out.

First note four relations that will prove useful:

\[\begin{align} \alpha &= \pi - 2\theta, \tag{1}\\ \sin\theta &= \frac{\sin\alpha}{r}, \tag{2}\\ r^2 &= 1 + 1 - 2\cos\alpha = 2(1 - \cos\alpha), \tag{3}\\ 1 &= 1 + r^2 - 2r\cos\theta \;\Rightarrow\; r = 2\cos\theta. \tag{4} \end{align}\]

The first holds because the triangle OTP is isosceles (its sides OT and OP are both radii of the paddock), so that its angles at T and P are both \(\theta\); the second is the sine rule for triangles; and the third and fourth follow from the cosine rule.

The area of the circular sector TPQ, centred on T, is

\[A_1 = \pi r^2 \frac{\theta}{2\pi} = \frac{r^2\theta}{2},\]

the area of the circular sector OTP, centred on O, is

\[A_2 = \pi\frac{\alpha}{2\pi} = \frac{\alpha}{2},\]

and the area of the triangle OTP is

\[A_3 = \frac{1}{2}r\sin\theta = \frac{1}{2}\sin\alpha,\]

using (2). The shaded region above the diameter TQ is made up of the sector TPQ and the segment of the paddock cut off by the chord TP, whose area is \(A_2 - A_3\). The area that the goat can graze is therefore \(A = 2(A_1 + A_2 - A_3)\), and the problem is to solve

\[A = r^2\theta + \alpha - \sin\alpha = \frac{\pi}{2}\]

for \(\alpha\) and hence \(r\). Using (1) and (3) to write \(\theta = \frac{1}{2}(\pi - \alpha)\) and \(r^2 = 2(1 - \cos\alpha)\) and rearranging yields

\[\sin\alpha + (\pi - \alpha)\cos\alpha = \frac{\pi}{2}. \tag{5}\]

This equation is readily solved using, for example, the Newton–Raphson root-finding algorithm, as implemented by scipy.optimize.newton. The derivative of the left-hand side of (5), \(-(\pi - \alpha)\sin\alpha\), is passed as the argument fprime; without it, newton uses the secant method instead.

python
import numpy as np
from scipy.optimize import newton

def func(alpha):
    return np.sin(alpha) + (np.pi - alpha) * np.cos(alpha) - np.pi / 2

def fprime(alpha):
    return -(np.pi - alpha) * np.sin(alpha)

alpha_guess = np.pi / 2
alpha = newton(func, alpha_guess, fprime=fprime)
print(alpha)
Output
1.2358969242799094

This value of \(\alpha\) is in radians. The corresponding rope length, \(r\), is given by (3):

python
r = np.sqrt(2 * (1 - np.cos(alpha)))
print(r)
Output
1.1587284730181215

The grazable fraction of the paddock, \(A/\pi\), can be plotted as a function of the rope length, \(r\), as follows:

python
import matplotlib.pyplot as plt

rgrid = np.linspace(0, 2, 100)
cos_alpha = 1 - rgrid**2 / 2
alpha = np.arccos(cos_alpha)
theta = (np.pi - alpha) / 2
A = rgrid**2 * theta + alpha - np.sin(alpha)

plt.plot(rgrid, A / np.pi)
plt.hlines(0.5, 0, r, colors='k', linestyles='--')
plt.vlines(r, 0, 0.5, colors='k', linestyles='--')
plt.xlim(0, 2)
plt.ylim(0, 1)
plt.yticks([0, 0.5, 1])
plt.xlabel('$r$')
plt.ylabel(r'Fraction grazed, $A/\pi$')
plt.show()
A curve that rises smoothly in an S shape from 0 at r = 0 to 1 at r = 2. Dashed lines from the two axes meet it at the point where r = 1.1587 and the fraction grazed is 0.5, which is marked and labelled r = 1.1587.
The fraction of the paddock that the goat can graze, \(A/\pi\), as a function of the rope length, \(r\).

Contour integrals

Ullisch's formula may be evaluated as follows. Writing \(z = \pi - \alpha\), which is \(2\theta\) by (1), turns (5) into \(\sin z - z\cos z = \pi/2\), so the denominator of both integrands,

\[g(z) = \sin z - z\cos z - \frac{\pi}{2},\]

is zero at the solution, \(z_0 = \pi - \alpha \approx 1.9057\). This is the only zero of \(g\) inside the circle \(|z - 3\pi/8| = \pi/4\), and it is a simple zero, since \(g'(z_0) = z_0\sin z_0 \ne 0\), so by the residue theorem

\[\oint \frac{z}{g(z)}\,dz = \frac{2\pi i\,z_0}{g'(z_0)} \quad \text{and} \quad \oint \frac{1}{g(z)}\,dz = \frac{2\pi i}{g'(z_0)}.\]

The ratio of the integrals is therefore \(z_0 = 2\theta\), and Ullisch's formula is relation (4), \(r = 2\cos\theta\), with \(2\theta\) written as a ratio of contour integrals. This ratio cannot be found symbolically (SymPy's solve function has no method for the equation \(g(z) = 0\), and so the integrals cannot be evaluated by residues either), but it is readily evaluated numerically. On the circle, \(z = 3\pi/8 + (\pi/4)e^{i\phi}\) and \(dz = i(\pi/4)e^{i\phi}\,d\phi\), which makes each integral one of a smooth, periodic function of \(\phi\) over \(0 \le \phi < 2\pi\). The trapezoidal rule converges geometrically (its error falls by a constant factor with each point added) for such integrals [L. N. Trefethen and J. A. C. Weideman, SIAM Rev. 56, 385–458 (2014), doi:10.1137/130932132], and here 16 points give \(r\) to seven significant figures and 32 points to 15:

python
c, R = 3 * np.pi / 8, np.pi / 4  # the centre and radius of the contour
N = 32
phi = np.linspace(0, 2 * np.pi, N, endpoint=False)
z = c + R * np.exp(1j * phi)
dz = 1j * R * np.exp(1j * phi) * (2 * np.pi / N)
g = np.sin(z) - z * np.cos(z) - np.pi / 2

ratio = np.sum(z / g * dz) / np.sum(dz / g)
print(ratio)
print(2 * np.cos(ratio.real / 2))
Output
(1.9056957293098822+4.605279462319879e-17j)
1.1587284730181229

The real part of the ratio is \(\pi - \alpha\) and its imaginary part is rounding error; the rope length differs from the Newton–Raphson solution above only in the last two digits printed, which are also rounding error.

Comments

Leave a comment

Optional, and never shown: only the moderator sees it.

Comments are written in Markdown (code between ``` lines) and appear once approved. Your name and comment are published; your email address, if you give one, is seen only by the moderator.