Vectors and Vector Operations

Nerd Cafe

1. What Is a Vector?

A vector is a mathematical quantity that has:

  • Magnitude (length)

  • Direction

Example:

Displacement of 5 km North is a vector, but just 5 km is a scalar.

2. Vector Representation

A vector in 2D space is written as:

V1=[v1v2]\overrightarrow{V_{1}}=\begin{bmatrix} v_{1} \\ v_{2} \end{bmatrix}
import numpy as np

v = np.array([3, 4])
print(v)

Output:

3. Types of Vectors

  • Zero vector: [0,0]

  • Unit vector: Magnitude is 1

  • Equal vectors: Same magnitude and direction

  • Opposite vectors: Same magnitude, opposite direction

4. Vector Operations

4.1 Addition & Subtraction

Mathematics:

A+B=[a1+b1a2+b2]\overrightarrow{A}+\overrightarrow{B}=\begin{bmatrix} a_{1}+b_{1} \\ a_{2}+b_{2} \end{bmatrix}

Python:

Output:

4.2 Scalar Multiplication

Mathematics:

k.A=[k.a1k.a2]k.\overrightarrow{A}=\begin{bmatrix} k.a_{1} \\ k.a_{2} \end{bmatrix}

Python:

Output:

4.3 Dot Product

Mathematics:

A.B=a1.b1+a2.b2\overrightarrow{A}.\overrightarrow{B}=a_{1}.b_{1}+a_{2}.b_{2}

Result is a scalar.

Python:

Output:

Use to find angle or test orthogonality.

4.4 Cross Product (Only in 3D)

Mathematics:

A×B=[a2b3a3b2a3b1a1b3a1b2a2b1]\overrightarrow{A}\times \overrightarrow{B}=\begin{bmatrix} a_{2}b_{3}-a_{3}b_{2} \\ a_{3}b_{1}-a_{1}b_{3} \\ a_{1}b_{2}-a_{2}b_{1} \end{bmatrix}

Python:

Output:

4.5 Magnitude or Norm

Mathematics:

A=a12+a22\left| \overrightarrow{A} \right|=\sqrt{{a_{1}^{2}}+{a_{2}^{2}}}

Python:

Output:

4.6 Unit Vector

Mathematics:

A^=AA\hat{A}=\frac{\overrightarrow{A}}{\left| \overrightarrow{A} \right|}

Python:

Output:

4.7 Angle Between Vectors

Mathematics:

θ=cos1(A.BAB)\theta=cos^{-1}(\frac{\overrightarrow{A}.\overrightarrow{B}}{|\overrightarrow{A}||\overrightarrow{B}|})

Python:

Output:

5. Vector Visualization in 2D & 3D

5.1 Plotting a Vector in 2D

Let’s plot a vector from the origin to point (x, y).

Python Code:

plt.quiver() draws an arrow from the origin.

Output:

5.2 Plotting Multiple Vectors in 2D

Output:

5.3 Plotting Vectors in 3D

6. Plot Vector Addition Graphically

Output:

Last updated