12.7: Dot Product
- Page ID
- 88002
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)
( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\id}{\mathrm{id}}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\kernel}{\mathrm{null}\,}\)
\( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\)
\( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\)
\( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)
\( \newcommand{\vectorA}[1]{\vec{#1}} % arrow\)
\( \newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow\)
\( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vectorC}[1]{\textbf{#1}} \)
\( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)
\( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)
\( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)By Carey Smith
The dot product is the sum of the products of the corresponding elements of 2 vectors. Both vectors have to be the same length. Geometrically, it is the product of the magnitudes of the two vectors and the cosine of the angle between them.
Figure \(\PageIndex{1}\): a*cos(θ) is the projection of the vector a onto the vector b.
Relationships:
a·b = |a|*|b|*cos(θ)
cos(θ) = |a|*|b| / (a·b)
The dot product of a vector with itself is the sum of the squares of the vector's elements. The vector's magnitude (length) is the square root of the dot product of the vector with itself.
This video gives details about dot product:
.
Here are examples illustrating the cases of parallel vectors, perpendicular vectors (a.k.a orthogonal), and vectors at 60 degrees relative to each other.
a = [1 2 3];
a_dot_a = dot(a,a) % This is the sum of the squares of the elements of A
% 14
a_mag = sqrt(a_dot_a)
% 3.7417
MATLAB also has this function to compute the magnitude of a vector:
a_mag = norm(a)
The norm function computes the square root of the sum of the squares of the elements of the vector.
Solution
Add example text here.
The dot product of 2 different vectors is equivalent to the product of each vector's magnitude (length) times the cos(angle between the 2 vectors).
When the vectors are parallel, the cos = 1, so the dot product = the product of their magnitudes.
When the vectors are perpendicular, the cos = 0, so the dot product = 0.
a = [1 2 3];
b = 2*a % = [2 4 6]
ab_dot = dot(a,b)
% = 1*2 + 2*4 + 3*6 = 28
|a| = sqrt(1^2 + 2^2 +3^2) = sqrt(14)
|b| = 2*|a| = 2*sqrt(14)
|a|*|b| = 2*14 = 28
Solution
Add example text here.
.
c1 = [1 0 0] % 0 degs
d1 = [0 1 0] % 45 degs
cd1_dot = dot(c1,d1)
% = 0, so these are perpendicular
Solution
Add example text here.
.
c2 = [ 1 1 0] % at 45 degs
d2 = [ 1 -1 0] % at -45 degs
cd2_dot = dot(c2,d2)
% = 0, so these are perpendicular
Solution
Add example text here.
.
e1 = [ 1 0 0] % unit vector at 0 degs
f1 = [ cosd(60) sind(60) 0] % unit vector at 60 degs
% f1 = [0.500 0.866 0]
ef1_dot = dot(e1,f1)
% = 0.500 = cosd(60)
Solution
Add example text here.
.
e2 = 2*[ 1 0 0] % at 0 degs
e2_mag = sqrt(dot(e2,e2)) % = 2
f2 = 5*[ cosd(60) sind(60) 0] % at 60 degs
f2_mag = sqrt(dot(f2,f2)) % = 5
% When they are not unit vectors, we need to divide by their magnitudes.
cos_ef = dot(e2,f2) / (e2_mag*f2_mag)
% = 0.5 = cos(60)
Solution
Add example text here.
.
%% dot_product_cos
% Define these 2 vectors:
a = [2, 1.5, 0]
b = [4, 0 ,0]
% Plot the vectors
figure;
plot([0, a(1)], [0, a(2)], 'b', 'LineWidth', 3)
hold on;
plot([0, b(1)], [0, b(2)], 'r', 'LineWidth', 3)
grid on;
axis equal;
legend('a', 'b')
%% Find the angle between them
a_mag = norm(a)
b_mag = norm(b)
cos_ab = dot(a,b)/(a_mag*b_mag)
theta_degs = acosd(cos_ab) % degrees
%% Draw an arc for the included angle
r = 1; % radius of the arc
ang_vector = linspace(0, theta_degs, 9);
ax = r*cosd(ang_vector);
ay = r*sind(ang_vector);
plot(ax, ay, 'k')
text(1.1, 0.3, '\theta', 'FontSize',16)
Figure \(\PageIndex{2}\): Dot product can be used to compute the angle between 2 vectors.
Solution
Add example text here.
.
Dot Product in 3 dimensions
The dot product can also be computed for 3-D vectors. Consider:
v1 = [a, b, c]
v2 = [x, y, z]
Then dot(v1, v2) = a*x + b*y + c*z
The plot3() function syntax is plot(x, y, z).
v1 = [3 -1 2];
v2 = [-4 1 2];
figure;
plot3([0,v1(1)], [0, v1(2)], [0, v1(3)])
grid on;
hold on;
plot3([0,v2(1)], [0, v2(2)], [0, v2(3)])
dp12 = dot(v1,v2) % = 3*(-4) + (-1)*1 + 2*2 = -9
mag_v1 = sqrt(dot(v1,v1)) % 3.74
mag_v2 = sqrt(dot(v2,v2)) % 4.58
cos_theta = dp12 / (mag_v1 * mag_v2) % -0.5249
theta = acosd(cos_theta) % = 121.7 degrees
Solution
Add example text here.
.
% A jet has heading of alpha = 20 degs.
alpha = 20; % degs
% What is the heading unit vector?
B = [cosd(alpha), sind(alpha), 0] % unit vector
% B = [ 0.9397 0.3420 0]
% The wind is 30 km/s at an angle of 150 degs. So its vector is
wind_vel = 30; % km/s
wind_ang = 150 % degs
A = wind_vel*[cosd(wind_ang), sind(wind_ang), 0]
% A = [-25.9808 15.0000 0]
% What is the component of wind along the jet’s heading?
%Answer:
head_wind = dot(A, B)
% head_wind = -19.2836
Solution
Add example text here.
.