MATLAB: Calculating normal on a plane: Can’t get proper results

crossdotMATLABnormalperpendicularplanevectors

I am trying to calculate a normal on a plane using the cross command. When I try to test the result with dot, it seems, the resulting vectors isn't perpendicular to the two vectors I used for cross. What is my problem?
%load data
filename = 'test.xlsx';
sheet = 1;
%x|y|z are vectors, u|v|w is 0|0|0
x = xlsread(filename,sheet,'D1:D10');
y = xlsread(filename,sheet,'E1:E10');
z = xlsread(filename,sheet,'F1:F10');
u = xlsread(filename,sheet,'A1:A10');
v = xlsread(filename,sheet,'B1:B10');
w = xlsread(filename,sheet,'C1:C10');
%get three points from the plane: P1=(0|0|0), P2 and P3
x1fornormal= 0;
y1fornormal= 0;
z1fornormal= 0;
P1fornormal= [x1fornormal y1fornormal z1fornormal];
x2fornormal= x(1,1);
y2fornormal= y(1,1);
z2fornormal= z(1,1);
P2fornormal= [x2fornormal y2fornormal z2fornormal]
x3fornormal= x(2,1);
y3fornormal= y(2,1);
z3fornormal= z(2,1);
P3fornormal= [x3fornormal y3fornormal z3fornormal]
%construction of the normal to the plane
Normal= cross(P2fornormal,P3fornormal)
dot(Normal,P2fornormal)==0 & dot(Normal,P3fornormal)==0

Best Answer

You should not be requiring an exact zero for those dot products, since in most circumstances your computations will involve round-off errors. Provide a tolerance for a difference from zero in accordance with the amount of error which you would reasonably expect.
Remember, Matlab’s ‘double’ format has only 53 bits in its significand (mantissa) which is equivalent to about 16 decimal places. Do something like this:
abs(dot(Normal,P2fornormal))<tol & abs(dot(Normal,P3fornormal))<tol
where ‘tol’ is approximately a few ‘eps’ times the product of the magnitudes you expect for ‘Normal’ and ‘P2’ or ‘P3’, the quantity ‘eps’ being 2^(-53),