Source scope: §4.5, PDF pages 28. Numerical examples, experiments, added derivations, and practice are teaching supplements unless identified as source results.
Differentiate every occurrence of a coordinate
Take A=[1324]. The quadratic expression is q(x)=x12+5x1x2+4x22, so
∇q(x)=[2x1+5x25x1+8x2]=(A+AT)x.
Writing 2Ax would give the wrong answer because A is not symmetric. Each coordinate appears on both sides of A in xTAx.
The source gives two rules, for constant a∈Rn, constant square A∈Rn×n, and x∈Rn:
∇x(aTx)=a,∇x(xTAx)=(A+AT)x.
If A=AT, the second becomes 2Ax. The square shape is necessary for the scalar expression xTAx to be defined.
Derive the rules from entries
The source states these formulas; this derivation is a teaching supplement. For the linear rule, differentiate aTx=∑iaixi with respect to xk to get ak.
For the quadratic rule, start from Module 4’s identity:
One sum differentiates the left occurrence, the other the right. The diagonal term contributes twice, as it should for Akkxk2. Assemble the coordinates to get (A+AT)x.
At x=(1,−1) the running example gives (−3,−3)T. Check the first coordinate independently by the centered difference [q(x+ϵe1)−q(x−ϵe1)]/(2ϵ). With ϵ=0.001, it equals −3 up to numerical rounding. A finite-difference check is evidence, not a proof, and tiny steps can suffer cancellation in floating-point arithmetic.
Transfer to fitting
Teaching supplement. For L(x)=∥Bx−b∥22, expand to xTBTBx−2bTBx+bTb. The constant has zero derivative and BTB is symmetric, yielding
∇L(x)=2BT(Bx−b).
Here B may be rectangular: the square matrix in the quadratic rule is BTB. If B is m×n, the gradient has n entries, matching the parameter vector.