Math for Robotics · Lesson 04
2D Vectors
Est. 30–40 min
Why
Why robot motion is vector arithmetic
Concept
The big idea: arrows you can add and scale
- A 2D vector has components (x, y) — length and direction packed together.
- Magnitude |v| = √(x² + y²).
- Add: (aₓ+bₓ, aᵧ+bᵧ). Tip-to-tail on paper.
- Scale: k·(x,y) = (kx, ky). Negative k flips direction.
- Dot product a·b = aₓbₓ + aᵧbᵧ = |a||b| cos φ. Measures alignment.
How it works
How it actually works, step by step
Write components
Decide the frame first. Then write (x, y) in that frame’s units.
Add for composition
Link1 vector + link2 vector = tip from base (planar FK again).
Scale for “go further”
Double a step vector to take a longer stride in the same direction.
Dot for “how aligned”
If a·b = 0 the vectors are perpendicular. If a·b > 0 they share a half-plane.
|a| = √(aₓ² + aᵧ²) a · b = aₓbₓ + aᵧbᵧ
Visual
Visualise it: add and scale vectors
Switch Add / Scale. Drag components and watch the result arrow and |a|, a·b update.
Interactive · 2D vector visualizer
|a|
89.4
a + b
(110, 110)
|b|
76.2
a · b
5200
Tip position of a planar arm is often a sum of link vectors. Scaling changes length; adding composes displacements.
Worked example
Worked example: a=(3,4), b=(1,−2)
Code lab
Code lab: add, scale, dot
"""Stage 1 L04 — add, scale, magnitude, and dot product of 2D vectors."""
import math
a = (3.0, 4.0) # steps east, north (metres)
b = (1.0, -2.0)
def add(u, v):
return (u[0] + v[0], u[1] + v[1])
def scale(k, u):
return (k * u[0], k * u[1])
def mag(u):
return math.hypot(u[0], u[1])
def dot(u, v):
return u[0] * v[0] + u[1] * v[1]
s = add(a, b)
print(f"a = {a}, |a| = {mag(a):.2f} m")
print(f"b = {b}, |b| = {mag(b):.2f} m")
print(f"a + b = {s}, |a+b| = {mag(s):.2f} m")
print(f"2·a = {scale(2, a)}")
print(f"a · b = {dot(a, b):.1f}")
cos_ang = dot(a, b) / (mag(a) * mag(b))
print(f"angle between a and b ≈ {math.degrees(math.acos(cos_ang)):.1f}°")Expected output
a = (3.0, 4.0), |a| = 5.00 m b = (1.0, -2.0), |b| = 2.24 m a + b = (4.0, 2.0), |a+b| = 4.47 m 2·a = (6.0, 8.0) a · b = -5.0 angle between a and b ≈ 116.6°
Real world
Vectors on an arm and on the floor
- Planar arm tip = sum of link vectors
- Mobile robot: velocity vector in the plane
- Force and friction as opposing vectors in free-body diagrams
Mistakes
Common mistakes and confusions
Hardware & safety
Hardware lab and safety notes
Make vectors physical with two strips of cardboard (10 cm and 8 cm) joined with a split pin — a paper two-link arm. Each strip is a vector; the tip is their sum.
- Lay the arm on your Lesson 3 graph-paper frame with the pin at the origin.
- Measure each strip's end point (x, y) and add them. Compare with the measured tip.
- Then walk it: 3 steps forward + 4 steps left on the floor — measure the straight-line distance (≈ 5 steps).
Debugging
Debugging: when it doesn’t work
| Symptom | Likely cause | Fix |
|---|---|---|
| |a + b| comes out as |a| + |b| | Added lengths, not components | Add x with x, y with y |
| Dot product sign looks wrong | Mixed up which vector is which axis | Write (x, y) labels on each strip |
| Measured tip and sum differ by cm | Strips measured from different origins | Measure every point from the frame origin |
| Interactive and paper don't match | Interactive uses screen units | Compare directions and ratios, not raw numbers |
Think
Think about it
Think
Can two non-zero vectors sum to zero? What does that mean physically?
Experiment
Try it in the lab: vector playground
Experiment
Vector playground
You will need
- vector-2d interactive
- Notebook
Steps
- Set a=(80,40), b=(30,70), mode Add. Note result and |a|.
- Switch to Scale, set k=−1. Confirm the arrow flips.
- Find a pair with a·b ≈ 0 (perpendicular).
Interactive · 2D vector visualizer
|a|
89.4
a + b
(110, 110)
|b|
76.2
a · b
5200
Tip position of a planar arm is often a sum of link vectors. Scaling changes length; adding composes displacements.
When a·b is near 0 the arrows look roughly at right angles.
Knowledge check
Check your understanding
Knowledge check
1. |(3,4)| equals…
2. a·b = 0 means…
3. Scaling by −2…
4. Tip of a planar two-link arm is…
Practice quiz
Mini assignment
Hands-on assignment
Mini assignment
Link as vector
Treat each link of a 2DOF sketch as a vector. Pick numbers, compute tip by addition, and check with the interactive.
Deliverables
- Sketch with components
- Numeric tip
- Screenshot or note from interactive
Do this, step by step
- Set the paper arm with link 1 along x (10, 0) and link 2 at 90° (0, 8). Predict the tip, then measure it.
- Turn link 2 to point back along −x. What is the tip now? Which vector rule explains it?
- Run
vectors_2d.py, change b to your link-2 numbers and confirm the sum. - In the interactive, find a pair with a·b ≈ 0 and write the numbers down.
Stuck? Hints, one at a time
- Hint 1: First case: (10, 0) + (0, 8) = (10, 8). Distance from origin = √(100 + 64) ≈ 12.8 cm.
- Hint 2: Link 2 backwards is (−8, 0), so the tip is (2, 0) — scaling by −1 flips direction.
- Hint 3: a·b = 0 when aₓbₓ = −aᵧbᵧ, e.g. a = (80, 40), b = (−20, 40).
Challenges (pick by level)
- 🟢 Easy: Find |(6, 8)| without a calculator.
- 🟡 Medium: Use the dot product to compute the angle between (3, 4) and (1, −2). (≈ 116.6°)
- 🟠 Hard: Write
tip(L1, L2, q1, q2)that builds two vectors and adds them; check it againsttwo_link_fk.py.
Next step
Next, Lesson 5: 3D Vectors adds z and the cross product. Related: velocity vectors on mobile robots in Stage 5 — Robotics Fundamentals.
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).