Euler Hall — free maths guides

Compute the residual b - Ax

Compute b - Ax entry by entry and say what each entry means. The idea in plain words, a worked example, and a check with the answers shown — part of the free Linear Algebra guides.

The definition, entry by entry

For a matrixAA, a vectorxx and a right-hand sidebb, the residual is r=b−Ax.r=b-Ax. Entryii is ri=bi−(Ax)i=bi−(ai1x1+⋯+ainxn),r_i=b_i-(Ax)_i=b_i-\big(a_{i1}x_1+\cdots+a_{in}x_n\big), so each entry is data minus model for that one measurement. The residual is a vector of the same length asbb, and it says how much of each entry the model failed to produce.

Sign convention.r=b−Axr=b-Ax, neverAx−bAx-b. The residual is what is left of the right-hand side after the model has explained its part, and it carries the sign that makes b=Ax+rb=Ax+r rebuildbb.

Three readings of the same vector.

| observation | conclusion | |---|---| |r=0r=\mathbf{0} |bb is in the column space; the system is consistent | |r≠0r\ne\mathbf{0} butATr=0A^Tr=\mathbf{0} |xx is the least-squares solution | |r≠0r\ne\mathbf{0} andATr≠0A^Tr\ne\mathbf{0} |xx is neither a solution nor the best fit |

The middle row is the one worth memorising: the least-squares residual is perpendicular to every column.

Why the residual is perpendicular to the columns at the optimum

The condition is worth deriving rather than memorising, because it is the same argument as projection.

MinimiseE(x)=∥Ax−b∥2E(x)=\|Ax-b\|^2. Expand: E(x)=(Ax−b)T(Ax−b)=xTATAx−2xTATb+bTb.E(x)=(Ax-b)^T(Ax-b)=x^TA^TAx-2x^TA^Tb+b^Tb. The gradient with respect toxx is2ATAx−2ATb2A^TAx-2A^Tb, and setting it to zero gives ATAx=ATb,A^TAx=A^Tb, which is exactlyAT(b−Ax)=0A^T(b-Ax)=\mathbf{0}, i.e.ATr=0A^Tr=\mathbf{0}.

What that says geometrically. The residual is perpendicular to every column ofAA, hence to the whole column space. Among all pointsAxAx reachable by the model, the one closest tobb is the one whose error sticks out perpendicularly — the foot of the perpendicular.

Why the sign never matters for the minimum.∥Ax−b∥=∥b−Ax∥\|Ax-b\|=\|b-Ax\|, so the sum of squares is blind to the convention. What is not blind is the interpretation: withr=b−Axr=b-Ax, a positive entry means the model under-predicted that measurement, and a plot drawn fromAx−bAx-b is a mirror image. The sign matters for reading the residual, not for finding the optimum, which is exactly why it is easy to get wrong and never notice.

And why a zero residual is special. Ifr=0r=\mathbf{0} the minimum error is00, so no betterxx exists. The system is consistent andxx solves it exactly. But uniqueness is a separate question: it needs independent columns, not a zero residual.

Worked example: residual, then the orthogonality check

Fity=C+Dxy=C+Dx to(0,6)(0,6),(1,0)(1,0),(2,0)(2,0).

Set up.A=[101112]A=\begin{bmatrix}1&0\\1&1\\1&2\end{bmatrix} andb=[600]b=\begin{bmatrix}6\\0\\0\end{bmatrix}.

Normal equations.ATA=[3335]A^TA=\begin{bmatrix}3&3\\3&5\end{bmatrix} andATb=[66]A^Tb=\begin{bmatrix}6\\6\end{bmatrix}, so [3335][CD]=[66] ⟹ C=5, D=−3.\begin{bmatrix}3&3\\3&5\end{bmatrix}\begin{bmatrix}C\\D\end{bmatrix}=\begin{bmatrix}6\\6\end{bmatrix}\ \Longrightarrow\ C=5,\ D=-3.

Residual. PredictionsAx^=(5,2,−1)A\hat x=(5,2,-1), so r=b−Ax^=[6−50−20+1]=[1−21].r=b-A\hat x=\begin{bmatrix}6-5\\0-2\\0+1\end{bmatrix}=\begin{bmatrix}1\\-2\\1\end{bmatrix}.

The check.ATr=[1+1+10−2+2]=[3−2+10]A^Tr=\begin{bmatrix}1+1+1\\0-2+2\end{bmatrix}=\begin{bmatrix}3-2+1\\0\end{bmatrix} — carefully: the first entry is1⋅1+1⋅(−2)+1⋅1=01\cdot1+1\cdot(-2)+1\cdot1=0 ✓ and the second is0⋅1+1⋅(−2)+2⋅1=00\cdot1+1\cdot(-2)+2\cdot1=0 ✓.

What the residual says about the data. The first point sits11 above the line, the second22 below, the third11 above. The signs alternate, which is what a best-fit line does; if every miss had the same sign the line could be moved to reduce all of them at once.

What it does not say.r≠0r\ne\mathbf{0}, sobb is not in the column space — no line passes through all three points. Andr⋅b=6−0+0=6≠0r\cdot b=6-0+0=6\ne0, so the residual is not perpendicular tobb.

By the end you can
  • Compute b - Ax entry by entry and say what each entry means
  • Read a zero residual as 'b is in the column space' and nothing more
  • Recognise the residual of a least-squares fit and verify it is perpendicular to every column
Test yourself

(a) ForA=[1234]A=\begin{bmatrix}1&2\\3&4\end{bmatrix},x=(1,1)x=(1,1),b=(3,7)b=(3,7): what is the residual? (b) A residual is perpendicular to both columns. Is it perpendicular tobb? (c) True or false: a zero residual makesxx unique.

Answers

(a)Ax=(3,7)Ax=(3,7) andr=b−Ax=(0,0)r=b-Ax=(0,0). This particularxx solves the system exactly.

(b) No.b=Ax^+rb=A\hat x+r, sobTr=(Ax^)Tr+rTr=0+∥r∥2>0b^Tr=(A\hat x)^Tr+r^Tr=0+\|r\|^2>0 unlessr=0r=\mathbf{0}.

(c) False. It makesxx a solution. Uniqueness needs independent columns; a3×23\times2 system can fitbb exactly and still be non-unique in general.

Work out where to start — free, no accountAnswer six real questions — free, no account