Euclidean space — guided practice with music taste: Gram matrix, angles, orthonormal bases, Gram-Schmidt and the exam’s cosine (and the decimals that got away from me)
In MATLAB assignment 3 I got a cosine question wrong while having the method right: I wrote 0.3741 where it was 0.3746, and in the second part, −0.5899 instead of −0.5898. The mistake was in the fourth decimal place, and it came from rounding the roots before dividing. The box compares numbers, so it did not matter that the idea was right. In this practice you will see why it happens and how to avoid it: round only at the end.
In this practice we do by hand what you saw in the Euclidean space theory, with the usual music tastes: the Gram matrix of the standard product in a non-standard basis, angles with two different dot products (and the decimals), a basis that is orthogonal with respect to a Gram matrix that is not the identity, Gram-Schmidt with a projection and, finally, the cosine part as it appears in the exam, with its marks. Each problem is checked in MATLAB, as in Assignment 3.
Table of Contents
Before you start
Make sure you are clear on:
- How to multiply a row by a matrix by a column (topic 1).
- What a basis is and how to write the coordinates of a vector in it (topic 3).
- The formulas of the topic: , and .
- That orthogonal means product 0, and orthonormal, in addition, norm 1.
If any of them sounds shaky, review the theory before going on.
The usual tastes, in hours a week of pop, rock and classical: Sergio , Laura , Pablo and Ana .
How to solve a dot product problem on paper
- Data: identify the dot product. If it is the standard one, ; if you are given an expression, write its matrix (in position , the coefficient of ).
- Shape: write each product as row column. First compute times the column and then the row times the result.
- Measure: norms with the positive root, cosines with the quotient. Keep roots as roots until the end.
- Check: a cosine is always between and ; an orthogonal basis has a diagonal ; a projected vector leaves a perpendicular remainder.
Problem 1: the Gram matrix in another basis
Statement. In with the standard dot product consider the basis .
a) Compute the Gram matrix of the dot product with respect to .
b) Two vectors have coordinates and in . Compute their dot product with and check it by moving them to the standard basis.
The idea
Each entry of is the product of two basis vectors: . Or, with matrices, , with the vectors of as columns. And in b) the product is done with the coordinates in and the of that basis, never mixing bases.
is symmetric: you only need the diagonal and what is above it, six products instead of nine.
In words: the basis is not the usual one. Its vectors neither have norm 1 nor are perpendicular to each other. The vector with coordinates in is actually , whose norm is , not 1. If you used the usual product with coordinates in , you would be treating the vectors of as if they were the standard ones, and they are not.
stores exactly what you are missing about the basis. On the diagonal, the squared norm of each of its vectors. Off the diagonal, how much they “overlap” two by two: a 0 would mean perpendicular; a positive number, that they form an acute angle; a negative one, obtuse. That is why, before doing any computation, you can already expect a 5 in the first entry (the from before, squared). And in b) you get a free check: the product has to come out the same both ways, with and in the standard basis. If it does not, one of the two computations is wrong.
Step by step
a) The six products:
| Entry | Product | Value |
|---|---|---|
b) First times the column , then the row times the result:
Check
In the standard basis, and . With the standard product, . The same number, as it must be: the dot product does not depend on the basis, only its matrix does.
Check it in MATLAB
format rat C = [1 0 1; 2 1 0; 0 1 -1]; % the vectors of B' as columns Gp = C' * C % G' = C^t I C X = [1; 1; 0]; Y = [0; 1; 1]; % coordinates in B' X' * Gp * Y % product with G' dot(C * X, C * Y) % the same, moving to the standard basis
Output:
Gp =
5 2 1
2 2 -1
1 -1 2
ans =
4
ans =
4
C * X moves the coordinates from to the standard basis (the change-of-basis matrix from topic 3), and there dot works.
Questions you might ask
Why and not ? Because entry of is row of (basis vector ) times column of (vector ): exactly . multiplies components, not vectors.
Does have the same rank as ? Yes, 3: they are congruent, and congruence preserves rank.
Doing the product in b) with the identity matrix, . The coordinates are in , so the matrix is the one of . Using only works with coordinates in an orthonormal basis.
Try it yourself
- Compute the Gram matrix of the standard product in the basis . (It comes out diagonal, : the basis is orthogonal.)
- With that basis, the product of and (coordinates in it) is 8. Check it in the standard basis.
Problem 2: one angle with two dot products
Statement. a) In define the dot product . Compute the cosine of the angle between and with this product and with the standard one. b) With the standard product of , compute the cosine of the angle between and to four decimal places.
The idea
a) First the Gram matrix: on the diagonal, the coefficients of and ; off it, that of (which is the same as that of ). With , everything comes from . b) is the assignment calculation: the key thing is not to round the roots before dividing.
With and , is directly an entry of the matrix: . And , .
Step by step
a) The Gram matrix is
and with the standard vectors: , , :
With the standard product, : they are orthogonal, . The same two vectors are perpendicular with one product and not with the other.
b) , and . Combining the roots before computing anything:
Look at what happens if you round first: and give , which rounds to . One wrong digit, the box in red.
| Way of computing | Result | To 4 decimals |
|---|---|---|
| , rounding at the end | ||
| Roots rounded to two decimals |
Why does such a small difference in the roots move the fourth digit? Step by step, both computations side by side:
| Step | Without rounding | Rounding first | Why |
|---|---|---|---|
| You round up: about 5 ten-thousandths too many | |||
| Up again: about 44 ten-thousandths too many | |||
| Denominator | Both errors go the same way and add up: the denominator grows by 0.14 % | ||
| Cosine | If the denominator grows by 0.14 %, the quotient drops by 0.14 %: about 16 hundred-thousandths | ||
| To 4 decimals | was only 8 hundred-thousandths above , the rounding boundary, and the error is twice that |
The lesson is not that you have to round “better”, but that the error from rounding along the way is carried to the end, and it only has to land near a boundary to change the digit you are asked for. With there is nothing to carry: a single rounding, in the last step.
Check
Both cosines are between and . And in a), : the product “brings the two axes closer”, because .
Check you’ve got it: what would happen if in a) both cross terms had a minus sign?
That is, with . The cosine would change sign: and . Step by step: the Gram matrix is now . The diagonal does not change, so the norms are still and . The only thing that changes is the product, , and with it the sign of the cosine. It is still a dot product: it is symmetric and its minors are and . If before “brought the two axes closer”, now it “pushes them apart”: , the same angle seen from the other side.
Check it in MATLAB
format short G = [2 1; 1 3]; u = [1; 0]; v = [0; 1]; c = (u' * G * v) / (sqrt(u' * G * u) * sqrt(v' * G * v)) acos(c) * 180 / pi c2 = dot([1 2 -1], [3 0 2]) / (norm([1 2 -1]) * norm([3 0 2]))
Output:
c =
0.4082
ans =
65.9052
c2 =
0.1132
MATLAB never rounds along the way: it keeps about 16 digits and only trims when displaying the result. That is why, in the assignment, you copy what comes out, not what you would compute with the calculator rounding each step.
Questions you might ask
How do I know that expression is a dot product? It is symmetric (the coefficients of and are equal) and positive definite: if . In topic 6 you will check it in one go with the minors of : and .
And if I am asked for the angle and not the cosine? , between and . With the calculator in degrees, or multiplying by if it comes out in radians.
Putting the coefficient of divided by 2 in . That is done with the quadratic forms of topic 6 (where merges the two cross terms); with the expression of a dot product, and already come separately and each goes to its position as it is.
Try it yourself
- With the same , compute . (.)
- Cosine, with the standard product, between and . (.)
Problem 3: is this basis orthogonal?
Statement. In consider the dot product with Gram matrix (in the standard basis) , the one from the theory where pop and rock “count together”. a) Is the standard basis orthogonal? b) Check that is an orthogonal basis and, starting from it, compute an orthonormal basis.
The idea
A basis is orthogonal if the Gram matrix in it is diagonal. For the standard basis, it is itself. For , you have to compute the three cross products with (or the matrix , with the vectors of as rows). Then each vector is divided by its norm, also with .
and . With those two columns, every product can be done in your head.
Step by step
a) In the standard basis the Gram matrix is , which is not diagonal: . It is not orthogonal with this product (although it is with the standard one).
b) Cross products:
- .
- .
- .
Pairwise orthogonal. They are three independent vectors (non-zero orthogonal vectors always are), so is an orthogonal basis. The norms:
Dividing:
Check
The Gram matrix of with is : zeros off the diagonal and, on the diagonal, the squares of the norms. Dividing by the norms gives the identity.
Check it in MATLAB
G = [2 1 0; 1 2 0; 0 0 1]; S = [1 -1 0; 1 1 0; 0 0 1]; % the vectors of S as rows S * G * S' % their Gram matrix: diagonal
Output:
ans =
2 0 0
0 6 0
0 0 1
With the vectors as rows, the Gram matrix is S * G * S' (row times times column ). With the vectors as columns it would be C' * G * C, the congruence formula: it is the same with .
Questions you might ask
Why is the standard basis not orthogonal here? Because orthogonality depends on the product: with , “pop” and “rock” have a product of 1, not 0. In the drawing, the axes “come closer”.
Do I have to prove separately that is a basis? No: a set of non-zero pairwise orthogonal vectors is always independent, and three independent vectors in are a basis.
Computing the norms in b) with the standard product: . With it is . If the statement gives a product, every measurement (products, norms, angles) is done with it.
Try it yourself
- Is the basis orthogonal with ? (Yes: .)
- Compute its orthonormal basis. (Norms , and 1.)
Problem 4: Gram-Schmidt and a projection
Statement. A playlist is described by two taste “directions”: (a bit of everything) and (more rock than pop, no classical). Let , with the standard product.
a) Compute an orthogonal basis of by Gram-Schmidt.
b) Compute the orthogonal projection of Sergio’s taste, , onto , and the distance from to .
The idea
Gram-Schmidt: , and from you remove its projection onto . With the orthogonal basis, the projection onto is the sum of the projections onto each , and the distance is the norm of what is left.
and : the coefficient of the first subtraction is 1.
In words, with the playlist. is “a bit of everything”. Projecting any taste onto gives its average in each genre: adds up to 3 hours over three genres, one hour on average, and that is why its projection is exactly . What Gram-Schmidt removes from is that “a bit of everything” part, which was already in . What is left, , is what makes different: one hour more rock and one hour less classical than the average. That difference no longer shares anything with , and that is why it is perpendicular.
The same happens with the projection of : its coefficient on is , my average hours (), and its coefficient on says how much I look like “more rock than classical”.
Step by step
a)
Check: .
b) With , , and :
What is left:
and the distance is its norm:
My taste is very close to that playlist: almost everything I listen to is explained by its two directions.
The same projection, without skipping anything:
| Step | Computation | Result | Why |
|---|---|---|---|
| 1 | What my taste shares with “a bit of everything” | ||
| 2 | You divide by it so the size of does not count | ||
| 3 | My average hours, in all three genres | ||
| 4 | I listen to less rock than classical: I go against | ||
| 5 | The minus sign removes rock and adds classical | ||
| 6 | Add 3 and 5 | Adding works because and are perpendicular | |
| 7 | minus step 6 | What the playlist does not explain about my taste | |
| 8 | The norm of the rest: |
Look at step 6: if and were not perpendicular, adding them would count twice what they share. That is what Gram-Schmidt was for.
Check
The remainder has to be perpendicular to all of : and .
Check it in MATLAB
format rat W = [1 1 1; 1 2 0]'; % w1 and w2 as columns u1 = W(:,1); u2 = W(:,2) - dot(W(:,2), u1) / dot(u1, u1) * u1 s = [2; 1; 2]; p = dot(s, u1) / dot(u1, u1) * u1 + dot(s, u2) / dot(u2, u2) * u2 r = s - p format short norm(r)
Output:
u2 =
0
1
-1
p =
5/3
7/6
13/6
r =
1/3
-1/6
-1/6
ans =
0.4082
The fractions are the paper ones, and the distance, .
Questions you might ask
Can I project without orthogonalising first? With the sum formula, no: it only works with an orthogonal basis. With any basis you have to solve a system (the normal equations from the theory).
Why is the distance the norm of the remainder? Because the remainder is perpendicular to : it is the “shortest jump” from to , like the height of a point above the floor.
Projecting with the original basis, . As and are not orthogonal, you count the shared part twice and the remainder is no longer perpendicular.
Try it yourself
- Project onto . (, because its product with is 0.)
- Compute a vector perpendicular to with the cross product: . Is it parallel to the remainder in b)? (Yes: .)
Problem 5: how the exam asks it
Exam-style part. [5 points] In , with respect to the standard basis, consider the dot product , with and . a) (1 point) Write its Gram matrix and the matrix expression of . b) (1.5 points) Compute the norms of and . c) (1.5 points) Compute the cosine of the angle between and with respect to . d) (1 point) Give a non-zero vector orthogonal to with respect to .
The idea
It is part c) of the bilinear forms exercise in the exams, on its own: there you are given a bilinear form, you check in topic 6 that it is a dot product, and then you compute the cosine with it. Careful with the notation: here are the components of the first vector and those of the second.
Row 1 of holds the coefficients of , and ; row 2, those of , , ; row 3, those of .
Step by step
a) Reading coefficients: , , , , , and 0 elsewhere:
It is symmetric, as a dot product must be.
b) , so and .
, so and .
c) :
d) is orthogonal to if . For example, : . (Any vector of that plane, except , will do.)
Check it in MATLAB
G = [2 1 0; 1 1 0; 0 0 3]; u = [1; 0; 1]; v = [0; 1; -1]; nu = sqrt(u' * G * u), nv = sqrt(v' * G * v) c = (u' * G * v) / (nu * nv) acos(c) * 180 / pi format rat null((G * u)', 'r') % the vectors orthogonal to u with f, as columns
Output:
nu =
2.2361
nv =
2
c =
-0.4472
ans =
116.5651
ans =
-1/2 -3/2
1 0
0 1
null((G * u)', 'r') solves and gives a basis of the plane of vectors orthogonal to : and . The first, times 2, is our .
Check and how it is marked
The cosine is between and , and it is negative: with , and make an obtuse angle, although with the standard product their product, , is also negative and the angle is a different one (, ).
Indicative split of the 5 points (mine, not the official one):
| Part | What is assessed | Points |
|---|---|---|
| a) | correctly placed and symmetric | 0.5 |
| a) | Expression row · · column | 0.5 |
| b) | Each norm with and the positive root | 0.75 + 0.75 |
| c) | with | 0.75 |
| c) | The quotient and the cosine | 0.75 |
| d) | The condition and a non-zero vector | 1 |
Questions you might ask
And if were not a dot product? Then you could not talk about norm or angle, and the exam says “if possible”: that is why the form is classified first (topic 6). Here has minors , and , all positive: it is positive definite.
Can I give in d)? No: the statement asks for a non-zero vector, and is orthogonal to everything without contributing anything.
Computing the cosine with the standard product because “the vectors are in the standard basis”. The standard basis tells you in which coordinates the vectors come; the product is given by . With the standard one you would get , and the whole part, zero marks.
Try it yourself
- With the same , cosine between and . (.)
- Is orthogonal to with ? (: no.)
Common mistakes in this practice
- Doing the products with the identity when the statement gives another Gram matrix (or coordinates in another basis).
- Writing column row.
- Putting in the norm.
- Rounding roots or quotients before the last step.
- Halving the cross terms of the expression of a dot product (that belongs to quadratic forms).
- Projecting with a basis that is not orthogonal.
Try it in the MATLAB playground
In the website’s MATLAB playground you have a small dot product “calculator” with any matrix you like:
G = [2 1 0; 1 1 0; 0 0 3]; % change the dot product here
u = [1; 0; 1]; v = [0; 1; -1]; % and the vectors here (as columns)
pe = u' * G * v;
nu = sqrt(u' * G * u); nv = sqrt(v' * G * v);
fprintf('u·v = %g, |u| = %.4f, |v| = %.4f, cos = %.4f\n', pe, nu, nv, pe / (nu * nv))
Things to look at:
- Set
G = eye(3)and compare with the results of problem 5: the standard product is the case . - Try a that is not symmetric,
[2 1 0; 0 1 0; 0 0 3], and swapuandv: you get a different number. That is why a dot product requires a symmetric . - With
G = [1 2; 2 1]andu = [1; -1],u' * G * uis negative: it is not a dot product and the root does not exist.
Summary
- The Gram matrix in a basis holds in the product of vector by vector ; with the standard product, .
- Every product is computed as row column, first times the column.
- Angles and orthogonality depend on the product: two vectors can be perpendicular with one and not with another.
- Round only at the end; MATLAB never rounds along the way.
- Gram-Schmidt subtracts projections onto the new vectors, and the projection onto a subspace is done with an orthogonal basis.
Checklist: have you practised it?
- Can you build the Gram matrix of a basis and use it with coordinates in that basis?
- Do you write the matrix of a dot product given by an expression without halving the cross terms?
- Have you checked that the same pair of vectors changes angle when the product changes?
- Do you keep roots as roots until the last step?
- Can you check whether a basis is orthogonal with and orthonormalise it?
- Have you done Gram-Schmidt and a projection, and checked that the remainder is perpendicular?
- Have you run the MATLAB blocks and checked the outputs?
If you answered yes to almost everything, you are ready for the Euclidean space exercises, where I no longer give you the step by step and there is a 20-question quiz like the Campus ones.
Next step: exercisesEuclidean space — exam-style exercises with music taste: norms, cosines, Gram matrix, Gram-Schmidt and a Campus-style quiz (and the form I ticked too many times)Did this article help you? Here is how you can support SergioLearns →



