三边测量代码Matlab中的错误

下面写的是三边测量的代码。 例如在第二个节点的情况下,例如x(2)= 4和y(2)= 4,在由“ye”和“xe”表示的节点的估计位置(代码的结尾)中引入非常小的误差位置是xe(2)= 3.999999999999999,ye(2)= 3.999999999999999而不是4,4。 类似地,第三个节点即x(3)= 3,y(3)= 0,估计的位置是xe(3)= 3(这是可以的)和ye(3)= - 4.440892098500626e-16而不是零。 请提出造成此错误的原因以及如何将其删除。 最好的祝福。

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

默认情况下,在MATLAB中,您创建的每个数字都是一个64位双精度浮点数(或“double”)。 你可以验证这个,例如通过检查isa(42, 'double')的结果isa(42, 'double')

你提到的'错误'是由于整数不一定具有精确的浮点表示这一事实。 在实践中,这不是问题(10e-16是一个很小的数字,对吧?)。 你的代码(至少从这个角度来看,我没有检查你的数学)是好的。 别担心。

查看维基百科或类似的资料来获得更多关于如何构建浮点数的信息。

链接地址: http://www.djcxy.com/p/84953.html

上一篇: Error in trilateration code Matlab

下一篇: 2d trilateration