Skip to main content

Tutorial 4

Binder Open In Colab

Interactive Notebooks

Click the badges above to run this tutorial interactively in your browser without installing anything!

  • Binder: Free cloud-based Jupyter environment
  • Colab: Google's free Jupyter notebook environment

Topics Covered​

This tutorial covers the following topics using Python and SymPy:

  • Parallel vectors and the cross product
  • Shortest distance between skew lines
  • Distance from a point to a line
  • Vector, parametric and Cartesian equations of lines
  • Equations of planes

Introduction​

Vectors are Matrix objects. The cross product is A.cross(B), the dot product is A.dot(B), and A.norm() gives the magnitude ∣a⃗∣|\vec{a}|.

import sympy as sy
from sympy.abc import x, y, z, t
from sympy.solvers import solve
from sympy import sqrt, sin, cos, pi, Matrix, simplify, nsimplify
sy.init_printing(use_latex=True)

Question 1​

Pipe A has diameter 0.80.8 and axis through (2,5,3)(2,5,3) and (7,10,8)(7,10,8). Pipe B has diameter 1.01.0 and axis through (0,6,3)(0,6,3) and (−12,0,9)(-12,0,9). Do the pipes intersect?

The axes are skew lines, so the shortest distance between them is ∣(a−b)⋅n∣n∣∣\left|(\mathbf{a}-\mathbf{b})\cdot\dfrac{\mathbf{n}}{|\mathbf{n}|}\right| where n=dA×dB\mathbf{n}=\mathbf{d}_{A}\times\mathbf{d}_{B}.

a, dA = Matrix([2, 5, 3]),  Matrix([5, 5, 5])       # point and direction of pipe A
b, dB = Matrix([0, 6, 3]), Matrix([-12, -6, 6]) # point and direction of pipe B

n = dA.cross(dB)
display(n, n/30) # 30*<2,-3,1>, i.e. <2,-3,1>

dist = abs((a - b).dot(n)) / sqrt(n.dot(n))
display(dist, sy.N(dist, 4)) # sqrt(14)/2 = 1.871

The sum of the radii is 0.4+0.5=0.9 m0.4+0.5=0.9\ \text{m}, which is less than 1.871 m1.871\ \text{m}, so the pipes do not intersect and no realignment is required.

Question 2​

Determine whether each pair of vectors is parallel.

a1, b1 = Matrix([2, 4, -1]),  Matrix([-6, -12, 3])
display(b1 == -3*a1) # True -> b1 = -3*a1, so parallel

a2, b2 = Matrix([4, 10]), Matrix([2, -9])
display(sy.simplify(a2[0]/b2[0]), sy.simplify(a2[1]/b2[1])) # 2 and -10/9 -> not equal

i. b∼=−3a∼\underset{\sim}{b}=-3\underset{\sim}{a}, so the vectors are parallel.

ii. The two ratios 4/2=24/2=2 and 10/(−9)=−10/910/(-9)=-10/9 differ, so no single scalar works and the vectors are not parallel.

Question 3​

Find the unit vectors perpendicular to a∼\underset{\sim}{a} and b∼\underset{\sim}{b}.

i. a∼=⟨2,4,5⟩\underset{\sim}{a}=\langle 2,4,5\rangle, b∼=⟨1,2,−2⟩\underset{\sim}{b}=\langle 1,2,-2\rangle

a = Matrix([2, 4, 5]); b = Matrix([1, 2, -2])
c = a.cross(b)
display(c) # <-18, 9, 0>
display(c.dot(a), c.dot(b)) # 0, 0 -> perpendicular to both

unit = c / sqrt(c.dot(c))
display(unit) # <-2/sqrt(5), 1/sqrt(5), 0>
display(-unit) # the opposite direction is also valid

The two unit vectors are ±⟨−25,15,0⟩\pm\left\langle-\dfrac{2}{\sqrt{5}},\dfrac{1}{\sqrt{5}},0\right\rangle.

ii. a∼=⟨2,4,−4⟩\underset{\sim}{a}=\langle 2,4,-4\rangle, b∼=⟨1,2,−2⟩\underset{\sim}{b}=\langle 1,2,-2\rangle

a = Matrix([2, 4, -4]); b = Matrix([1, 2, -2])
display(a.cross(b)) # <0, 0, 0> -> a = 2*b, they are parallel

The cross product is the zero vector, so a∼\underset{\sim}{a} and b∼\underset{\sim}{b} are parallel. A unit vector perpendicular to both does not exist (it would require dividing by ∣0∣=0|\mathbf{0}|=0).

note

The printed solution computes a×ba\times b as ⟨−2,−1,0⟩\langle-2,-1,0\rangle with the unit vector ⟨−2/5,−1/5,0⟩\langle-2/\sqrt{5},-1/\sqrt{5},0\rangle. That vector is not even perpendicular to a∼\underset{\sim}{a}: ⟨−2,−1,0⟩⋅⟨2,4,5⟩=−8≠0\langle-2,-1,0\rangle\cdot\langle2,4,5\rangle=-8\neq0. The sign error comes from writing i(−8+10)i(-8+10) instead of i(−8−10)i(-8-10). SymPy gives the correct a×b=⟨−18,9,0⟩a\times b=\langle-18,9,0\rangle.

Question 4​

Find the distance from P(−3,7,4)P(-3,7,4) to the line r=(2,−2,−3)+λ(4,−5,3)r=(2,-2,-3)+\lambda(4,-5,3).

The distance is ∣u×v∣∣u∣\dfrac{|\mathbf{u}\times\mathbf{v}|}{|\mathbf{u}|} where u=(4,−5,3)\mathbf{u}=(4,-5,3) and v=AP→=p−a\mathbf{v}=\overrightarrow{AP}=\mathbf{p}-\mathbf{a}.

p = Matrix([-3, 7, 4])
a = Matrix([2, -2, -3]) # point on the line
u = Matrix([4, -5, 3]) # direction of the line
v = p - a
display(v) # <-5, 9, 7>

cr = u.cross(v)
display(cr) # <-62, -43, 11>
display(sqrt(cr.dot(cr)), sqrt(u.dot(u))) # 3*sqrt(646), 5*sqrt(2)

display(simplify(sqrt(cr.dot(cr)) / sqrt(u.dot(u)))) # 3*sqrt(323)/5

So the distance is 33235≈10.78\dfrac{3\sqrt{323}}{5}\approx10.78.

Question 5​

Distance between the skew lines l:r=a+λul:r=\mathbf{a}+\lambda\mathbf{u} and m:r=b+μvm:r=\mathbf{b}+\mu\mathbf{v}.

a = Matrix([0, 4, -1]);  u = Matrix([1, -3, -2])
b = Matrix([2, -1, 0]); v = Matrix([-3, 1, 2])

display(b - a) # <2, -5, 1>
cr = u.cross(v)
display(cr, sqrt(cr.dot(cr))) # <-4, 4, -8>, 4*sqrt(6)
display(abs((b - a).dot(cr)) / sqrt(cr.dot(cr))) # 3*sqrt(6)/2

The distance is 96=362≈3.674\dfrac{9}{\sqrt{6}}=\dfrac{3\sqrt{6}}{2}\approx3.674.

Question 6​

Line LL through P(3,1,−2)P(3,1,-2) and Q(−2,7,−4)Q(-2,7,-4).

i. Vector equation

P = Matrix([3, 1, -2]); Q = Matrix([-2, 7, -4])
PQ = Q - P
display(PQ) # <-5, 6, -2>

L:r∼=⟨3,1,−2⟩+t⟨−5,6,−2⟩L:\underset{\sim}{r}=\langle 3,1,-2\rangle+t\langle-5,6,-2\rangle.

ii. Parametric equation

display(P + t*PQ)                  # <3 - 5*t, 1 + 6*t, -2 - 2*t>

x=3−5t,y=1+6t,z=−2−2t,t∈Rx=3-5t,\quad y=1+6t,\quad z=-2-2t,\qquad t\in\mathbb{R}.

iii. Cartesian equation

Eliminating the parameter tt gives x−3−5=y−16=z+2−2\dfrac{x-3}{-5}=\dfrac{y-1}{6}=\dfrac{z+2}{-2}. A quick check: every point on the line must make all three ratios equal.

pt = P + 1*PQ                              # t = 1 gives the point Q
display(pt) # <-2, 7, -4>
display([sy.simplify((pt[i] - P[i]) / PQ[i]) for i in range(3)]) # [1, 1, 1]

Question 7​

Cartesian equation of the plane through (1,2,−1)(1,2,-1) perpendicular to the line of intersection of 2x+y+z=22x+y+z=2 and x+2y+z=3x+2y+z=3.

n1 = Matrix([2, 1, 1]); n2 = Matrix([1, 2, 1])
n = n1.cross(n2)
display(n) # <-1, -1, 3>

k = n.dot(Matrix([1, 2, -1]))
display(k) # -6

The plane is −x−y+3z=−6-x-y+3z=-6, i.e. x+y−3z=6x+y-3z=6.

Question 8​

Plane containing L1:r∼=⟨1,−3,4⟩+t⟨2,1,1⟩L_{1}:\underset{\sim}{r}=\langle 1,-3,4\rangle+t\langle 2,1,1\rangle and parallel to L2:r∼=⟨0,0,0⟩+s⟨1,2,3⟩L_{2}:\underset{\sim}{r}=\langle 0,0,0\rangle+s\langle 1,2,3\rangle.

u = Matrix([2, 1, 1]); v = Matrix([1, 2, 3])
n = u.cross(v)
display(n) # <1, -5, 3>
display(n.dot(Matrix([1, -3, 4]))) # 28
display(n.dot(v)) # 0 -> v lies in the plane

So the plane is x−5y+3z=28x-5y+3z=28. Because n⋅v=0\mathbf{n}\cdot\mathbf{v}=0, the normal is perpendicular to the direction of L2L_{2}, which proves L2L_{2} is parallel to the plane.

Question 9​

Plane containing L1:r∼=⟨−2,3,4⟩+t⟨1,2,−1⟩L_{1}:\underset{\sim}{r}=\langle-2,3,4\rangle+t\langle 1,2,-1\rangle and L2:r∼=⟨3,4,0⟩+s⟨−1,−2,1⟩L_{2}:\underset{\sim}{r}=\langle 3,4,0\rangle+s\langle-1,-2,1\rangle.

A = Matrix([-2, 3, 4]); B = Matrix([3, 4, 0])
AB = B - A
display(AB) # <5, 1, -4>

u = Matrix([1, 2, -1])
n = AB.cross(u)
display(n) # <7, 1, 9>

display(n.dot(A), n.dot(B)) # 25, 25 -> both points lie on the plane

So the plane is 7x+y+9z=257x+y+9z=25. (Using the other point gives the same kk, which is a quick consistency check.)

Question 10​

a∼=⟨1,−2,−3⟩\underset{\sim}{a}=\langle 1,-2,-3\rangle, b∼=⟨2,1,−1⟩\underset{\sim}{b}=\langle 2,1,-1\rangle, c∼=⟨1,3,−2⟩\underset{\sim}{c}=\langle 1,3,-2\rangle.

i. a∼⋅b∼(a∼×b∼)\underset{\sim}{a}\cdot\underset{\sim}{b}\left(\underset{\sim}{a}\times\underset{\sim}{b}\right)

a = Matrix([1, -2, -3]); b = Matrix([2, 1, -1]); c = Matrix([1, 3, -2])
display(a.dot(b)) # 3
display(a.cross(b)) # <5, -5, 5>
display(a.dot(b) * a.cross(b)) # <15, -15, 15>

ii. (a∼+b∼)×c∼(\underset{\sim}{a}+\underset{\sim}{b})\times\underset{\sim}{c}

display(a + b)                                # <3, -1, -4>
display((a + b).cross(c)) # <14, 2, 10>

Question 11​

Shortest distance between the skew lines La:ra=4i+2j−6k+t(2i−j−k)L_{a}:r_{a}=4i+2j-6k+t(2i-j-k) and Lb:rb=i−3j−3k+t(i−2j−k)L_{b}:r_{b}=i-3j-3k+t(i-2j-k).

ba = Matrix([2, -1, -1]); bb = Matrix([1, -2, -1])
aa = Matrix([4, 2, -6]); ab = Matrix([1, -3, -3])

cr = ba.cross(bb)
display(cr, sqrt(cr.dot(cr))) # <-1, 1, -3>, sqrt(11)

display(abs((aa - ab).dot(cr)) / sqrt(cr.dot(cr))) # sqrt(11)

The shortest distance is 1111=11≈3.317\dfrac{11}{\sqrt{11}}=\sqrt{11}\approx3.317.


Extra Learning Resources​

Key Concepts to Master​

  1. Parallel test: a⃗×b⃗=0\vec{a}\times\vec{b}=\mathbf{0}
  2. Perpendicular test: a⃗⋅b⃗=0\vec{a}\cdot\vec{b}=0
  3. Distance point to line: ∣u×v∣∣u∣\dfrac{|\mathbf{u}\times\mathbf{v}|}{|\mathbf{u}|}
  4. Distance between skew lines: ∣(b−a)⋅u×v∣u×v∣∣\left|(\mathbf{b}-\mathbf{a})\cdot\dfrac{\mathbf{u}\times\mathbf{v}}{|\mathbf{u}\times\mathbf{v}|}\right|
  5. Plane from a point and normal: n⋅(r−r0)=0\mathbf{n}\cdot(\mathbf{r}-\mathbf{r}_{0})=0

SymPy Resources​

Common Pitfalls​

  • Cross product is not commutative: a.cross(b) == -b.cross(a)
  • Matrix is a column vector, so Matrix([2,5,3]) is a point in 3D, not a row
  • The perpendicular vector from a cross product can be scaled by any non-zero number — always divide by the magnitude to get the unit vector
  • When a plane is given by three points, substitute a point into kk only after computing the normal