calcderror.m

来自「无线传感器节点定位算法」· M 代码 · 共 41 行

M
41
字号
%|
%| PURPOSE:  Calculate the derivative of the error between the model and the data
%|

function [dError] = calcDError(guessBlindLocs)

global refDevices;    % number of reference nodes
global blindDevices;  % number of blind devices
global totalDevices;  % the total number of devices
global linearRefLocs; % locations of the reference devices
global dhat;          % estimated distance between devices based on the measured
                      % received power.
global dfuncEvals;    % counter for number of function evaluations.
dfuncEvals = dfuncEvals + 1;
TINY       = 1e-5;

x = [linearRefLocs(1:refDevices), guessBlindLocs(1:blindDevices)];
y = [linearRefLocs(refDevices+1:2*refDevices), guessBlindLocs(blindDevices+1:2*blindDevices)];
%x         = [0 0 1 1 0.5 0.75];
%y         = [0 1 1 0 0.75 0.5];

%| 1.  Do the preliminary calculations here in order to save time in the
%|     next loop.
for k = refDevices+1 : totalDevices,
   l = [1:k-1];
   modelDistSqr    = max(TINY, (x(k)-x(l)).^2 + (y(k)-y(l)).^2);
   commonTerm(k,l) = log( modelDistSqr ./ (dhat(k,l).^2)) ./ modelDistSqr;
end

commonTerm(:,totalDevices) = zeros(totalDevices,1);

%| 2.  For each device, calculate the partial derivatives.
for k = refDevices+1 : totalDevices,
   dFdx(k)  = sum(commonTerm(k,1:k-1).*(x(k)-x(1:k-1))) + ...
      sum(commonTerm(k+1:totalDevices, k)'.*(x(k)-x(k+1:totalDevices)));
   dFdy(k)  = sum(commonTerm(k,1:k-1).*(y(k)-y(1:k-1))) + ...
      sum(commonTerm(k+1:totalDevices, k)'.*(y(k)-y(k+1:totalDevices)));
end

dError = [dFdx(refDevices+1:totalDevices), dFdy(refDevices+1:totalDevices)];

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?