[Lab02] Dot Product

Manipulate[
Module[{vv=If[unit,Normalize[v],v],uw=Normalize[w],ww,dot,
angles,proj},
ww=If[unit,uw,w];
dot=vv.ww;
proj=dot*uw;
angles=Sort[N@{ArcTan@@vv,ArcTan@@ww}];
Graphics[{
{GrayLevel[.5],Circle[]},
{If[dot>0,Hue[.7],Hue[1]],Circle[{0,0},.3,If[-Subtract@@angles<π,angles,Reverse[angles]+{0,2π}]]},
If[unit&&showDot,{Directive[GrayLevel[.5],Dashed],Line[{vv,proj}]},{}],
If[showDot,{If[dot>0,Directive[Hue[.7],Thick],Directive[Hue[1],Thick]],Line[{{0,0},proj}]},{}],
Arrow[{{0,0},vv}],
Arrow[{{0,0},ww}]
},Axes->True,Ticks->None,PlotRange->3,PlotLabel->Grid[{{If[dot>0,Style["acute angle",Hue[.7]],Style["obtuse angle",Hue[1]]],Row[{"(OverVector[v]) · (OverVector[w]) = ",dot}],If[dot>0,Style["positive",Hue[.7]],Style["negative",Hue[1]]]}},ItemSize->10],ImageSize->{400,400}]],
{{unit,False,"use unit vectors"},{True,False}},
{{showDot,False,"show dot product"},{True,False}},
{{v,{1.,0.}},{-3,-3},{3,3},Locator,Appearance->None},
{{w,{-2.,1.}},{-3,-3},{3,3},Locator,Appearance->None}
]

CAPTION

Drag either of the two vectors to move them. The angle between the vectors is shown (in blue when acute and in red when obtuse). The unit circle is shown for scale. The vectors can be constrained to be unit vectors, in which case the dot product is the cosine of the angle between them.

The dot product is a number, not a vector. When "show dot product" is checked, a colored line segment is shown along one of the vectors. If the segment is blue, the dot product is simply the length of this segment; if the segment is red, the dot product is the negative of its length. For unit vectors, the length of the colored segment is precisely the length of the projection of one vector onto the other. For other vectors, the length is scaled by the product of the magnitudes of the vectors. In concise terms: (OverVector[v])·(OverVector[w])=‖(OverVector[v])‖ ‖(OverVector[w])‖ cos(θ), where θ is the angle between the vectors.

The projection is only shown from the first vector to the second. You could, of course, project the second vector onto the first to produce a segment of equal length. In fact, you can simply switch the positions of the two vectors to see the other projection.

THIS NOTEBOOK IS THE SOURCE CODE FROM

"Dot Product" from the Wolfram Demonstrations Project
 http://demonstrations.wolfram.com/DotProduct/

Contributed by: Bruce Torrence

A full-function Wolfram Mathematica system (Version 6 or higher) is required to edit this notebook.
GET WOLFRAM MATHEMATICA »

© Wolfram Demonstrations Project & Contributors   |   Terms of Use   |   Make a new version of this Demonstration »