Optimization · Module 5 of 10
Nonlinear Least Squares
Fitting a model to data is optimization with a special structure. The objective is a sum of squared residuals, and that structure lets Gauss-Newton approximate the Hessian for free, from the Jacobian alone.
Readiness check
This module fits models by minimising residuals. Tick only what you can do closed-notes.
- Recall a residual as data minus model.
- Solve the normal equations for a straight-line fit.
- Form a Jacobian of partial derivatives.
- Recall the Newton step from Module 4.
- Multiply a matrix transpose by a vector.
The core idea
A least-squares problem minimises half the sum of squared residuals. Its gradient is JTr and its Hessian is well approximated by JTJ, so Gauss-Newton solves JTJ Δ = −JTr, using only first derivatives of the model.
minimise ½ Σ ri(x)2 = ½ ‖r(x)‖2∇f = JTr, ∇2f ≈ JTJGauss-Newton: JTJ Δ = −JTrModel fitting is the most common optimization in engineering: choose parameters x so a model best matches data. The residuals ri(x) are the mismatches, and the objective is f(x) = ½‖r(x)‖2, half the sum of their squares. This structure is special. The gradient is ∇f = JTr, where J is the Jacobian of the residuals (their partial derivatives with respect to the parameters), and the Hessian is JTJ plus a term involving second derivatives of the residuals. When the residuals are small or nearly linear, that second term is negligible, so JTJ is an excellent Hessian approximation, available from first derivatives alone. The Gauss-Newton method exploits this, solving JTJ Δ = −JTr for the parameter step. For a linear model the Jacobian is constant and this is exactly the normal equations, solved in one step. When Gauss-Newton takes too bold a step, Levenberg-Marquardt adds a damping term (JTJ + λI), blending it toward steepest descent for robustness.
The skills, taught in order
Five skills build the least-squares objective, its Gauss-Newton solution, and the Levenberg-Marquardt safeguard.
5.1 The least-squares objective
Given residuals ri(x) = model(x) − data, the objective is f(x) = ½Σri2. Minimising it finds the parameters that best fit the data in the least-squares sense. The factor of one half makes the gradient clean.
5.2 The gradient and the Gauss-Newton Hessian
The gradient is ∇f = JTr, where J holds the residual derivatives. The true Hessian is JTJ plus a residual-curvature term; dropping that term gives the Gauss-Newton approximation ∇2f ≈ JTJ, positive semidefinite and free from second derivatives.
| Quantity | Expression | Role |
|---|---|---|
| Objective | ½‖r‖2 | sum of squared residuals |
| Gradient | JTr | zero at the optimum |
| Hessian (approx) | JTJ | Gauss-Newton curvature |
| Step | −(JTJ)−1JTr | Gauss-Newton update |
The special structure of least squares: the Jacobian alone gives both the gradient and an approximate Hessian.
5.3 The Gauss-Newton method
Gauss-Newton solves JTJ Δ = −JTr for the parameter step Δ, then updates x. It is Newton's method with the cheap JTJ Hessian, converging fast when residuals are small. For a linear model J is constant and the step reaches the exact least-squares solution at once.
5.4 Levenberg-Marquardt damping
When Gauss-Newton overshoots, Levenberg-Marquardt solves (JTJ + λI) Δ = −JTr. A small λ recovers Gauss-Newton's speed; a large λ shrinks the step toward steepest descent for safety. Adapting λ each iteration gives a robust, widely used fitter.
5.5 Model fitting in practice
Nonlinear least squares calibrates material laws, sensor curves, and dynamic models to test data. The residuals encode the fit, the Jacobian the sensitivities, and the converged parameters, with their uncertainties, are the engineering result.
Engineering connection: curve-fitting a stress-strain law or a heat-transfer correlation to measured data is a nonlinear least-squares problem solved exactly this way.
Worked example 1: a linear least-squares fit
Fit the line y = a + bx to the three points (0, 1), (1, 3), (2, 4) by minimising the sum of squared residuals. Find a, b, and the residual sum of squares.
- ProblemFind the least-squares line for the data in Figure 1.
- Given / findPoints (0, 1), (1, 3), (2, 4). Find a, b, and the residual sum of squares.
- AssumptionsErrors in y only; the model is linear, so the Jacobian is constant.
- ModelMinimise ½Σ(a + bxi − yi)2; the normal equations give b = (nΣxy − ΣxΣy)/(nΣx2 − (Σx)2), a = ȳ − bx̄.
- Equationsn = 3, Σx = 3, Σy = 8, Σxy = 11, Σx2 = 5
- Solveb = (3·11 − 3·8)/(3·5 − 9) = (33 − 24)/6 = 1.5. a = 8/3 − 1.5·1 = 1.167. Residuals: −0.167, 0.333, −0.167; sum of squares = 0.167.
- CheckThe gradient JTr is zero here: the residuals sum to zero and are orthogonal to the x-values, the defining condition of the least-squares optimum.
- ConclusionThe best-fit line is y = 1.167 + 1.5x with residual sum of squares 0.167. A linear model is solved in one step by the normal equations.
Worked example 2: a Gauss-Newton step
Fit the one-parameter model y = a·x to the points (1, 2), (2, 3), (3, 5). Take one Gauss-Newton step from a = 1.5 and confirm it reaches the exact least-squares value.
- ProblemTake one Gauss-Newton step for the model in Figure 2 and compare to the exact fit.
- Given / findModel y = a·x, points (1, 2), (2, 3), (3, 5), start a = 1.5. Find the step and the new a.
- AssumptionsResiduals ri = a xi − yi; Jacobian Ji = xi, constant in a.
- ModelGauss-Newton: Δa = −JTr / JTJ; anew = a + Δa.
- Equationsr = (1.5·1 − 2, 1.5·2 − 3, 1.5·3 − 5) = (−0.5, 0, −0.5)JTJ = 1 + 4 + 9 = 14, JTr = −0.5 − 1.5 = −2
- SolveΔa = −(−2)/14 = 0.143. anew = 1.5 + 0.143 = 1.643. The exact least-squares value is Σxy/Σx2 = 23/14 = 1.643.
- CheckThe one Gauss-Newton step lands exactly on the least-squares solution, because the model is linear in a, so J is constant and JTJ is the exact Hessian.
- ConclusionGauss-Newton reaches the exact fit in one step here, the least-squares analogue of Newton solving a quadratic. For a nonlinear model it would take several damped steps.
Misconceptions and diagnostics
| Mistake | Symptom | Diagnostic question | Correction |
|---|---|---|---|
| Using the full Hessian | Needless second derivatives | "Are the residuals small?" | Gauss-Newton's JTJ suffices when they are. |
| Gauss-Newton on large residuals | Steps overshoot or diverge | "Is JTJ a good Hessian here?" | Add Levenberg-Marquardt damping (JTJ + λI). |
| Confusing residual with error | Fit quality misjudged | "Is r data minus model?" | Residual is the model-to-data mismatch, not the parameter error. |
| Ignoring the Jacobian structure | Treating fitting as generic optimization | "Did I exploit ½‖r‖2?" | Use JTr and JTJ from the least-squares form. |
Practice ladder
For residuals r = (0.2, −0.1, 0.3) and Jacobian J = (1, 1, 1), find JTr.
Show answer
JTr = 0.2 − 0.1 + 0.3 = 0.4.
With that J, find JTJ and the Gauss-Newton step for a single parameter.
Show answer
JTJ = 3. Δ = −JTr/JTJ = −0.4/3 = −0.133.
Fit y = a·x to (1, 3), (2, 5), (3, 8) by the exact least-squares formula.
Show answer
a = Σxy/Σx2 = (3 + 10 + 24)/(1 + 4 + 9) = 37/14 = 2.643.
You fit a nonlinear material law to test data and Gauss-Newton diverges. Explain what is likely wrong and how Levenberg-Marquardt helps.
What good work looks like
Large or highly nonlinear residuals make JTJ a poor Hessian, so the Gauss-Newton step overshoots. Levenberg-Marquardt adds λI to damp the step toward steepest descent, restoring convergence, then relaxes λ as the fit improves.
Working with AI, and proving it yourself
Use AI as an examiner, not a solver
Portfolio task
Fit a real model to measured data by Gauss-Newton or Levenberg-Marquardt, report the converged parameters, and confirm the gradient JTr is near zero.
Retrieval and spaced review
Closed notes. Answer out loud, then reveal.
1. Write the least-squares objective.
f(x) = ½‖r(x)‖2, half the sum of squared residuals.
2. Give its gradient and approximate Hessian.
∇f = JTr and ∇2f ≈ JTJ.
3. Write the Gauss-Newton system.
JTJ Δ = −JTr.
4. What does Levenberg-Marquardt add?
A damping term: (JTJ + λI) Δ = −JTr.
5. Why does a linear model converge in one step?
Its Jacobian is constant, so JTJ is the exact Hessian.
Textbook mapping
This module follows Nocedal and Wright, Numerical Optimization, 2nd edition. Use these references to read further.
| Topic in this module | Where to read more |
|---|---|
| The least-squares objective | Nocedal & Wright, Chapter 10 |
| The Gauss-Newton method | Nocedal & Wright, Chapter 10 |
| Levenberg-Marquardt | Nocedal & Wright, Chapter 10 |
Chapter numbers refer to the 2nd edition. The least-squares methods are standard, so any recent edition will align closely.