function [c nu] = SpeedOfSound( X0, Y0, Tij)
% - This computes an estimate of the speed of sound in the sediment that
% minimizes the error of all the measured distances assuming the supplied
% locations are perfectly accurate.
% - It is assumed that the speed of sound is constant everywhere.
% - X0 and Y0 are vectors of the same length that specify the known
% positions of the transponders.  The length of these vectors (N) is equal to
% the number of transponders.
% - Tij is a matrix that includes the measured travel times between all
% transponders.  Row i and Column j represents the two way travel time of
% the sound that originates and i and is "bounced off" j.  Elements on the
% diagonal have no meaning and are not used.
% - c is returned estimate of the actual speed of sound.
% - nu is a NXN matrix that represents the residual errors in the speed of
% sound for measurement i-j.  Elements on the diagonal are meaningless


N = length(X0);
if(N ~= length(Y0))
    error('Length of X0 not equal to length of Y0');
end

if((N ~= size(Tij,1)) || (N ~= size(Tij,2)))
     error('Size of Tij not consistent with length of X0 and Y0');
end   

A = zeros(N,1);
B = zeros(N,1);
k = 0;
for i = 1:N
    for j = 1:N
      if(i ~= j)
        k = k+1;
        A(k) = Tij(i,j);
        B(k) = 2*sqrt((X0(i)-X0(j))^2+(Y0(i)-Y0(j))^2);
      end
    end
end

c = A\B;

nu = zeros(N,N);
k = 0;
for i = 1:N
    for j = 1:N
      if(i ~= j)
        k = k+1;
        nu(i,j) = B(k)/A(k)-c;
      end
    end
end



end

