Program to calculate angle between two N | Python3 program for the above approach ; Function to find the magnitude of the given vector ; Stores the final magnitude ; Traverse the array ; Return square root of magnitude ; Function to find the dot product of two vectors ; Stores dot product ; Traverse the array ; Return the product ; Stores dot product of two vectors ; Stores magnitude of vector A ; Stores magnitude of vector B ; Stores angle between given vectors ; Print the angle ; Driver Code ; Given magnitude arrays ; Size of the array ; Function call to find the angle between two vectors
import math NEW_LINE def magnitude ( arr , N ) : NEW_LINE INDENT magnitude = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT magnitude += arr [ i ] * arr [ i ] NEW_LINE DEDENT return math . sqrt ( magnitude ) NEW_LINE DEDENT def dotProduct ( arr , brr , N ) : NEW_LINE INDENT product = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT product = product + arr [ i ] * brr [ i ] NEW_LINE DEDENT return product NEW_LINE DEDENT def angleBetweenVectors ( arr , brr , N ) : NEW_LINE INDENT dotProductOfVectors = dotProduct ( arr , brr , N ) NEW_LINE magnitudeOfA = magnitude ( arr , N ) NEW_LINE magnitudeOfB = magnitude ( brr , N ) NEW_LINE angle = ( dotProductOfVectors / ( magnitudeOfA * magnitudeOfB ) ) NEW_LINE print ( ' % .5f ' % angle ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ - 0.5 , - 2 , 1 ] NEW_LINE brr = [ - 1 , - 1 , 0.3 ] NEW_LINE N = len ( arr ) NEW_LINE angleBetweenVectors ( arr , brr , N ) NEW_LINE DEDENT