There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply () below.
If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.
Join the Discussion. Help a fellow learner on their journey.
Ask or answer a question about this exercise by clicking reply () below!
You can also find further discussion and get answers to your questions over in Language Help.
Agree with a comment or answer? Like () to up-vote the contribution!
I’m getting conflicting answers when trying to find the angle between [-17, 22] and [0, 32]. Playing with the applet, it’s clear that the angle should be approximately 40 degrees. But when I try to calculate it, I get a value that’s very near to 90. What am I doing wrong?
>>> from numpy import dot, arccos
>>> from math import degrees
>>> a = [-17, 22]
>>> b = [0, 32]
>>> dot_ab = dot(a, b)
>>> dot_ab
704
>>> mag_a = dot(a, a)
>>> mag_a
773
>>> mag_b = dot(b, b)
>>> mag_b
1024
>>> angle_ab = arccos(dot_ab / (mag_a * mag_b))
>>> degrees(angle_ab)
89.94904158653507
This annoyed me for way longer than i planned. I wrote the functions myself and am getting correct answers.
#lesson example
a = [3, 2,-3]
b = [0, -3,-6]
#test example #a = [ -17, 22] #b = [0, 32]
dot product
dot =
for i in range(len(a)):
mult = a[i] * b[i]
dot.append(mult)
dot_ab = sum(dot)
magnitude
def mag(vector_a, vector_b):
mag =
for i in range(len(vector_a)):
if len(vector_a) != len(vector_b):
print(‘Oi what the ■■■■ do you think your doing!!’)
else:
mult = vector_a[i] * vector_b[i]
mag.append(mult)
return sum(mag) ** 0.5
mag_a = mag(a, a)
mag_b = mag(b, b)
from numpy import arccos
from math import degrees
angle_ab = arccos(dot_ab / (mag_a * mag_b))
angle_ab
1.1795022111693247(ex A) or 0.65788860518221(ex B)
degrees(angle_ab)
67.5804986263507 or 37.69424046668917
Thanks Jid! I came back 11 days later during the quiz section of the course to try and figure this out again. Youve saved me as much hours as youve put it!!
# Codecademy Tutoral Angle between vectors
# lib
import numpy as np
# arrays
a = np.array([3, -1, 2])
b = np.array([0, -1, 1])
# Dot product
dot_ab = np.dot(a, b)
dot_ab
# two find the angle between two vectors, we rely on the dot product between the two vectors and use the following
# dot between vectors is the magnitude
mag_a = np.array(np.sqrt(np.dot(a,a)))
print('Magnitude of vector A:', mag_a)
mag_b = np.array(np.sqrt(np.dot(b,b)))
print('Magnitude of vecor B:', mag_b)
from numpy import arccos
from math import degrees
angle_ab = arccos(dot_ab / (mag_a * mag_b))
print('Angle Between Vectors:', angle_ab)
degrees = (degrees(angle_ab))
print('Degrees Between Vectors:', degrees)
# (55.46)