NaN Error Help?
Show older comments
Hello,
I am trying to run a simple program that iterates an equation to calculate system error. However, I keep getting errors due to certain parts returning NaN. I've tried to simplify my program to prevent this, but have had no luck. Any tips are appreciated. Thanks!
c = 3*10^8;
%define user coordinates
xu = 100;
yu = 883;
zu = 134;
%define arbitrary starting location
xn = 1;
yn = 1;
zn = 1;
tn = 1;
%define satellite offset
offset = 534;
%define coordinates of satellite
x1 = 11000*1852;
y1 = 0;
z1 = 0;
x2 = 0;
y2 = 11000*1852;
z2 = 0;
x3 = 0;
y3 = 0;
z3 = 11000*1852;
x4 = 11000*1852;
y4 = 0;
z4 = 0;
%calculate output location matrix
m= ones(4,4);
i = 1;
count = 0;
while i > 0
%calculate pseudorange measured by user to satellite 1
R1 = sqrt((xu-x1)^2 + (yu-y1)^2 + (zu-z1)^2 + offset);
%calculate nominal pseudorange to satellite
Rn1 = sqrt((xn-x1)^2 + (yn-y1)^2 + (zn-z1)^2 + c*tn);
deltaR1 = R1 - Rn1;
alphaX1 = (xn-x1)/(Rn1 - c*tn);
alphaY1 = (yn-y1)/(Rn1 - c*tn);
alphaZ1 = (zn-z1)/(Rn1 - c*tn);
new_row1 = [alphaX1, alphaY1, alphaZ1, c];
m(1,:) = new_row1;
%calculate pseudorange measured by user to satellite 2
R2 = sqrt((xu-x2)^2 + (yu-y2)^2 + (zu-z2)^2 + offset);
%calculate nominal pseudorange to satellite
Rn2 = sqrt((xn-x2)^2 + (yn-y2)^2 + (zn-z2)^2 + c*tn);
deltaR2 = R2 - Rn2;
alphaX2 = (xn-x2)/(Rn2 - c*tn);
alphaY2 = (yn-y2)/(Rn2 - c*tn);
alphaZ2 = (zn-z2)/(Rn2 - c*tn);
new_row2 = [alphaX2, alphaY2, alphaZ2, c];
m(2,:) = new_row2;
%calculate pseudorange measured by user to satellite 3
R3 = sqrt((xu-x3)^2 + (yu-y3)^2 + (zu-z3)^2 + offset);
%calculate nominal pseudorange to satellite
Rn3 = sqrt((xn-x3)^2 + (yn-y3)^2 + (zn-z3)^2 + c*tn);
deltaR3 = R3 - Rn3;
alphaX3 = (xn-x3)/(Rn3 - c*tn);
alphaY3 = (yn-y3)/(Rn3 - c*tn);
alphaZ3 = (zn-z3)/(Rn3 - c*tn);
new_row3 = [alphaX3, alphaY3, alphaZ3, c];
m(3,:) = new_row3;
%calculate pseudorange measured by user to satellite 4
R4 = sqrt((xu-x4)^2 + (yu-y4)^2 + (zu-z4)^2 + offset);
%calculate nominal pseudorange to satellite
Rn4 = sqrt((xn-x4)^2 + (yn-y4)^2 + (zn-z4)^2 + c*tn);
deltaR4 = R4 - Rn4;
alphaX4 = (xn-x4)/(Rn4 - c*tn);
alphaY4 = (yn-y4)/(Rn4 - c*tn);
alphaZ4 = (zn-z4)/(Rn4 - c*tn);
new_row4 = [alphaX4, alphaY4, alphaZ4, c];
m(4,:) = new_row4;
%calculate result
A = inv(m);
final = A*[deltaR1; deltaR2; deltaR3; deltaR4];
deltaX = final(1,1);
deltaY = final(2,1);
deltaZ = final(3,1);
deltaT = final(4,1);
xn = xn + deltaX;
yn = yn + deltaY;
zn = zn + deltaZ;
tn = tn + deltaT;
erms = sqrt((1/3)*((xn-xu)^2 + (yn-yu)^2 + (zn-zu)^2));
count = count + 1;
if erms < 1e-6
i = -1;
end
end
disp(count)
disp(tn)
1 Comment
David Goodmanson
on 24 Mar 2017
Hi bau, Since satellite positions 1 and 4 are identical, that means rows 1 and 4 of matrix m are identical, so m has no inverse. Even if that were not the case, row 4 of m consists of four elements of size 3e8, while the other entries of m are all less than 1e-1. The matrix is badly scaled and there may be problems with the inverse even if all four rows were different.
Answers (1)
Jan
on 24 Mar 2017
The problem is a warning, not an error:
Warning: Matrix is singular, close to singular or badly scaled.
Results may be inaccurate. RCOND = NaN.
> A = inv(m);
This means, that the created matrix m is singular and the inversion fails. A simplification of the code will not help, because it is a mathematical problem. The comments in the code are not clear enough to understand, what you want to calculate, so based on the failing code only I cannot guess, if there is a bug in the implementation or in the algorithm.
Categories
Find more on Reference Applications in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!