1

I'm trying to use the code from this SO question (https://stackoverflow.com/a/13849249) to calculate an angle between two vectors. However I am unable to get the angle to print/show

I have tried print(angle_between) from both the indent and non-indented position. I have also tried return(angle_between) from the indented and non-indented position. I did however not include what I though was just the explanation for the code.

import numpy as np

 def unit_vector(vector):
    np.linalg.norm(vector)

def angle_between(p_Sun, p_Titan):

   v1_u = unit_vector(p_Sun)
   v2_u = unit_vector(p_Titan)
   return np.arccos(np.clip(np.dot(v1_u, v2_u), -1.0, 1.0))
print(angle_between(p_Sun, p_Titan))

I expect it to print the angle in radians, however I instead get the error .

"TypeError: unsupported operand type(s) for *: 'NoneType' and 'NoneType'"

When I calculate the angle in a different manner (from the same post just a different comment, https://stackoverflow.com/a/2827475) I can use the print() function and get the angle. However, I do not like this method as it fails for certain vector pairs.

M_Coops
  • 15
  • 3

1 Answers1

0

The unit_vector function should be written as:

def unit_vector(vector):    
    return vector / np.linalg.norm(vector)

Currently, your function isn't returning anything, so by default it returns None. That is why you get the error about multiplying NoneTypes. Additionally, it calculates a single value, the norm. For a unit vector, you must divide the current vector by the norm. Hope that helps!

minterm
  • 245
  • 3
  • 13