Calculating the angle between two vectors

12.2k Views Asked by At

I want to ask a question about the angle between two vectors.

I am a Chemistry student who is studying the bond angle between 2 Hydrogen atoms using Python.

I recall from final-year high school the following property of angles is observed between vectors:

$$\cos \theta = \frac{a\cdot b}{|a||b|}$$

and have been given the following three dimensional vectors in cartesian form:

[0.0, 0.0, 0.102249] (Sulfur)
[0.0, 0.968059, -0.817992] (Hydrogen 1)
[0.0, -0.968059, -0.817992] (Hydrogen 2)

A diagram is supplied below.

Diagram representing vectors of concern

I know the vectors of concern are Hydrogen 1 and Hydrogen 2.

I know to take their dot product to calculate the ${a\cdot b}$ term of the fraction.

However, I have been asked to make use of numpy's norm() function, which returns a vector or matrix form.

From what I seem to make out, a vector norm in this instance is apparently the same as the length of the vector i.e. the modulus or $|a|$ of vector $a$ but I'm not sure if this is correct.

What does the norm of a vector serve as a purpose to the calculation of the angle between two vectors $a$ and $b$?

1

There are 1 best solutions below

2
On BEST ANSWER

Since the norm is just the magnitude or modulus or whatever you call it, this code should do the trick:

from numpy import arccos, array
from numpy.linalg import norm

# Note: returns angle in radians
def theta(v, w): return arccos(v.dot(w)/(norm(v)*norm(w)))

sulfur = array([0.0, 0.0, 0.102249])
hydrogen_1 = array([0.0, 0.968059, -0.817992])
hydrogen_2 = array([0.0, -0.968059, -0.817992])
print(theta(hydrogen_1-sulfur, hydrogen_2-sulfur))

The numpy function is documented here.