WhatsApp us
University Maths25 Nov 2026

The Gram–Schmidt Process and Least Squares, Step by Step

How the Gram–Schmidt process builds an orthogonal basis, how it gives the QR factorisation, and how both solve least squares problems such as fitting a line, with six checked examples and practice.

The Gram–Schmidt Process and Least Squares, Step by Step

The Gram–Schmidt process turns a set of independent vectors into an orthogonal set with the same span: keep the first vector, then from each new vector subtract its projections onto the ones you have already built. Least squares uses the same idea in reverse: when Ax = b has no solution, you project b onto the column space of A and solve for that instead. The six worked examples below run from a two-vector case to fitting a line and approximating a function.

The process in one line per step

For independent vectors v₁, v₂, v₃, …, define

u₁ = v₁

u₂ = v₂ − (v₂·u₁ / u₁·u₁) u₁

u₃ = v₃ − (v₃·u₁ / u₁·u₁) u₁ − (v₃·u₂ / u₂·u₂) u₂

and so on. Each subtracted term is the projection of the new vector onto an earlier direction, so what remains is perpendicular to all of them. To get an orthonormal set, divide each uᵢ by its length at the end, qᵢ = uᵢ / ‖uᵢ‖.

Two habits save a lot of fractions. First, you may rescale any uᵢ before using it in later steps (projection onto a line does not care about the length of the direction vector). Second, check each new u against all the earlier ones with a dot product before moving on.

Our online linear algebra tutor page explains how sessions with Ajay Vatsyayan work if you would like your own attempts checked step by step.

Example 1: two vectors in R³

Let v₁ = (1, 1, 0) and v₂ = (2, 0, 1).

u₁ = (1, 1, 0), with u₁·u₁ = 2.

v₂·u₁ = 2, so the projection is (2/2)(1, 1, 0) = (1, 1, 0).

u₂ = (2, 0, 1) − (1, 1, 0) = (1, −1, 1).

Check: u₁·u₂ = 1 − 1 + 0 = 0. Normalised: q₁ = (1, 1, 0)/√2 and q₂ = (1, −1, 1)/√3.

Example 2: three vectors in R⁴, with rescaling

Let v₁ = (1, 1, 1, 1), v₂ = (3, 1, 1, −1), v₃ = (1, 2, 0, 1).

u₁ = (1, 1, 1, 1), u₁·u₁ = 4.

v₂·u₁ = 4, so u₂ = (3, 1, 1, −1) − (1, 1, 1, 1) = (2, 0, 0, −2). Rescale to u₂ = (1, 0, 0, −1).

v₃·u₁ = 4 and v₃·u₂ = 1 − 1 = 0, so u₃ = (1, 2, 0, 1) − (1, 1, 1, 1) − 0 = (0, 1, −1, 0).

Check all three pairs: every dot product is 0. The zero projection onto u₂ is not a mistake; it just means v₃ was already perpendicular to that direction.

If a uᵢ ever comes out as the zero vector, the original vᵢ was a combination of the earlier ones. Gram–Schmidt doubles as an independence test.

Example 3: from Gram–Schmidt to QR

Put the vectors of Example 1 into A = [[1, 2], [1, 0], [0, 1]] (columns v₁, v₂). Gram–Schmidt gives Q with orthonormal columns q₁, q₂, and the coefficients form an upper triangular R = QᵀA:

r₁₁ = v₁·q₁ = 2/√2 = √2

r₁₂ = v₂·q₁ = 2/√2 = √2

r₂₂ = v₂·q₂ = 3/√3 = √3

So A = QR with Q = [q₁ q₂] and R = [[√2, √2], [0, √3]]. R is triangular because each vᵢ only uses q₁, …, qᵢ.

Least squares: what problem it solves

When A has more rows than columns, Ax = b usually has no exact solution. The least squares solution x̂ is the x that makes ‖Ax − b‖ as small as possible. Geometrically, Ax̂ is the projection of b onto the column space of A, so the residual r = b − Ax̂ is perpendicular to every column of A. That condition, Aᵀ(b − Ax̂) = 0, is the normal equations:

AᵀA x̂ = Aᵀb

Which way to solve it?

MethodWhat you computeWhen to use it
Normal equationsAᵀA x̂ = AᵀbSmall problems by hand; quick and exam-friendly
QR factorisationR x̂ = Qᵀb (back-substitution)When you already have Q and R, or numerical accuracy matters
SVD / pseudo-inversex̂ = A⁺bWhen A is rank-deficient or close to it
Orthogonal columnsx̂ᵢ = (b·aᵢ)/(aᵢ·aᵢ)When the columns are already orthogonal: no system to solve

Numerically, forming AᵀA squares the condition number of A, which is why software prefers QR or the SVD. In a written exam, the normal equations are usually what is expected unless the question asks for QR.

Example 4: fitting a line by least squares

Fit y = c₀ + c₁x to the points (0, 1), (1, 2), (2, 4), (3, 4).

Each point gives one equation c₀ + c₁xᵢ = yᵢ, so A = [[1, 0], [1, 1], [1, 2], [1, 3]] and b = (1, 2, 4, 4).

AᵀA = [[4, 6], [6, 14]], Aᵀb = (11, 22)

The determinant of AᵀA is 56 − 36 = 20, so

c₀ = (14·11 − 6·22)/20 = 22/20 = 1.1

c₁ = (4·22 − 6·11)/20 = 22/20 = 1.1

The line is y = 1.1 + 1.1x. Residuals: b − Ax̂ = (−0.1, −0.2, 0.7, −0.4). They add to 0 (perpendicular to the column of ones) and 0(−0.1) + 1(−0.2) + 2(0.7) + 3(−0.4) = 0 (perpendicular to the x column). That two-line check catches almost every arithmetic slip.

Example 5: least squares with QR, and a link back to Gram–Schmidt

Solve Ax = b in the least squares sense with A from Example 3 and b = (1, 2, 3).

Qᵀb = (3/√2, 2/√3). Back-substitute in R x̂ = Qᵀb:

√3 x̂₂ = 2/√3, so x̂₂ = 2/3

√2 x̂₁ + √2(2/3) = 3/√2, so x̂₁ = 3/2 − 2/3 = 5/6

The projection is Ax̂ = (13/6, 5/6, 2/3), and the residual is b − Ax̂ = (−7/6, 7/6, 7/3) = (7/6)(−1, 1, 2).

Run Gram–Schmidt one more step on v₃ = b = (1, 2, 3) and you get exactly this vector. The least squares residual is the part of b that Gram–Schmidt would turn into the next orthogonal direction. The normal equations give the same x̂ = (5/6, 2/3).

Example 6: Gram–Schmidt with an integral inner product

The same process works in any inner product space. On polynomials, use ⟨f, g⟩ = ∫₀¹ f(x)g(x) dx.

Start with 1 and x. ⟨x, 1⟩ = ½ and ⟨1, 1⟩ = 1, so u₂ = x − ½. The orthogonal pair is {1, x − ½}.

Now find the closest straight-line approximation to x² on [0, 1], meaning the one that minimises ∫₀¹ (x² − a − bx)² dx. Project x² onto the orthogonal basis:

⟨x², 1⟩ = 1/3 and ⟨x², x − ½⟩ = 1/4 − 1/6 = 1/12, with ⟨x − ½, x − ½⟩ = 1/12

Projection = (1/3)(1) + (1/12 ÷ 1/12)(x − ½) = x − 1/6

So the least squares line for x² on [0, 1] is y = x − 1/6. Minimising the integral directly with partial derivatives gives the same a = −1/6, b = 1. Continue Gram–Schmidt to higher powers and you build the shifted Legendre polynomials.

Where students lose marks

  • Dividing by the length, not the length squared. The projection coefficient is (v·u)/(u·u), not (v·u)/‖u‖, unless u is already a unit vector.
  • Projecting onto the original vectors. Subtract projections onto u₁, u₂, … (the new orthogonal ones), never onto v₁, v₂, ….
  • Normalising too early. Square roots in the middle of the process multiply your chances of an error. Normalise once at the end.
  • Forgetting the orthonormal step for QR. Q needs unit columns; orthogonal is not enough.
  • Trying A⁻¹b for a non-square A. It does not exist. Use the normal equations.
  • Dropping the column of ones. Fitting y = c₀ + c₁x needs both columns. Without the ones, you force the line through the origin.
  • Not checking Aᵀr = 0. The residual must be orthogonal to every column; if it is not, the solution is wrong.

Practice questions

  1. Apply Gram–Schmidt to (1, 2, 2) and (1, 0, 4), then normalise.
  2. Fit a least squares line y = c₀ + c₁x to (1, 2), (2, 3), (3, 5), (4, 6).
  3. Find the least squares solution of Ax = b with A = [[1, 0], [0, 1], [1, 1]] and b = (1, 1, 0).
  4. Using Question 1, find the projection of (3, 0, 0) onto the plane spanned by (1, 2, 2) and (1, 0, 4).
  5. Find the QR factorisation of [[1, 1], [2, 0], [2, 4]].
  6. What happens if you apply Gram–Schmidt to (1, 2) and (2, 4)? What does it tell you?

Answers.

  1. u₂ = (1, 0, 4) − (9/9)(1, 2, 2) = (0, −2, 2). Orthonormal: (1, 2, 2)/3 and (0, −1, 1)/√2.
  2. y = 0.5 + 1.4x.
  3. AᵀA = [[2, 1], [1, 2]] and Aᵀb = (1, 1), so x̂ = (1/3, 1/3).
  4. With q₁ = (1, 2, 2)/3 and q₂ = (0, −1, 1)/√2, b·q₁ = 1 and b·q₂ = 0, so the projection is q₁ = (1/3, 2/3, 2/3).
  5. Q has columns (1, 2, 2)/3 and (0, −1, 1)/√2; R = [[3, 3], [0, 2√2]].
  6. u₂ = (2, 4) − 2(1, 2) = (0, 0). The vectors are dependent, so they span only a line.

Where these ideas go next

Orthogonality runs through the rest of linear algebra. The orthogonal eigenvectors of a symmetric matrix, used in orthogonal diagonalisation, are often produced by Gram–Schmidt inside a repeated eigenspace. The singular value decomposition gives the most stable least squares method and handles rank-deficient A, where the rank–nullity theorem tells you the solution is not unique. In statistics and data science, least squares is linear regression; our guide to linear algebra for machine learning shows where it sits, and school students meet the same fitted line in our regression and correlation guide for IB Maths AA SL.

How Maths Bodhi helps

Gram–Schmidt and least squares are routine once the projection formula is automatic and the checks are a habit. In online sessions we work on your own course problems, ask you to predict each check before computing it, and review your attempts so the fractions stop costing marks. We explain and practise; we don't complete graded work. For the wider first-year picture, see our university maths tutoring.

Home tuition is available in Gurgaon (Gurugram). Students elsewhere in India and abroad learn with us online. You can book a free demo class.

Written and checked by the Maths Bodhi team (November 2026).

Frequently asked questions

What is the Gram Schmidt process used for?
It turns any basis into an orthogonal or orthonormal basis with the same span, which gives QR factorisation and simple projections.
What is the formula for Gram Schmidt orthogonalization?
Each new uₖ is vₖ minus the sum of (vₖ·uᵢ / uᵢ·uᵢ)uᵢ over all earlier uᵢ.
How do you find a least squares solution?
Solve the normal equations AᵀAx̂ = Aᵀb, or Rx̂ = Qᵀb if you have a QR factorisation of A.
Why is the least squares residual orthogonal to the column space?
Because the closest point to b in the column space is its orthogonal projection, so the error must be perpendicular to every column.
Is least squares the same as linear regression?
Ordinary linear regression is least squares with a design matrix whose columns are a column of ones and the predictor values.
What happens in Gram Schmidt if the vectors are dependent?
A zero vector appears at the step where a vector is a combination of the earlier ones.
Gram-Schmidtleast squaresQR factorisationlinear algebrauniversity maths

Related guides

Keep exploring this maths topic

University Maths25 Nov 2026

Singular Value Decomposition Explained, with Step-by-Step Examples

What the singular value decomposition A = UΣVᵀ means, how to compute it by hand for square and rectangular matrices, and how it gives low-rank approximations and the pseudo-inverse, with checked examples and practice.

singular value decompositionSVDlinear algebrapseudo-inverse

By Maths Bodhi Team

Read guide

University Maths25 Nov 2026

Linear Algebra for Machine Learning: What You Actually Need, with Small Worked Examples

The linear algebra that computer science and data science students really use in machine learning, mapped to the topics of a standard course, with a small worked example for each idea.

linear algebramachine learningdata sciencePCA

By Maths Bodhi Team

Read guide

University Maths25 Nov 2026

Diagonalization of a Matrix: The Test, the Method and 6 Worked Examples

How to diagonalise a matrix as A = PDP⁻¹, how to tell quickly whether it can be done, and how to use it for matrix powers, symmetric matrices and differential equations, with checked examples and practice.

diagonalizationlinear algebraeigenvaluesmatrix powers

By Maths Bodhi Team

Read guide

University Maths25 Nov 2026

How to Find the Matrix of a Linear Transformation: Examples and Solutions

How to build the matrix of a linear transformation from a formula, from images of other vectors, from a geometric description, on polynomial and function spaces, and in a new basis, with six checked examples and practice.

linear transformationslinear algebrachange of basisstandard matrix

By Maths Bodhi Team

Read guide

University Maths25 Nov 2026

The Rank–Nullity Theorem Explained, with Examples for Matrices and Linear Transformations

What the rank–nullity theorem says, why it is true, and how to use it on matrices, polynomial maps and matrix spaces, with six checked examples, common mistakes and practice.

rank nullity theoremlinear algebranull spacelinear transformations

By Maths Bodhi Team

Read guide

University Maths2 Nov 2026

Why Linear Algebra Feels Hard After the First Few Weeks, and How to Fix It

Many students cope with matrix calculations, then get lost at subspaces, independence, bases and linear maps. Here is why that happens, five worked examples of the questions that cause it, and a study method that gets you through.

linear algebra helpvector spaceslinear algebra proofsuniversity linear algebra

By Maths Bodhi Team

Read guide