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.
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 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
| Symbol | Meaning | Value | Units |
|---|---|---|---|
| Mass displacements | Optimization variables | mm | |
| First spring stiffness | 1000 | N/mm | |
| Second spring stiffness | 10 | N/mm | |
| Third spring stiffness | 500 | N/mm | |
| Force on mass three | 50 | N | |
| Total potential energy | Objective | N·mm | |
| Unloaded starting configuration | mm |
Differentiating with respect to each displacement gives
The gradient components have units of force. The squared norm sums the squared components; it is not the square of their sum.
Equilibrium as a reference solution
The quadratic energy can be written as
Positive spring stiffnesses make the energy strictly convex. Setting its gradient to zero gives . Each spring carries the same applied force:
The displayed 3D topology is an energy-elevation surface over a two-displacement slice, with 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:
A golden-section line search approximates the minimizing step on a bracketed, one-dimensional function . 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:
An exact line search satisfies . 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
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 . For ,
The coefficient is dimensionless and weights how much of the previous direction is retained.
Polak–Ribière
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 mm, rather than its separately specified unloaded start . Keeping those cases distinct makes the supplied gradient and update numbers reproducible.
An exact quadratic line search gives
Step 9 — recompute the gradient
Check the line search using , allowing for rounding.
Steps 10 and 11 — norms and beta
For Polak–Ribière,
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
Step 13 — check descent
The new direction is descending. Repeat the line search along 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
Comments (3)
Demo discussion
Camilo@lazypanda772
How much would changing the spring stiffness ratios affect that zigzagging compared with the conjugate-gradient paths?

Jonathan@smalllion429
The line-search choices deserve attention too; they can influence how fair the race between methods is.
Nalan@yellowladybug179
A three-mass equilibrium makes the zigzagging of steepest descent easier to picture.