フィルターのクリア

Cross product of two vectors not perpendicular to the input vectors

7 ビュー (過去 30 日間)
StevenC
StevenC 2021 年 3 月 31 日
コメント済み: StevenC 2021 年 3 月 31 日
Why is the following output vector C, not perpendiular to the other two input vectors?
a = 20; b =20;
v1 = [cosd(a) sind(a) 0];
v2 = [cosd(b) 0 -sind(b)];
C = cross(v1,v2);
%check if perpendicular fails:
dot(C,v1)==0 & dot(C,v2) == 0

採用された回答

DGM
DGM 2021 年 3 月 31 日
Ah, yeah. Beware using equality tests when you're dealing with floating point math. The result from dot(C,v1) isn't zero; it's 1.3878E-17, i.e. approximately zero. One way to deal with the issue is:
% pick some tolerance
tol=1E-12;
dot(C,v1)<tol & dot(C,v2)<tol
I'm sure there are other ways

その他の回答 (1 件)

Justin Ferland
Justin Ferland 2021 年 3 月 31 日
編集済み: Justin Ferland 2021 年 3 月 31 日
The vectors are in fact perpendicular. The cross product of two will give you a vector that is perpendicular to the two input vectors.
The reason you are not getting the desired result is due to how matlab and computers in general store numbers.
computers generally store numbers as floating point numbers. An example of this is the fractional number 1/3 a computer would store this as the decimal number 0.3333333333 with some finite number of threes. In the same way sometimes matlab will approximate 0 as some very small finite number. In you case dot(C,v1) gives a number that is in the order of 10^-17 which is extremely small but not zero
to solve this it could be useful to work with symbolic variables this is a variable class simmilar to variables of class double except they store their values symbolic quantities ie x = 1/3 in computer terms means 0.33333333333.... with some finite amount of zeros while x = sym(1/3) is the same as 1/3
to apply this in your program you could make use of the sym function
a = sym(20)
b =sym(-20)
v1 = ([cosd(a) sind(a) 0])
v2 = ([cosd(b) 0 -sind(b)])
C = cross(v1,v2)
% %check if perpendicular fails:
dot(C,v1)==0 && dot(C,v2) == 0
  1 件のコメント
StevenC
StevenC 2021 年 3 月 31 日
Thank you, I'm happy to learn about the sym function for this purpose.

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

カテゴリ

Help Center および File ExchangeNumbers and Precision についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by