function f = timeerror4( x, y, z, d, e1, e2, e3, e4 )
% newton: takes an initial guess vector and performs one step
% of newton's method on it

% constants from the problem
c = 299792.458;
p = 26570;

x1 = 0;
y1 = 0;
z1 = 6370;
d1 = 0.0001;

phi1 = pi / 4;
theta1 = pi / 4;

phi2 = pi / 8;
theta2 = pi / 3;

phi3 = pi / 3;
theta3 = 2*pi / 3;

phi4 = pi / 8;
theta4 = pi / 8;

A1 = p * cos(phi1) * cos(theta1);
B1 = p * cos(phi1) * sin(theta1);
C1 = p * sin(phi1);
t1 = (d + sqrt(A1^2 + B1^2 + (C1 - z1)^2) / c) + e1;

A2 = p * cos(phi2) * cos(theta2);
B2 = p * cos(phi2) * sin(theta2);
C2 = p * sin(phi2);
t2 = (d + sqrt(A2^2 + B2^2 + (C2 - z1)^2) / c) + e2;

A3 = p * cos(phi3) * cos(theta3);
B3 = p * cos(phi3) * sin(theta3);
C3 = p * sin(phi3);
t3 = (d + sqrt(A3^2 + B3^2 + (C3 - z1)^2) / c) + e3;

A4 = p * cos(phi4) * cos(theta4);
B4 = p * cos(phi4) * sin(theta4);
C4 = p * sin(phi4);
t4 = (d + sqrt(A4^2 + B4^2 + (C4 - z1)^2) / c) + e4;

% initial guess:
x0 = [x; y; z; d];

% the functions describing four spheres
f1 = (x - A1)^2 + (y - B1)^2 + (z - C1)^2 - (c * (t1 - d))^2;
f2 = (x - A2)^2 + (y - B2)^2 + (z - C2)^2 - (c * (t2 - d))^2;
f3 = (x - A3)^2 + (y - B3)^2 + (z - C3)^2 - (c * (t3 - d))^2;
f4 = (x - A4)^2 + (y - B4)^2 + (z - C4)^2 - (c * (t4 - d))^2;

% construct the Jacobian out of partial derivatives of the functions
a11 = 2 * (x - A1);
a12 = 2 * (y - B1);
a13 = 2 * (z - C1);
a14 = 2 * c^2 * (t1 - d);

a21 = 2 * (x - A2);
a22 = 2 * (y - B2);
a23 = 2 * (z - C2);
a24 = 2 * c^2 * (t2 - d);

a31 = 2 * (x - A3);
a32 = 2 * (y - B3);
a33 = 2 * (z - C3);
a34 = 2 * c^2 * (t3 - d);

a41 = 2 * (x - A4);
a42 = 2 * (y - B4);
a43 = 2 * (z - C4);
a44 = 2 * c^2 * (t4 - d);

% the Jacobian
DF = [a11 a12 a13 a14; a21 a22 a23 a24; a31 a32 a33 a34; a41 a42 a43 a44];

F = [f1; f2; f3; f4];

% DF * s = -F
s =  DF \ -F;
f = x0 + s;
end
Error using timeerror4 (line 29)
Not enough input arguments.