Linear Algebra Error: Matrix is close to singular or badly scaled.

1 view (last 30 days)
Hi,
I have a simple localization code that calculates the x,y,z coordinates for a wave heard at 4 receivers. The equations are set up using matrices but it is giving me an error I don't understand. I checked if the matrix reaches singularity and it does not. So I am not sure what the error is pointing to. I am attaching the code here:
% Sensor/receiver locations/ vertices of a tetrahedron
% si(xi,yi,zi)
r1 = [0.125 0.125 0.125];
r2 = [0.25 0.125 0.125];
r3 = [0.1875 0.25 0.125];
r4 = [0.1875 0.1875 0.25];
% Calculate the Time of Arrival at the receivers
c = 343; % m/s
t = zeros(1, 4);
sound_loc = [10 20 10]; % Replace x, y, and z with your sound location coordinates
vertices = [r1; r2; r3; r4]; % Replace vertex1 to vertex4 with your 3D vertices
for j = 1:4
t(j) = pdist([sound_loc; vertices(j, :)], 'euclidean') / c;
end
% Setting up matrices to solve for location of source
A = [2*r1(1) 2*r1(2) 2*r1(3) 2*c*t(1) -1; 2*r2(1) 2*r2(2) 2*r2(3) 2*c*t(2) -1; 2*r3(1) 2*r3(2) 2*r3(3) 2*c*t(3) -1; 2*r4(1) 2*r4(2) 2*r4(3) 2*c*t(4) -1 ];
%u = [x y z -c*t r^2-c^2*t^2];
r1_sq = sqrt(r1(1)^2 + r1(2)^2 + r1(3)^2);
r2_sq = sqrt(r2(1)^2 + r2(2)^2 + r2(3)^2);
r3_sq = sqrt(r3(1)^2 + r3(2)^2 + r3(3)^2);
r4_sq = sqrt(r4(1)^2 + r4(2)^2 + r4(3)^2);
b = [r1_sq-c^2*t(1)^2; r2_sq-c^2*t(2)^2; r3_sq-c^2*t(3)^2; r4_sq-c^2*t(4)^2 ];
% Calculating u : u = [x y z -c*t r^2-c^2*t^2];
% u = (A^T*A)^-1*A^T*b
t = 0;
u1 = (transpose(A)*b);
u2 = (transpose(A)*A);
u = inv(u2) * u1;
  3 Comments
Bruno Luong
Bruno Luong on 2 Sep 2023
Edited: Bruno Luong on 2 Sep 2023
@Star Strider "u2 = A\A"
where it comes from??? what you want to show here?
Shrishti Yadav
Shrishti Yadav on 2 Sep 2023
Edited: Shrishti Yadav on 2 Sep 2023
The original equation was: Au = b with A as an mxn matrix and u is nx1 and b is nx1
so taking the transpose on both sides and then get only u on the left hand side, we get the following after multiplying both sides by the inverse of the transpose of A * A:
u = inv(A^TA)*A^T*b
So it should be ok in terms of singularity. The goal was to calculate u correctly. that was it. i didn't know if the way i am multiplying it out makes an error.

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 2 Sep 2023
I am not certain what you want to do.
Consider using the pinv function.

More Answers (1)

Bruno Luong
Bruno Luong on 2 Sep 2023
Edited: Bruno Luong on 2 Sep 2023
@Shrishti Yadav "I checked if the matrix reaches singularity and it does not. "
Your A matrix has size 4 x 5, the the rank is maximum 4.
The matrix u2 = A'*A is 5 x 5 with maximum rank <= 4 so it must be singular, despite what you claim.
In short you try to solve for 5 unknown with 4 equations. The system is then underdetermined and MATLAB warns you that.

Categories

Find more on Linear Algebra 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!