0

Written below is code for trilateration. A very small error is introduced in the estimated positions of nodes represented by "ye" and "xe" (end of the code) e.g in case of the second node i.e. x(2)=4 and y(2)=4 the estimated position is xe(2)=3.999999999999999 and ye(2)=3.999999999999999 instead of 4,4. Similarly, third node i.e x(3)=3, y(3)=0 the estimated positions are xe(3)=3(which is okay) and ye(3)=-4.440892098500626e-16 instead of zero. Please suggest what is causing this error and how to remove it. Best Regards.

x = [1,4,3];    % X coordinates of the three nodes to be localized
y = [4,4,0];    % X coordinates of the three nodes to be localized

% X and Y coordinates of the three antennas(a, b and c) that will be used
% to localize the three nodes mentioned above
xa=2; ya=3;
xb=1; yb=2;
xc=3; yc=2;

for i=1:3
% Find distances from user to all 3 transmitters:
da = sqrt((x(i)-xa)^2+(y(i)-ya)^2);
db = sqrt((x(i)-xb)^2+(y(i)-yb)^2);
dc = sqrt((x(i)-xc)^2+(y(i)-yc)^2);

va = ((db*db-dc*dc) - (xb*xb-xc*xc) - (yb*yb-yc*yc)) / 2;
vb = ((db*db-da*da) - (xb*xb-xa*xa) - (yb*yb-ya*ya)) / 2;

temp1 = vb*(xc-xb) - va*(xa-xb);
temp2 = (ya-yb)*(xc-xb) - (yc-yb)*(xa-xb);

% Estimated user position:
ye(i) = temp1 / temp2;
xe(i) = (va - y(i)*(yc-yb)) / (xc-xb);
end
Tariq Islam
  • 97
  • 10

1 Answers1

0

By default, in MATLAB every number you create is a 64-bit double-precision floating point number (or 'double'). You can verify this, for instance by checking the result of isa(42, 'double').

The 'error' you mention is due to the fact that integers don't necessarily have an exact floating point representation. In practice, this is not a problem (10e-16 is a pretty small number, right?). Your code (at least from this perspective, I didn't check your math) is fine. Don't worry.

Check Wikipedia or a similar source for more information on how floating point numbers are built up.

Tom
  • 2,674
  • 1
  • 25
  • 33