Cross product of two vectors not perpendicular to the input vectors
7 次查看(过去 30 天)
显示 更早的评论
采纳的回答
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
0 个评论
更多回答(1 个)
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
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Numbers and Precision 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!