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.

01

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.
0 or 1 weak itemsContinue with this module.
2 weak itemsRevisit least-squares regression in Numerical Methods, Module 6.
3 or more weak itemsRevisit the Newton step in Module 4.
02

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 Δ = −JTr

Model 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 skill works when: you form the residuals and Jacobian and solve the Gauss-Newton system for the step.
The skill breaks down when: residuals are large and nonlinear, so JTJ is a poor Hessian and damping is needed.
The concept. Each residual is the vertical gap between a data point and the model. Least squares tunes the parameters to make the sum of those gaps squared as small as possible.
03

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.

QuantityExpressionRole
Objective½‖r‖2sum of squared residuals
GradientJTrzero at the optimum
Hessian (approx)JTJGauss-Newton curvature
Step−(JTJ)−1JTrGauss-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.

04

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.

Figure 1. The least-squares line balances the three residuals. For a linear model the normal equations give the parameters directly, no iteration needed.
  1. ProblemFind the least-squares line for the data in Figure 1.
  2. Given / findPoints (0, 1), (1, 3), (2, 4). Find a, b, and the residual sum of squares.
  3. AssumptionsErrors in y only; the model is linear, so the Jacobian is constant.
  4. ModelMinimise ½Σ(a + bxi − yi)2; the normal equations give b = (nΣxy − ΣxΣy)/(nΣx2 − (Σx)2), a = ȳ − bx̄.
  5. Equationsn = 3, Σx = 3, Σy = 8, Σxy = 11, Σx2 = 5
  6. 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.
  7. 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.
  8. 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.
Result. a = 1.167, b = 1.5, residual sum of squares 0.167.
05

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.

Figure 2. With a constant Jacobian, the Gauss-Newton step from any start reaches the exact least-squares parameter in one move, mirroring Newton on a quadratic.
  1. ProblemTake one Gauss-Newton step for the model in Figure 2 and compare to the exact fit.
  2. Given / findModel y = a·x, points (1, 2), (2, 3), (3, 5), start a = 1.5. Find the step and the new a.
  3. AssumptionsResiduals ri = a xi − yi; Jacobian Ji = xi, constant in a.
  4. ModelGauss-Newton: Δa = −JTr / JTJ; anew = a + Δa.
  5. 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
  6. 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.
  7. 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.
  8. 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.
Result. Δa = 0.143, a = 1.643, the exact least-squares value.
06

Misconceptions and diagnostics

MistakeSymptomDiagnostic questionCorrection
Using the full HessianNeedless second derivatives"Are the residuals small?"Gauss-Newton's JTJ suffices when they are.
Gauss-Newton on large residualsSteps overshoot or diverge"Is JTJ a good Hessian here?"Add Levenberg-Marquardt damping (JTJ + λI).
Confusing residual with errorFit quality misjudged"Is r data minus model?"Residual is the model-to-data mismatch, not the parameter error.
Ignoring the Jacobian structureTreating fitting as generic optimization"Did I exploit ½‖r‖2?"Use JTr and JTJ from the least-squares form.
07

Practice ladder

Level 1 · Direct skill

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.

Level 2 · Mixed concept

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.

Level 3 · Independent problem

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.

Transfer task | Real engineering

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.

08

Working with AI, and proving it yourself

Use AI as an examiner, not a solver

"Check that my residuals are orthogonal to the Jacobian columns at the fit."
"Give me three data sets; I will set up the Gauss-Newton step for each."
"Fit the model for me." Forming J and r and solving the step is the skill.
"Why did it diverge?" Reasoning about residual size and damping is the point.

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.

Must include: the residuals, the Jacobian, the converged parameters, and a gradient check.
09

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.

TodayFinish this quiz and Levels 1 and 2 of the ladder.
+1 dayRe-derive the Gauss-Newton step from a blank page.
+3 daysFit three new models by least squares.
+7 daysAdd constraints to the problem, Module 6.
+30 daysReuse least squares to calibrate a real model to data.
10

Textbook mapping

This module follows Nocedal and Wright, Numerical Optimization, 2nd edition. Use these references to read further.

Topic in this moduleWhere to read more
The least-squares objectiveNocedal & Wright, Chapter 10
The Gauss-Newton methodNocedal & Wright, Chapter 10
Levenberg-MarquardtNocedal & Wright, Chapter 10

Chapter numbers refer to the 2nd edition. The least-squares methods are standard, so any recent edition will align closely.