Skip to content

Having Fun With Conjugate Gradient Descent On A Spring–Mass System

Steepest descent, Fletcher–Reeves and Polak–Ribière race to the same equilibrium of a three-mass system. Only one of them takes the scenic route.

Nov 5, 202612 min readTime O(n·k)Space O(n)

Spring displacement

The mechanical system

Three masses are connected in series by three springs. The first spring is attached to a fixed support, and an external force FF acts on mass three. At equilibrium, the displacements minimize total potential energy: stored elastic energy minus the work of the applied force.

The supplied study compares steepest descent, Fletcher–Reeves conjugate gradient, and Polak–Ribière conjugate gradient, all aimed at the same equilibrium.

Energy, gradient, and units

Π(u)=12k1u12+12k2(u2−u1)2+12k3(u3−u2)2−Fu3.\Pi(\mathbf u)=\frac12k_1u_1^2+\frac12k_2(u_2-u_1)^2+\frac12k_3(u_3-u_2)^2-Fu_3.
SymbolMeaningValueUnits
u1,u2,u3u_1,u_2,u_3Mass displacementsOptimization variablesmm
k1k_1First spring stiffness1000N/mm
k2k_2Second spring stiffness10N/mm
k3k_3Third spring stiffness500N/mm
FFForce on mass three50N
Π\PiTotal potential energyObjectiveN·mm
u0\mathbf u_0Unloaded starting configuration(0,0,0)(0,0,0)mm

Differentiating with respect to each displacement gives

∇Π(u)=[k1u1−k2(u2−u1)k2(u2−u1)−k3(u3−u2)k3(u3−u2)−F].\nabla\Pi(\mathbf u)=\begin{bmatrix} k_1u_1-k_2(u_2-u_1)\\ k_2(u_2-u_1)-k_3(u_3-u_2)\\ k_3(u_3-u_2)-F \end{bmatrix}.

The gradient components have units of force. The squared norm sums the squared components; it is not the square of their sum.

aTb=∥a∥ ∥b∥cos⁡θ,∥g∥2=gTg.\mathbf a^T\mathbf b=\|\mathbf a\|\,\|\mathbf b\|\cos\theta,\qquad\|\mathbf g\|^2=\mathbf g^T\mathbf g.

Equilibrium as a reference solution

The quadratic energy can be written as

Π(u)=12uTAu−bTu,A=[1010−100−10510−5000−500500],b=[0050].\Pi(\mathbf u)=\tfrac12\mathbf u^T A\mathbf u-\mathbf b^T\mathbf u,\qquad A=\begin{bmatrix}1010&-10&0\\-10&510&-500\\0&-500&500\end{bmatrix},\qquad \mathbf b=\begin{bmatrix}0\\0\\50\end{bmatrix}.

Positive spring stiffnesses make the energy strictly convex. Setting its gradient to zero gives Au∗=bA\mathbf u^*=\mathbf b. Each spring carries the same applied force:

u1∗=Fk1=0.05,u2∗=u1∗+Fk2=5.05,u3∗=u2∗+Fk3=5.15 mm.u_1^*=\frac{F}{k_1}=0.05,\qquad u_2^*=u_1^*+\frac{F}{k_2}=5.05,\qquad u_3^*=u_2^*+\frac{F}{k_3}=5.15\ \mathrm{mm}. ∇Π(u∗)=0,Π(u∗)=−128.75 N mm.\nabla\Pi(\mathbf u^*)=\mathbf0,\qquad\Pi(\mathbf u^*)=-128.75\ \mathrm{N\,mm}.

The displayed 3D topology is an energy-elevation surface over a two-displacement slice, with u1u_1 held at its equilibrium value. It is not a four-dimensional plot of all three independent displacements and energy.

Shared direction and line-search framework

All three methods update the design through a search direction and a scalar step:

u(k+1)=u(k)+αkd(k),αk=arg⁡min⁡α≥0Π(u(k)+αd(k)).\mathbf u^{(k+1)}=\mathbf u^{(k)}+\alpha_k\mathbf d^{(k)},\qquad\alpha_k=\arg\min_{\alpha\geq0}\Pi(\mathbf u^{(k)}+\alpha\mathbf d^{(k)}).

A golden-section line search approximates the minimizing step on a bracketed, one-dimensional function ϕk(α)=Π(u(k)+αd(k))\phi_k(\alpha)=\Pi(\mathbf u^{(k)}+\alpha\mathbf d^{(k)}). Use the same bracketing, tolerance, and stopping criterion for each method to make a numerical comparison meaningful.

For this quadratic model, an exact step is available as a reference:

αk=−gkTdkdkTAdk,gk=∇Π(u(k)).\alpha_k=-\frac{\mathbf g_k^T\mathbf d_k}{\mathbf d_k^TA\mathbf d_k},\qquad\mathbf g_k=\nabla\Pi(\mathbf u^{(k)}).

An exact line search satisfies gk+1Tdk=0\mathbf g_{k+1}^T\mathbf d_k=0. A finite-tolerance golden-section search satisfies this only approximately. The current topology’s reference path uses the analytic quadratic step, not a measured three-method golden-section comparison.

Three search-direction choices

Steepest descent

dk=−gk.\mathbf d_k=-\mathbf g_k.

This is the negative-gradient baseline. Each new direction discards the previous direction; narrow, elongated energy valleys can cause a zigzagging path.

Fletcher–Reeves

Start with d0=−g0\mathbf d_0=-\mathbf g_0. For k>0k>0,

βFR,k=∥gk∥2∥gk−1∥2,dk=−gk+βFR,kdk−1.\beta_{\mathrm{FR},k}=\frac{\|\mathbf g_k\|^2}{\|\mathbf g_{k-1}\|^2},\qquad\mathbf d_k=-\mathbf g_k+\beta_{\mathrm{FR},k}\mathbf d_{k-1}.

The coefficient is dimensionless and weights how much of the previous direction is retained.

Polak–Ribière

βPR,k=gkT(gk−gk−1)∥gk−1∥2,dk=−gk+βPR,kdk−1.\beta_{\mathrm{PR},k}=\frac{\mathbf g_k^T(\mathbf g_k-\mathbf g_{k-1})}{\|\mathbf g_{k-1}\|^2},\qquad\mathbf d_k=-\mathbf g_k+\beta_{\mathrm{PR},k}\mathbf d_{k-1}.

Unlike Fletcher–Reeves, this coefficient depends on the change in gradient. Under exact quadratic line searches, successive gradients are orthogonal and the two coefficients coincide in exact arithmetic. Approximate line searches need not preserve that identity.

Worked iteration: the supplied numerical example

The source’s numerical steps use x(0)=(10,−10,10)\mathbf x^{(0)}=(10,-10,10) mm, rather than its separately specified unloaded start (0,0,0)(0,0,0). Keeping those cases distinct makes the supplied gradient and update numbers reproducible.

g(0)=[10200−102009950],d(0)=−g(0),∥g(0)∥2=307082500.\mathbf g^{(0)}=\begin{bmatrix}10200\\-10200\\9950\end{bmatrix},\qquad\mathbf d^{(0)}=-\mathbf g^{(0)},\qquad\|\mathbf g^{(0)}\|^2=307082500.

An exact quadratic line search gives

α0≈0.0009867282151,x(1)=x(0)+α0d(0)≈[−0.064627790.064627790.18205426].\alpha_0\approx0.0009867282151,\qquad\mathbf x^{(1)}=\mathbf x^{(0)}+\alpha_0\mathbf d^{(0)}\approx\begin{bmatrix}-0.06462779\\0.06462779\\0.18205426\end{bmatrix}.

Step 9 — recompute the gradient

x2−x1≈0.12926,x3−x2≈0.11743.x_2-x_1\approx0.12926,\qquad x_3-x_2\approx0.11743. g1(1)=1000(−0.06463)−10(0.12926)≈−65.920,g2(1)=10(0.12926)−500(0.11743)≈−57.421,g3(1)=500(0.11743)−50≈8.713.\begin{aligned} g_1^{(1)}&=1000(-0.06463)-10(0.12926)\approx-65.920,\\ g_2^{(1)}&=10(0.12926)-500(0.11743)\approx-57.421,\\ g_3^{(1)}&=500(0.11743)-50\approx8.713. \end{aligned}

Check the line search using g(1)Td(0)≈0\mathbf g^{(1)T}\mathbf d^{(0)}\approx0, allowing for rounding.

Steps 10 and 11 — norms and beta

∥g(1)∥2≈(−65.920)2+(−57.421)2+(8.713)2≈7718.5.\|\mathbf g^{(1)}\|^2\approx(-65.920)^2+(-57.421)^2+(8.713)^2\approx7718.5. βFR(1)=7718.5471307082500≈2.5135093×10−5.\beta_{\mathrm{FR}}^{(1)}=\frac{7718.5471}{307082500}\approx2.5135093\times10^{-5}.

For Polak–Ribière,

βPR(1)=∥g(1)∥2−g(1)Tg(0)∥g(0)∥2≈βFR(1).\beta_{\mathrm{PR}}^{(1)}=\frac{\|\mathbf g^{(1)}\|^2-\mathbf g^{(1)T}\mathbf g^{(0)}}{\|\mathbf g^{(0)}\|^2}\approx\beta_{\mathrm{FR}}^{(1)}.

The equality here follows from gradient orthogonality in this exact quadratic step, not from a general equivalence of the nonlinear algorithms.

Step 12 — form the new direction

d(1)=−g(1)+β(1)d(0)≈[65.66457.677−8.963].\mathbf d^{(1)}=-\mathbf g^{(1)}+\beta^{(1)}\mathbf d^{(0)}\approx\begin{bmatrix}65.664\\57.677\\-8.963\end{bmatrix}.

Step 13 — check descent

g(1)Td(1)=−∥g(1)∥2+β(1)g(1)Td(0)≈−7718.5<0.\mathbf g^{(1)T}\mathbf d^{(1)}=-\|\mathbf g^{(1)}\|^2+\beta^{(1)}\mathbf g^{(1)T}\mathbf d^{(0)}\approx-7718.5<0.

The new direction is descending. Repeat the line search along x(1)+αd(1)\mathbf x^{(1)}+\alpha\mathbf d^{(1)} to obtain the next step and displacement.

Reference update loop

let u = [0, 0, 0];
let g = gradient(u);
let d = scale(g, -1);

for (let k = 0; norm(g) > tolerance && k < maxIterations; k++) {
  const alpha = goldenLineSearch(a => energy(add(u, scale(d, a))));
  const nextU = add(u, scale(d, alpha));
  const nextG = gradient(nextU);
  const denominator = dot(g, g);
  if (denominator === 0) break;
  const beta = method === 'FR'
    ? dot(nextG, nextG) / denominator
    : method === 'PR'
      ? dot(nextG, subtract(nextG, g)) / denominator
      : 0;
  d = add(scale(nextG, -1), scale(d, beta));
  u = nextU;
  g = nextG;
}

This is algorithmic pseudocode: vector operations, the line-search bracket, and its stopping tolerance must be supplied by an implementation. A practical nonlinear conjugate-gradient solver should also check for a descending direction and restart with the negative gradient when needed.

What remains to compare

The source requests a comparison of the three methods but does not provide a completed experiment. No timing or iteration leaderboard is invented here. A fair experiment should report the gradient norm, energy, displacements, step size, beta, and function evaluations with identical starting conditions and tolerances.

For the strictly quadratic three-variable problem, analytic-step linear conjugate gradient reaches equilibrium in at most three steps in exact arithmetic. That reference result must not be presented as a guarantee for finite-tolerance nonlinear methods using golden-section line searches.

Series · Numerical Optimization

  1. Part 1

    Finding The Cheapest Cutting Speed With Golden-Section Search

  2. Part 2

    Having Fun With Conjugate Gradient Descent On A Spring–Mass System

All algorithms

Comments (3)

Demo discussion
  • Nalan Poçan

    Nalan@yellowladybug179

    A three-mass equilibrium makes the zigzagging of steepest descent easier to picture.

    • Camilo Solorzano

      Camilo@lazypanda772

      How much would changing the spring stiffness ratios affect that zigzagging compared with the conjugate-gradient paths?

    • Jonathan Moreno

      Jonathan@smalllion429

      The line-search choices deserve attention too; they can influence how fair the race between methods is.