5.6.1 · D3Machine Learning (Aerospace Applications)

Worked examples — Linear regression — normal equation, gradient descent derivation

2,500 words11 min readBack to topic

This deep dive is the hands-on companion to the parent topic. There we derived the two ways to fit a line. Here we run those machines on every kind of input you could meet — clean data, perfect fits, broken (singular) data, badly scaled data, real-world word problems, and an exam twist.

This page is self-contained: everything we use is defined right here first.

Recall The two formulas we will hammer

Closed form (one shot): . Iterative: . Here is the learning rate (step size). The appears because the gradient of the average cost carries that same — we step against that averaged slope.


The scenario matrix

A line-fitting problem can be thrown at you in a limited number of flavours. Here is the full menu. Every flavour gets at least one fully worked example below.

Cell Case class What makes it special Example
A Positive slope, imperfect fit ordinary case, errors don't cancel Ex 1
B Perfectly collinear points error is exactly zero; fit passes through all Ex 2
C Negative slope sign of is negative — catches sign slips Ex 3
D Zero slope (flat) best line is horizontal, Ex 4
E Singular / degenerate inverse does not exist (all equal) Ex 5
F Gradient descent, step-by-step iterative path, learning rate behaviour Ex 6
G Learning rate too large (divergence) limiting behaviour of Ex 7
H Badly scaled features why Feature Scaling matters Ex 8
I Real-world word problem (aerospace) units, interpretation Ex 9
J Exam twist: proof/geometry orthogonality of the residual Ex 10

Example 1 — Cell A: positive slope, imperfect fit

Forecast: the points rise left-to-right, so guess and a small positive . Write your guess down before reading on.

  1. Build and . Why this step? The first column of s lets the bias live inside , so one formula handles both knobs.

  2. Form and . Why this step? The normal equation needs exactly these two objects. collapses all the inputs into a summary (row·column sums: top-left is , bottom-right is ); pairs each input column with the answers (, and ).

  3. Invert the . Determinant . Why this step? To isolate we must undo the multiply-by-; for a we swap the diagonal, negate the off-diagonal, divide by the determinant.

  4. Multiply out. So .

Verify: , , . Residuals sum to — exactly what a least-squares fit forces. Slope matched our forecast. ✓

Figure — read it like this: the black dots are the three data points, the red line is the fitted , and each dashed vertical segment is a residual (how far a dot sits above or below the red line). Notice the middle dot is above the line while the outer two are below — that visual balance is the residuals summing to zero.

Figure — Linear regression — normal equation, gradient descent derivation

Example 2 — Cell B: perfectly collinear data

Forecast: if the points are already on a line, the "best" line is that line, and the total error should be exactly zero.

  1. Build. , .
  2. Summaries. , . Why this step? Same normal-equation recipe as Ex 1 — top-left , bottom-right ; nothing about "perfect data" changes the procedure.
  3. Determinant , so Why this step? Same isolate- reason as Ex 1: invert to undo the multiply.
  4. Solve. . So .

Verify: gives exactly — residual vector is , cost . The fit is exact, matching the forecast. ✓


Example 3 — Cell C: negative slope

Forecast: downhill data → . Each step of in drops by , so expect , .

  1. Build. , .
  2. Summaries. (same 's as Ex 1!), . Why this step? depends only on the inputs, so we reuse Ex 1's inverse; only changes (, ).
  3. Solve. . So .

Verify: : an exact fit, and confirms the negative slope our eyes predicted. ✓ Lesson: a negative answer is not an error — the sign encodes direction.


Example 4 — Cell D: zero slope (flat line)

Forecast: ignores entirely, so the best line is flat: , .

  1. Summaries. , . Why this step? The 's are the same as Ex 1 so is unchanged; sums the answers against each column (, ). We build these two because the normal equation is fed exactly them.
  2. Solve. . Why this step? The gradient with respect to is zero only when the slope contributes nothing — the algebra hands us exactly .

Verify: everywhere, residuals all . A flat target genuinely produces a flat fit. ✓


Example 5 — Cell E: singular / degenerate input

Forecast: all points sit on a vertical line . A function cannot be vertical. Expect the machine to break — the inverse should not exist.

  1. Build. , .
  2. Summaries. . Why this step? Same recipe; note the -column is exactly the -column, a warning sign we'll feel in the next step.
  3. Determinant. . Why this step? A zero determinant means the matrix has no inverse — the normal equation formula is undefined. This is multicollinearity: the bias column and the -column carry the same information (one is the other), so we cannot separate from .

Verify: the determinant is literally , confirming the forecast. Fixes: drop the redundant column, or add a tiny penalty — see Regularization (Ridge, Lasso), which replaces with so the determinant is never zero. ✓

Figure — read it like this: the red vertical line is where all three black dots live (). No sloped-or-flat line of the form can trace a vertical — that impossibility is exactly the zero determinant expressed as a picture.

Figure — Linear regression — normal equation, gradient descent derivation

Example 6 — Cell F: gradient descent, two honest steps

Forecast: starting from zero, the first gradient is large and negative (predictions are far below the data), so jumps up. Expect it to crawl toward the true answer from Ex 1.

  1. Iteration 0 — predict then measure error. , so error . Why this step? The gradient needs the error first — we must know how wrong we are before deciding which way to step.
  2. Gradient.
  3. Update. . Why this step? We step against the gradient (downhill on the cost surface); scales how far.
  4. Iteration 1. , error . Gradient : first component ; second component . Update . Why this step? Exactly the same rule repeated — descent is just "measure error, step downhill" over and over.

Verify: . Each step moves toward the closed-form target — exactly the crawl we forecast. ✓

Figure — read it like this: the axes are the two knobs ( horizontal, vertical). The black path hops , and the red star is the exact answer. See how each hop shrinks and bends toward the star — that is convergence.

Figure — Linear regression — normal equation, gradient descent derivation

Example 7 — Cell G: learning rate too large

Forecast: a giant step overshoots the valley and lands further up the other wall. The cost should get worse, and repeated steps blow up.

  1. One update. . Why this step? multiplies the step; the sign is still correct (downhill), but the magnitude is far too big.
  2. Check the cost. Using with : . With : , residuals , . Why this step? Comparing before and after tells us if the step helped. Here it exploded.

Verify: — cost rose ~100×, confirming divergence. Rule of thumb: if increases, halve . Convex Optimization guarantees a valley exists; too-big steps just can't find it. ✓


Example 8 — Cell H: badly scaled features

Forecast: the metre-feature's gradient component will dwarf the fraction's, so one knob races while the other barely moves — the descent zig-zags.

  1. Gradient component per feature. For one point, . Why this step? The gradient is the error times the feature value; a big feature value makes a big component.
  2. Ratio. . Why this step? The two knobs feel forces differing by a factor of ; any single is wrong for one of them.

Verify: the component ratio matches the feature-magnitude ratio exactly — proof the imbalance comes purely from scale. Fix: standardise features first — see Feature Scaling. After scaling, both components are comparable and descent goes straight downhill. ✓


Example 9 — Cell I: aerospace word problem

Forecast: we already fit this data (Ex 1): . At expect kg/s.

  1. Reuse the fit. , . Why this step? Calibration = fit once, then use the line as a lookup for new readings.
  2. Predict at . . Why this step? We extrapolate one volt beyond the data — allowed because the physical relation is linear here.

Verify: units check — slope is kg/s per volt, times volts gives kg/s; bias is kg/s; sum is kg/s. ✓ Answer kg/s matches the forecast. (In practice we'd flag as slightly outside the V calibration range.)


Example 10 — Cell J: exam twist (geometry proof)

Forecast: the normal equation was born from the condition , so this should hold exactly — geometry, not luck.

  1. Compute residuals. From Ex 1, , so . Why this step? Perpendicularity is a statement about , so we need in numbers first.
  2. Dot with column 1 (the s). . Why this step? Dotting with the all-ones column tests perpendicularity to the bias direction — geometrically it says the residuals must sum to zero (no leftover vertical offset the bias could still absorb).
  3. Dot with column 2 (the 's). . Why this step? Dotting with the -column tests perpendicularity to the slope direction — it says the errors carry no leftover linear trend the slope could still exploit. Both dots being zero means , completing the proof.

Verify: both dot products are , so . Geometrically, is the shadow (orthogonal projection) of onto the plane spanned by 's columns; the leftover points straight out of that plane. This is the heart of Least Squares Estimation. ✓

Figure — read it like this: the black horizontal arrow lies inside the column space (drawn as the black line); the tall black arrow is ; the red arrow is the residual joining the tip of up to . The little square shows the red arrow meets the column space at a right angle — that right angle is .

Figure — Linear regression — normal equation, gradient descent derivation

Recall Self-test

Which cell has no inverse and why? ::: Cell E — all equal makes (bias and feature columns are proportional). If gradient descent's cost rises, what do you do? ::: Halve the learning rate (Cell G). What does a negative mean? ::: Downhill data — decreases as increases (Cell C). Why must residuals be orthogonal to 's columns? ::: Otherwise a leftover trend could still be removed, so the fit wasn't optimal (Cell J).