For our optimization course project, my team of three implemented the Levenberg–Marquardt (LM) algorithm from scratch, compared it with Gauss–Newton (GN), and used both to fit a nonlinear forest-growth model.
My part was the hand-worked examples: stepping through the algorithm by hand so we (and our audience at the demo) could see exactly what each iteration does. This post walks through the method the way I'd explain it to a classmate, and then the result that surprised us.
The problem: nonlinear least squares
You have data points and a model with unknown parameters. You want the parameters that make the model's predictions as close as possible to the data, which means minimising the sum of squared residuals:
If the model is linear, this has a closed-form answer. If it isn't, you iterate.
Gauss–Newton in one line
At each step, approximate the model linearly using the Jacobian J (the matrix of partial derivatives of each residual), and solve:
Then step to x + p. Near a good solution this converges very fast. Far from one, or when JᵀJ is badly conditioned, the step can be huge and in the wrong direction.
Levenberg–Marquardt adds a damping knob
LM solves almost the same equation, with one extra term:
- Small
λ: the step is basically Gauss–Newton (fast). - Large
λ: the step becomes a short step downhill along the gradient (safe).
After each trial step, LM checks whether the error went down. If it did, accept the step and decrease λ. If not, reject it and increase λ. The algorithm tunes its own caution.
Doing one step by hand
This is the kind of example I worked through for the project. Take the tiny problem of finding x with x² = 2, so the residual is r(x) = x² − 2 and the Jacobian is J(x) = 2x.
Start at x₀ = 1, with λ₀ = 10:
- Residual:
r₀ = 1² − 2 = −1, so the error norm is1.0. - Jacobian:
J₀ = 2. - Damped equation:
(2·2 + 10) p = −(2 · −1), so14p = 2andp ≈ 0.143. - Trial point:
x = 1.143, new residual1.143² − 2 ≈ −0.694. - The error dropped from
1.0to0.694, so accept and setλ₁ = λ₀ / 10 = 1.
With λ now smaller, the next step will behave more like Gauss–Newton and move faster towards √2. Working a few of these by hand made the "self-tuning" idea click for me in a way the formula alone didn't.
The real test: Chapman–Richards growth
The model we fit describes how a tree grows: slowly when young, quickly in its prime, then levelling off:
Four parameters: maximum size α, scaling β, growth rate k, and shape m.
We generated synthetic data from known "true" parameters, added noise, then ran both LM and GN from four random starting points each.
Result 1: LM was more reliable
Across all four random starts, LM reached a low error every time. GN was less consistent; in one run it stalled at a sum of squared residuals around 30, while LM got below 1.
In the best run for each:
| α | β | k | m | SSR | |
|---|---|---|---|---|---|
| True | 20.00 | 0.50 | 0.40 | 0.50 | |
| LM | 19.93 | 0.70 | 0.37 | 0.19 | 0.805 |
| GN | 19.90 | 0.36 | 0.41 | 0.63 | 1.224 |
We also saw GN's error creep upwards over its last iterations (from 1.2199 to 1.2232), because plain GN has no check that a step actually improves things. LM only accepts steps that do.
Result 2: the better fit got the parameters "wrong"
LM's fit has lower error, yet its β and m are further from the true values than GN's.
That looks like a contradiction until you plot the curves: both fits lie almost on top of the true curve. Different combinations of β and m produce nearly identical shapes over this range of t, so the data can't tell them apart. The parameters are weakly identifiable.
The lesson for me: minimising error and recovering the true parameters are different goals. If the parameters themselves matter (say, a forester wants the actual growth rate), you need more data, a wider time range, or constraints on the parameters, not just a better optimiser.
What I took away
- Working an algorithm by hand, one step at a time, is the fastest way I know to really understand it.
- Damping isn't a hack. It's what makes LM safe to start far from the answer.
- A good fit is not proof of good parameters. Check identifiability before trusting the numbers.
Team
Jack built the Chapman–Richards modelling and the experiments, Suwinya wrote the theoretical analysis and convergence discussion, and I wrote the step-by-step worked examples. The code is in Jack's repository.

