Skip to main content

Tutorial 3

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:

  • Vectors: position vectors, magnitude, scalar multiplication
  • Unit vectors and direction cosines
  • Vector, parametric and Cartesian equations of lines
  • Dot product and cross product
  • Gradient, divergence and curl

Introduction​

A vector is just a Matrix with one column. Matrix([2, 4]) is the position vector ⟨2,4⟩\langle 2, 4\rangle and Matrix([1, 3, -7]) is i∼+3j∼−7k∼\underset{\sim}{i}+3\underset{\sim}{j}-7\underset{\sim}{k}.

import sympy as sy
from sympy.abc import x, y, z, t
from sympy.solvers import solve
from sympy import sqrt, tan, sin, cos, sec, pi, ln, exp
from sympy import atan, acos, asin, diff, Matrix, simplify, nsimplify
sy.init_printing(use_latex=True)

# --- handy helpers used in Q7, Q8 and Q9 ---
def gradient(f, vars=(x, y, z)):
"""Gradient of a scalar field, returned as a column vector."""
return Matrix([diff(f, v) for v in vars])

def divergence(F, vars=(x, y, z)):
"""Divergence of a vector field given as a list/Matrix of components."""
return sum(diff(F[i], vars[i]) for i in range(len(vars)))

def curl(F):
"""Curl of a 3D vector field given as a list/Matrix of components."""
return Matrix([diff(F[2], y) - diff(F[1], z),
diff(F[0], z) - diff(F[2], x),
diff(F[1], x) - diff(F[0], y)])

Question 1​

Sketch P(3,−1,5)P(3,-1,5) and Q(2,1,−1)Q(2,1,-1) and find the distance between them.

P = Matrix([3, -1, 5])
Q = Matrix([2, 1, -1])
PQ = Q - P
display(PQ, sqrt(PQ.dot(PQ))) # <-1,2,-6>, sqrt(41)

The distance is 12+(−2)2+(−6)2=41\sqrt{1^{2}+(-2)^{2}+(-6)^{2}}=\sqrt{41}.

Question 2​

For a∼=⟨2,4⟩\underset{\sim}{a}=\langle 2,4\rangle, compute 3a∼3\underset{\sim}{a}, 12a∼\frac{1}{2}\underset{\sim}{a} and −2a∼-2\underset{\sim}{a}.

a = Matrix([2, 4])
display(3*a) # <6, 12>
display(a/2) # <1, 2>
display(-2*a) # <-4, -8>

Scalar multiplication never changes the direction of a vector, only its length — except for a negative scalar, which reverses the direction. A scalar larger than 11 stretches the vector, a scalar between 00 and 11 shrinks it, and −2-2 doubles the length while flipping it through 180∘180^\circ.

Question 3​

Given OP→=i∼+3j∼−7k∼\overrightarrow{OP}=\underset{\sim}{i}+3\underset{\sim}{j}-7\underset{\sim}{k} and OQ→=5i∼−2j∼+4k∼\overrightarrow{OQ}=5\underset{\sim}{i}-2\underset{\sim}{j}+4\underset{\sim}{k}.

i. Find the unit vector in the direction of PQ→\overrightarrow{PQ}.

OP = Matrix([1, 3, -7])
OQ = Matrix([5, -2, 4])
PQ = OQ - OP
display(PQ) # <4, -5, 11>
display(sqrt(PQ.dot(PQ))) # 9*sqrt(2)
display(PQ / sqrt(PQ.dot(PQ))) # (1/(9*sqrt(2)))<4,-5,11>

ii. Find the direction cosines of PQ→\overrightarrow{PQ}.

unit_PQ = PQ / sqrt(PQ.dot(PQ))
display(list(unit_PQ)) # [cos(alpha), cos(beta), cos(gamma)]

So cos⁡α=492\cos\alpha=\frac{4}{9\sqrt{2}}, cos⁡β=−592\cos\beta=\frac{-5}{9\sqrt{2}} and cos⁡γ=1192\cos\gamma=\frac{11}{9\sqrt{2}}.

iii. Find the vector of magnitude 55 in the direction of QP→\overrightarrow{QP}, in polar form.

display(5 * (-unit_PQ))                          # the vector itself
angles = [acos(e) * 180/pi for e in (-unit_PQ)] # polar angles of QP
display([sy.N(a, 6) for a in angles]) # 108.32, 66.87, 149.80 degrees

The vector of magnitude 55 in the direction of QP→\overrightarrow{QP} is 5⟨cos⁡108.32∘,cos⁡66.87∘,cos⁡149.80∘⟩5\langle\cos 108.32^\circ,\cos 66.87^\circ,\cos 149.80^\circ\rangle.

Question 4​

A(1,2)A(1,2) and B(3,4)B(3,4).

i. Vector equation of the line LL through AA and BB.

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

So L:r∼=⟨1,2⟩+t⟨2,2⟩L:\underset{\sim}{r}=\langle 1,2\rangle+t\langle 2,2\rangle.

ii. Points on the line for t=0,1,…,5t=0,1,\ldots,5.

display([(ti, A + ti*AB) for ti in range(6)])
ttr∼\underset{\sim}{r}
0⟨1,2⟩\langle 1,2\rangle
1⟨3,4⟩\langle 3,4\rangle
2⟨5,6⟩\langle 5,6\rangle
3⟨7,8⟩\langle 7,8\rangle
4⟨9,10⟩\langle 9,10\rangle
5⟨11,12⟩\langle 11,12\rangle

Question 5​

A unit vector a⃗\vec{a} makes an angle of π/3\pi/3 with ii, π/4\pi/4 with jj and an acute angle θ\theta with kk. Find θ\theta and the components of a⃗\vec{a}.

The components of a unit vector are exactly its direction cosines, and cos⁡2α+cos⁡2β+cos⁡2γ=1\cos^{2}\alpha+\cos^{2}\beta+\cos^{2}\gamma=1.

cos_a = cos(pi/3)
cos_b = cos(pi/4)
cos2_g = 1 - cos_a**2 - cos_b**2
display(simplify(cos2_g)) # 1/4

theta = acos(sqrt(cos2_g)) # acute angle only
display(theta, sy.N(theta*180/pi, 6)) # pi/3, 60 degrees

a = Matrix([cos_a, cos_b, cos(theta)])
display(simplify(a), sqrt(a.dot(a))) # <1/2, 1/sqrt(2), 1/2>, |a| = 1

Hence θ=60∘\theta=60^\circ and a⃗=12i+12j+12k\vec{a}=\frac{1}{2}i+\frac{1}{\sqrt{2}}j+\frac{1}{2}k.

note

The printed solution also lists the 120∘120^\circ case a⃗=12i+12j−12k\vec{a}=\frac{1}{2}i+\frac{1}{\sqrt{2}}j-\frac{1}{2}k. Because the question specifies an acute angle, that case must be discarded — taking acos of the positive root gives the acute angle directly.

Question 6​

If a⃗\vec{a} is a unit vector and (x⃗−a⃗)⋅(x⃗+a⃗)=8(\vec{x}-\vec{a})\cdot(\vec{x}+\vec{a})=8, find ∣x⃗∣|\vec{x}|.

Expanding the dot product gives ∣x⃗∣2−∣a⃗∣2=8|\vec{x}|^{2}-|\vec{a}|^{2}=8, and ∣a⃗∣=1|\vec{a}|=1.

X = sy.Symbol('X', nonnegative=True)          # X = |x|
display(solve(sy.Eq(X**2 - 1, 8), X)) # [3]

So ∣x⃗∣=3|\vec{x}|=3 (the negative root is rejected because a magnitude cannot be negative).

Question 7​

Find the gradient of f=2x2+yx2−y2f=\dfrac{2x^{2}+y}{x^{2}-y^{2}}.

f = (2*x**2 + y)/(x**2 - y**2)
grad_f = gradient(f, (x, y))
display(simplify(grad_f[0])) # -2*x*y*(2*y + 1)/(x**2 - y**2)**2
display(simplify(grad_f[1])) # (x**2 + 4*x**2*y + y**2)/(x**2 - y**2)**2

∇f=2xy(−2y−1)(x2−y2)2 i+x2+4x2y+y2(x2−y2)2 j\nabla f=\frac{2xy(-2y-1)}{(x^{2}-y^{2})^{2}}\,\boldsymbol{i}+\frac{x^{2}+4x^{2}y+y^{2}}{(x^{2}-y^{2})^{2}}\,\boldsymbol{j}

Question 8​

Calculate the divergence of the vector fields.

a. F=y3i+xyjF=y^{3}\boldsymbol{i}+xy\boldsymbol{j}

display(divergence(Matrix([y**3, x*y]), (x, y)))       # x

b. G=4yx2i+sin⁡(y)j+3kG=\dfrac{4y}{x^{2}}\boldsymbol{i}+\sin(y)\boldsymbol{j}+3\boldsymbol{k}

display(simplify(divergence(Matrix([4*y/x**2, sin(y), 3]))))   # cos(y) - 8*y/x**3

c. G=exi+ln⁡(xy)j+exyzkG=e^{x}\boldsymbol{i}+\ln(xy)\boldsymbol{j}+e^{xyz}\boldsymbol{k}

display(simplify(divergence(Matrix([exp(x), ln(x*y), exp(x*y*z)]))))   # e**x + 1/y + x*y*e**(x*y*z)

Question 9​

Calculate the curl of the vector fields.

a. F=3x2i+2zj−xkF=3x^{2}\boldsymbol{i}+2z\boldsymbol{j}-x\boldsymbol{k}

display(curl(Matrix([3*x**2, 2*z, -x])))               # <-2, 1, 0>

b. F=y3i+xyj−zkF=y^{3}\boldsymbol{i}+xy\boldsymbol{j}-z\boldsymbol{k}

display(curl(Matrix([y**3, x*y, -z])))                 # <0, 0, y - 3*y**2>

The curl points along k\boldsymbol{k} only.

c. F=(1+y+z2)i+exyzj−(xyz)kF=(1+y+z^{2})\boldsymbol{i}+e^{xyz}\boldsymbol{j}-(xyz)\boldsymbol{k}

display(simplify(curl(Matrix([1 + y + z**2, exp(x*y*z), -x*y*z]))))

∇×F=−(xz+xy exyz) i+(2z+yz) j+(yz exyz−1) k\nabla\times F=-(xz+xy\,e^{xyz})\,\boldsymbol{i}+(2z+yz)\,\boldsymbol{j}+(yz\,e^{xyz}-1)\,\boldsymbol{k}

note

The printed solution drops the minus sign on F3=−xyzF_{3}=-xyz in the i\boldsymbol{i} and j\boldsymbol{j} components and mislabels the j\boldsymbol{j} term as k\boldsymbol{k}. SymPy differentiates each component literally, so the signs come out right.


Extra Learning Resources​

Key Concepts to Master​

  1. Magnitude: ∣a⃗∣=a12+a22+a32|\vec{a}|=\sqrt{a_{1}^{2}+a_{2}^{2}+a_{3}^{2}}
  2. Unit vector: a^=a⃗/∣a⃗∣\hat{a}=\vec{a}/|\vec{a}|
  3. Direction cosines: the components of a unit vector, with cos⁡2α+cos⁡2β+cos⁡2γ=1\cos^{2}\alpha+\cos^{2}\beta+\cos^{2}\gamma=1
  4. Cross product: a⃗×b⃗\vec{a}\times\vec{b} is perpendicular to both, with ∣a⃗×b⃗∣=∣a⃗∣∣b⃗∣sin⁡θ|\vec{a}\times\vec{b}|=|\vec{a}||\vec{b}|\sin\theta
  5. Del operator: ∇f\nabla f (gradient, a vector), ∇⋅F\nabla\cdot F (divergence, a scalar), ∇×F\nabla\times F (curl, a vector)

SymPy Resources​

Common Pitfalls​

  • Matrix([1,2,3]) is a column vector; dot and cross only work on Matrix objects, not plain lists
  • Use ** for powers, and remember sec(x) = 1/cos(x)
  • solve returns a list — index it ([0]) or filter it before using a root
  • Always declare an applied variable as positive (sy.Symbol('r', positive=True)) to discard non-physical roots