sat_longitudes=[-62.3256, -85.2234, -92.8388];
sat_latitudes=[7.8520, 45.1053, 29.7834];
sat_PR_times=[0, -0.003817986, -0.004077077];

sat_longitudes=sat_longitudes*pi/180;
sat_latitudes=sat_latitudes*pi/180;

c=299860*1000;

% 33.77152093947228 N, 84.38938929815090 W

[location(1) location(2) location(3)]=sph2cart(-84.38938929815090*pi/180, 33.77152093947228*pi/180,  6380308);
location=location';

%location = 1.0e+006 * [0.51903247347540  -5.27933542201590   3.54728161283403]';

%u = 2.150698698977393e+007
%u = 2.150844308431280e+007;
u=0.07172828348*c;

% Based on absolute time diff (u), compute teh actual ranges. 

PR=c*sat_PR_times;
ranges=u+PR;

gps_radius=20200000+6380000;

% Verify the solution and calc the r.m.s error.

for i=1:3
    [sat_pos(1,i), sat_pos(2,i), sat_pos(3,i)]=sph2cart(sat_longitudes(i),sat_latitudes(i),  gps_radius+308);
end

% sat_pos now contains the positions of the 3 satellites in x, y, z form
% (vertical) vectors.

error1=ranges(1)-norm(sat_pos(:,1)-location)
error2=ranges(2)-norm(sat_pos(:,2)-location)
error3=ranges(3)-norm(sat_pos(:,3)-location)

rms_error=sqrt(error1^2+error2^2+error3^2)

