Possible error with dot product

10 ビュー (過去 30 日間)
William Kett
William Kett 2021 年 9 月 22 日
コメント済み: William Kett 2021 年 10 月 4 日
There appears to be an issue with the dot function which computes the dot product between 2 vectors. Either that, or I am using it incorrectly.
xa = -4.2162; % x-component of vector a
ya = -7.1335;% y-component of vector a
xb = -1.4941e-08; % x-component of vector b
yb = 1.7922e-08; % y-component of vector b
% dot product = a_i*b_i + a_j*b_j =|a|*|b|*cos(theta)
%% Get values for above formulae
dotprod1 = xa * xb + ya * yb
dotprod1 = -6.4852e-08
theta = rad2deg(acos((xa*xb+ya*yb)/(sqrt(xa^2+ya^2)*sqrt(xb^2+yb^2))));
dotprod2 = sqrt(xa^2+ya^2)*sqrt(xb^2+yb^2) * cos(deg2rad(theta))
dotprod2 = -6.4852e-08
dotprod1 == dotprod2 % Should give 1
ans = logical
1
%% Using the dot function
% a = a_i + a_j
% b = b_i + b_j
dotprod3 = dot(xa+ya,xb+yb) % dot of full vectors a and b
dotprod3 = -3.3833e-08
dotprod1 == dotprod3
ans = logical
0
Why isn't dot producing the same value?

採用された回答

Geoff Hayes
Geoff Hayes 2021 年 9 月 22 日
William - from dot, this function returns the scalar dot product of the two input vectors. So in your case, the
dot(xa+ya,xb+yb)
is equivalent to
(xa + ya) * (xb + yb)
which is not the same as what you calculated previously as
dotprod1 = (xa * xb) + (ya * yb) % I added the brackets
Note also that your code and comment
dotprod1 == dotprod2 % Should give 1
is not necessarily true for floating point numbers. See Compare Floating-Point Numbers for details.
  1 件のコメント
William Kett
William Kett 2021 年 10 月 4 日
Thank you for your response. I needed to use this for computing advection and was uncertain if dot worked.

サインインしてコメントする。

その他の回答 (0 件)

製品


リリース

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by