Math for Robotics · Lesson 06
Matrices
Est. 30–40 min
Why
Why robots pack linear maps into tables of numbers
Concept
The big idea: a matrix is a machine that eats a vector and returns a vector
- A 2×2 matrix has two rows and two columns.
- Matrix × vector: each output component is a dot product of one row with the vector.
- Matrix × matrix: compose two maps — apply B first, then A, when you write A·B (column-vector convention).
- Identity I leaves vectors unchanged. Rotation matrices are special: they preserve length.
How it works
How it actually works, step by step
Write the table
Rows = output recipes. Columns = how much each input component contributes.
Multiply by a vector
Outₓ = row0 · v, Outᵧ = row1 · v.
Multiply two matrices
Column j of the result is A times column j of B.
Check a rotation
For R(θ) = [[c,-s],[s,c]], |R v| should equal |v|.
Visual
Visualise it: rotation as a 2×2 matrix
Change θ and watch the basis vectors move. That motion is exactly multiplication by a 2×2 rotation matrix.
Interactive · 2D rotation visualizer
R(θ) =
[ 0.819 , -0.574 ]
[ 0.574 , 0.819 ]
R·v = (62.3, 68.0)
Length is preserved under pure rotation (|v| ≈ |R·v|). This 2×2 matrix is the planar cousin of SO(3) rotations used on 6DOF wrists.
Worked example
Worked example: B rotates (1,0) to (0,1)
Code lab
Code lab: 2×2 multiply
"""Stage 1 L06 — multiply two 2×2 matrices and apply to a vector."""
A = [[1, 2], [3, 4]]
# R(90° CCW) = [[0,-1],[1,0]] maps (1,0) → (0,1)
B = [[0, -1], [1, 0]]
v = [1, 0]
def mat_mul(X, Y):
return [
[X[0][0]*Y[0][0] + X[0][1]*Y[1][0], X[0][0]*Y[0][1] + X[0][1]*Y[1][1]],
[X[1][0]*Y[0][0] + X[1][1]*Y[1][0], X[1][0]*Y[0][1] + X[1][1]*Y[1][1]],
]
def mat_vec(M, u):
return [M[0][0]*u[0] + M[0][1]*u[1], M[1][0]*u[0] + M[1][1]*u[1]]
C = mat_mul(A, B)
Bv = mat_vec(B, v)
print("A =", A)
print("B =", B)
print("A·B =", C)
print("B · [1,0] =", Bv, " # 90° CCW: +x maps to +y")Expected output
A = [[1, 2], [3, 4]] B = [[0, -1], [1, 0]] A·B = [[2, -1], [4, -3]] B · [1,0] = [0, 1] # 90° CCW: +x maps to +y
Real world
Where small matrices appear
- 2×2 and 3×3 rotation blocks inside larger transforms
- Camera intrinsics as a 3×3 matrix (later stages)
- Jacobian matrices map joint rates → tip rates (Stage 8)
Mistakes
Common mistakes and confusions
Hardware & safety
Hardware lab and safety notes
No new parts — use your paper arm and graph paper. A rotation matrix is just a recipe that tells each point where to go when the whole arm turns.
- Mark the tip of link 1 at (10, 0) cm. Apply B = [[0, −1], [1, 0]] by hand: new tip (0, 10).
- Now physically turn the paper arm 90° anticlockwise about the pin. Does the tip land on (0, 10)?
- Try A = [[2, 0], [0, 1]] (stretch x): not a rotation — your cardboard can't do it, because a rotation keeps lengths.
Debugging
Debugging: when it doesn’t work
| Symptom | Likely cause | Fix |
|---|---|---|
| A·B ≠ B·A and you think the code is broken | Matrix multiplication is not commutative | That is normal — keep the order carefully |
| Result vector has the wrong shape | Row vector used where a column is expected | Use column vectors: M · [x, y]ᵀ |
| Rotation also changes length | Sign slip in [[c, −s], [s, c]] | Check det = 1 and the columns have length 1 |
| NumPy gives an element-wise result | Used * instead of @ | Use A @ B or np.matmul |
Think
Think about it
Think
What does the identity matrix do to a vector? What should R(0°) look like?
Experiment
Try it in the lab: multiply by hand
Experiment
Multiply by hand
You will need
- Notebook
- rotation-2d interactive
Steps
- Write R(90°) = [[0,−1],[1,0]].
- Compute R(90°)·(1,0) and R(90°)·(0,1) by hand.
- Confirm with the interactive that +x maps to +y.
Interactive · 2D rotation visualizer
R(θ) =
[ 0.819 , -0.574 ]
[ 0.574 , 0.819 ]
R·v = (62.3, 68.0)
Length is preserved under pure rotation (|v| ≈ |R·v|). This 2×2 matrix is the planar cousin of SO(3) rotations used on 6DOF wrists.
(1,0) → (0,1) and (0,1) → (−1,0) for +90° CCW.
Knowledge check
Check your understanding
Knowledge check
1. Matrix×vector output component is…
2. A·B equals B·A always?
3. Rotation matrices preserve…
4. R(0°) equals…
Practice quiz
Mini assignment
Hands-on assignment
Mini assignment
2×2 practice
Pick two 2×2 matrices with small integers. Multiply them by hand and with a 4-line Python script. Confirm they match.
Deliverables
- Hand working
- Script output
Do this, step by step
- Pick A = [[1, 2], [0, 1]] and B = [[0, −1], [1, 0]]. Compute A·B and B·A by hand.
- Edit
matrices_2x2.pyto print both products and compare with your hand working. - Apply A·B and B·A to (1, 0). Draw both results on graph paper.
- Write one sentence: why does the order matter for a robot arm?
Stuck? Hints, one at a time
- Hint 1: Row × column: entry (1,1) of A·B = 1·0 + 2·1 = 2.
- Hint 2: A·B = [[2, −1], [1, 0]], B·A = [[0, −1], [1, 2]].
- Hint 3: (A·B)·(1, 0) = (2, 1); (B·A)·(1, 0) = (0, 1). Different points — so “turn then shear” ≠ “shear then turn”.
Challenges (pick by level)
- 🟢 Easy: Multiply [[2, 0], [0, 3]] by (4, 5).
- 🟡 Medium: Show that R(90°)·R(90°) = R(180°) by multiplying.
- 🟠 Hard: Write a 2×2 multiply without NumPy and test it against
np.matmulon 100 random matrices.
Next step
Next, Lesson 7: Transformations adds a translation to the rotation. Related: matrices in control and state-space in Stage 7 — Control Systems.
Sources
Self-attested for now — keep artifacts in your lab notebook.
Complete
Today's goal
0 of 1 done today
Mark this lesson complete when you have worked the experiment, knowledge check, and assignment (even roughly).