Program to calculate angle between two N | 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 ; Given magnitude arrays ; Size of the array ; Function call to find the angle between two vectors
function magnitude ( arr , N ) { var magnitude = 0 ; for ( var i = 0 ; i < N ; i ++ ) magnitude += arr [ i ] * arr [ i ] ; return Math . sqrt ( magnitude ) ; } function dotProduct ( arr , brr , N ) { var product = 0 ; for ( var i = 0 ; i < N ; i ++ ) product = product + arr [ i ] * brr [ i ] ; return product ; } function angleBetweenVectors ( arr , brr , N ) { var dotProductOfVectors = dotProduct ( arr , brr , N ) ; var magnitudeOfA = magnitude ( arr , N ) ; var magnitudeOfB = magnitude ( brr , N ) ; var angle = dotProductOfVectors / ( magnitudeOfA * magnitudeOfB ) ; document . write ( angle . toFixed ( 6 ) ) ; } var arr = [ - 0.5 , - 2 , 1 ] ; var brr = [ - 1 , - 1 , 0.3 ] ; var N = arr . length ; angleBetweenVectors ( arr , brr , N ) ;