Tutorial 4
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 .
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 and axis through and . Pipe B has diameter and axis through and . Do the pipes intersect?
The axes are skew lines, so the shortest distance between them is where .
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 , which is less than , 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. , so the vectors are parallel.
ii. The two ratios and differ, so no single scalar works and the vectors are not parallel.
Question 3
Find the unit vectors perpendicular to and .
i. ,
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 .
ii. ,
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 and are parallel. A unit vector perpendicular to both does not exist (it would require dividing by ).
The printed solution computes as with the unit vector . That vector is not even perpendicular to : . The sign error comes from writing instead of . SymPy gives the correct .
Question 4
Find the distance from to the line .
The distance is where and .
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 .
Question 5
Distance between the skew lines and .
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 .
Question 6
Line through and .
i. Vector equation
P = Matrix([3, 1, -2]); Q = Matrix([-2, 7, -4])
PQ = Q - P
display(PQ) # <-5, 6, -2>
.
ii. Parametric equation
display(P + t*PQ) # <3 - 5*t, 1 + 6*t, -2 - 2*t>
.
iii. Cartesian equation
Eliminating the parameter gives . 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 perpendicular to the line of intersection of and .
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 , i.e. .
Question 8
Plane containing and parallel to .
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 . Because , the normal is perpendicular to the direction of , which proves is parallel to the plane.
Question 9
Plane containing and .
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 . (Using the other point gives the same , which is a quick consistency check.)
Question 10
, , .
i.
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.
display(a + b) # <3, -1, -4>
display((a + b).cross(c)) # <14, 2, 10>
Question 11
Shortest distance between the skew lines and .
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 .
Extra Learning Resources
Key Concepts to Master
- Parallel test:
- Perpendicular test:
- Distance point to line:
- Distance between skew lines:
- Plane from a point and normal:
SymPy Resources
- SymPy Matrices —
dot,cross,norm,det - SymPy
solve— solving the simultaneous equations that define a plane
Common Pitfalls
- Cross product is not commutative:
a.cross(b) == -b.cross(a) Matrixis a column vector, soMatrix([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 only after computing the normal