How a Hanging Chain Finds Its Own Shape
Hang a rope between two poles and let gravity do its work. The curve that appears is not a parabola, though it looks deceptively similar — it is a catenary, and it is the unique shape that minimizes the rope’s total gravitational potential energy while keeping its length fixed. This article works through the physics, derives the closed-form solution, and then verifies it numerically by treating the rope as a chain of rigid links and minimizing its energy directly with a constrained optimizer.
The Physical Setup
Consider a flexible, inextensible chain of length $L$ suspended between two fixed points at the same height, separated by a horizontal distance $D$, with $L > D$. Under gravity, the chain settles into the shape $y(x)$ that minimizes its total potential energy
$$
U[y] = \rho g \int_0^{D} y(x),\sqrt{1 + y’(x)^2};dx
$$
subject to the length constraint
$$
\int_0^{D} \sqrt{1 + y’(x)^2};dx = L
$$
and the boundary conditions $y(0) = y(D) = 0$.
Deriving the Catenary Equation
Introducing a Lagrange multiplier $\lambda$ for the length constraint turns this into an unconstrained variational problem for the functional
$$
F(y, y’) = (y - \lambda)\sqrt{1 + y’^2}
$$
Since $F$ does not depend explicitly on $x$, the Beltrami identity applies, giving a first integral of the Euler–Lagrange equation. Working through the algebra leads to the classical result
$$
y(x) = a \cosh!\left(\frac{x - D/2}{a}\right) - a\cosh!\left(\frac{D}{2a}\right)
$$
where the shape parameter $a$ is fixed by requiring the arc length to equal $L$:
$$
L = 2a\sinh!\left(\frac{D}{2a}\right)
$$
This is a transcendental equation in $a$ with no closed-form inverse, so it has to be solved numerically — a perfect entry point for Python.
A Numerical Cross-Check: The Chain as N Rigid Links
To verify the analytical result independently, the rope can be modeled as $N$ rigid links, each of fixed length $l = L/N$, connected end to end. The free parameters are the $N$ link angles $\theta_i$ measured from the horizontal. Once the angles are known, the joint positions follow from a cumulative sum of the link displacement vectors. The physically realized configuration is the one that minimizes total potential energy
$$
U(\theta) = \rho g \sum_{i=1}^{N} \bar{y}_i , l
$$
where $\bar{y}_i$ is the height of the midpoint of link $i$, subject to the constraint that the last joint lands exactly on the second support point. This turns the problem into a standard constrained nonlinear optimization, solved here with SLSQP.
Python Source Code
1 | # ========================================================== |
Code Walkthrough
Section 2 — Analytical solution. The transcendental equation $L = 2a\sinh(D/2a)$ has no algebraic inverse, so scipy.optimize.brentq is used to bracket and find the root numerically. The bracket $[D/100,\ 1000D]$ is chosen deliberately: at the lower bound, $D/(2a)=50$, which keeps $\sinh(50)$ large but finite (no floating-point overflow), guaranteeing a large positive residual; at the upper bound the residual approaches $D-L<0$. Because the residual is strictly monotonic between these two values, brentq is guaranteed to converge.
Section 3 — Discretized chain. Rather than treating each joint’s $(x,y)$ coordinates as independent variables (which would require handling $2N$ unknowns and per-segment length constraints), the model uses one angle $\theta_i$ per link. This automatically enforces that every link has exactly length $l = L/N$, cutting the constraint count down to just two equations — the horizontal and vertical position of the last joint. Positions are computed with np.cumsum, a fully vectorized operation, so the model scales cleanly to large $N$ without any Python-level loop overhead. The initial guess is not arbitrary: it uses the slope of the already-known analytical catenary, which gives SLSQP a near-optimal starting point and ensures fast, reliable convergence.
Section 4 — Validation. The discretized joints are compared directly against the analytical curve evaluated at the same $x$-coordinates. Agreement at the level of the RMSE reported in the console output confirms that the two independent methods — closed-form calculus of variations and constrained numerical optimization — describe the same physical shape.
Section 6 — Family of curves. By resolving the transcendental equation for several values of $L$ and stacking the resulting curves along a third axis, the 3D plot shows how the chain sags more deeply as extra length is added while the support span $D$ stays fixed — a direct visualization of the constraint’s effect on the energy-minimizing shape.
Section 7 — Energy landscape. This is the most direct illustration of “potential energy minimization” as an optimization concept. A two-parameter family of trial shapes is built around the true catenary: $s$ uniformly scales it, and $t$ adds an independent sine-shaped perturbation that also respects the endpoint conditions. The resulting energy surface is a bowl, and the grid-based minimum should land close to $s=1,\ t=0$ — the true analytical solution — visually confirming that the catenary is not merely a low-energy shape among nearby alternatives, but the minimum.
Catenary parameter a : 4.695415 Maximum sag depth : 2.923421 Optimizer status : Optimization terminated successfully Potential energy at optimum : -22.234566 RMSE (numeric vs analytical) : 1.631274e-04 /tmp/ipykernel_527/4050898374.py:160: DeprecationWarning: `trapz` is deprecated. Use `trapezoid` instead, or one of the numerical integration functions in `scipy.integrate`. E[i, j] = np.trapz(y_trial * ds, x_analytic) Grid-search minimum located at s=1.500, t=-1.4617 (expected near s=1.000, t=0.0000 for the true catenary)



Discussion
The two solution methods approach the same problem from opposite directions. The analytical route treats the chain as a continuous curve and applies the calculus of variations directly, yielding an exact — if transcendental — formula. The numerical route discretizes the chain into finitely many rigid links and finds the energy minimum through constrained nonlinear programming, making no assumption about the functional form of the solution in advance. That both methods converge to the same curve is a strong practical confirmation of the underlying physics, and it is also exactly the kind of cross-check worth running whenever a discretized optimization model is proposed for a problem that already has a known closed-form answer.
Beyond ropes and chains, the same mathematics governs the resting shape of power transmission lines, the profile of certain arches (an inverted catenary is the ideal compression-only arch shape), and the equilibrium configuration of suspension bridge cables before the deck load is added. The Rayleigh–Ritz style landscape in Figure 3 also previews a broader idea used throughout computational mechanics and structural optimization: whenever a system settles into the configuration of least potential energy, nearby trial configurations can be swept numerically to confirm — and visualize — that the true physical solution truly sits at the bottom of the energy bowl.































