Error in trilateration code Matlab
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) eg in case of the second node ie 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 ie 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
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.
链接地址: http://www.djcxy.com/p/84954.html下一篇: 三边测量代码Matlab中的错误