Clear Filters
Clear Filters

Cross product of two vectors not perpendicular to the input vectors

7 views (last 30 days)
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

Accepted Answer

DGM
DGM on 31 Mar 2021
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

More Answers (1)

Justin Ferland
Justin Ferland on 31 Mar 2021
Edited: Justin Ferland on 31 Mar 2021
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

Tags

Community Treasure Hunt

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

Start Hunting!