Azzera filtri
Azzera filtri

Why I am getting "Matrix is singular to working precision"?

7 visualizzazioni (ultimi 30 giorni)
While solving the below equation, I am getting "Matrix is singular to working precision". What does it mean?
2w+3x+y-11z=1
5w-2x+5y-4z=5
w-x+3y-3z=3
3w+4x-7y+2z=-7

Risposte (4)

Walter Roberson
Walter Roberson il 28 Set 2023
The equations have an infinite number of solutions. They are consistent but the 4th equation can be deduced from the previous 3.
When you have a matrix equation of the form A*x = b for matrix A and vector b and vectors of unknowns x, then the classic solution is to premultiply by inv(A) getting inv(A) * A * x = inv(A)*b and the inv(A) * A becomes the identity matrix by definition of matrix inverse, leading to x = inv(A) * b
But this fails if A is not an invertible matrix. When dealing with square matrices that are not invertible then we call those "singular"
So when you converted your system of equations to matrix form the coefficient matrix you got was singular, meaning that it is not invertible.
This is not a bug in MATLAB, this is a problem that your last equation does not give any new information so it is not possible to find a unique solution

Bruno Luong
Bruno Luong il 28 Set 2023
Modificato: Bruno Luong il 28 Set 2023
The warning message is there "Matrix is singular to working precision" because your equations are dependent
% 2*w+3*x+y-11*z = 1 %(eqt1)
% 5*w-2*x+5*y-4*z = 5 %(eqt2)
% w-x+3*y-3*z = 3 %(eqt3)
% 3*w+4*x-7*y+2*z =-7 %(eqt4)
22*eqt1 + 45*eqt2 = 164*eqt3 + 35*eqt4.
In such case "\" cannot solve it (the result is garbage). You need to do something more intelligent.
You'll notice the coefficients of y is exactly the rhs, so ONE solution is
w=x=z=0, and y = 1
The general solution is (not telling you how I find it, but this is the fact)
z = randi([-10 10]) % arbitrary value, here I give integer in (-10,10)
z = -9
w=0,
w = 0
x=3*z,
x = -27
y=1+2*z,
y = -17
% Check
2*w+3*x+y-11*z
ans = 1
5*w-2*x+5*y-4*z
ans = 5
w-x+3*y-3*z
ans = 3
3*w+4*x-7*y+2*z
ans = -7
To check validity of using "\" you could enter the matrix of the linear system and check the rank
A=[ 2 3 1 -11;
5 -2 5 -4;
1 -1 3 -3;
3 4 -7 2]
A = 4×4
2 3 1 -11 5 -2 5 -4 1 -1 3 -3 3 4 -7 2
rank(A)
ans = 3
The rank < 4 indicates you have singular system (having dependent equations)

Sam Chak
Sam Chak il 28 Set 2023
I attempted to replicate the results that displayed the warning message 'Matrix is singular to working precision.' From the message 'Matrix...' , I guess that you attempted to solve the system of linear equations in the matrix equation form to obtain by explicitly computing the matrix inverse. Therefore, we can conclude that you did not use the Gaussian elimination method.
The warning message implies that the system of linear equations is rank-deficient, resulting in an underdetermined system where there are more unknown variables than independent equations. This means that there are infinitely many solutions to the system. I have included a logical test for you to determine if the system has full rank.
Now that we have addressed the reason for receiving the warning message, would you like to continue pursuing the general solution as shown by@Bruno Luong?
syms w x y z c1
% system of linear equations
eqn1 = 2*w + 3*x + y -11*z == 1;
eqn2 = 5*w - 2*x + 5*y - 4*z == 5;
eqn3 = w - x + 3*y - 3*z == 3;
eqn4 = 3*w + 4*x - 7*y + 2*z ==-7;
eqns = [eqn1, eqn2, eqn3, eqn4];
v = [w, x, y, z];
% convert to matrix equation form, A*v = b
[A, b] = equationsToMatrix(eqns, v)
A = 
b = 
% Test 1
solv1 = inv(double(A))*double(b)
Warning: Matrix is singular to working precision.
solv1 = 4×1
NaN NaN NaN NaN
% Test 2
solv2 = double(A)\double(b)
Warning: Matrix is singular to working precision.
solv2 = 4×1
NaN NaN Inf Inf
% Determine if the system has full rank
% 1 means full rank (true), 0 means rank-deficient (false)
tf = logical(rank(A) == length(A))
tf = logical
0
  1 Commento
Sam Chak
Sam Chak il 28 Set 2023
@Shakil, It appears that Octave's backslash operator '\' (left matrix division) can compute the general solution when it detects that the system is rank-deficient.

Accedi per commentare.


Torsten
Torsten il 28 Set 2023
Modificato: Torsten il 28 Set 2023
syms w x y z c1 lambda
% system of linear equations
eqn1 = 2*w + 3*x + y -11*z == 1;
eqn2 = 5*w - 2*x + 5*y - 4*z == 5;
eqn3 = w - x + 3*y - 3*z == 3;
eqn4 = 3*w + 4*x - 7*y + 2*z ==-7;
eqns = [eqn1, eqn2, eqn3, eqn4];
v = [w, x, y, z];
% a special solution of the inhomogenous system
v_inhom = solve(eqns,v)
v_inhom = struct with fields:
w: 0 x: 0 y: 1 z: 0
v_inhom = [v_inhom.w;v_inhom.x;v_inhom.y;v_inhom.z];
% convert to matrix equation form, A*v = b
[A, b] = equationsToMatrix(eqns, v)
A = 
b = 
% solution of homogeneous system
v_hom = null(A)
v_hom = 
% general solution of linear system
v_sol = v_inhom + lambda*v_hom
v_sol = 
% test general solution
subs(lhs(eqns)-rhs(eqns),[w x y z],[v_sol(1) v_sol(2) v_sol(3) v_sol(4)])
ans = 

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by