Now I don't quite understand. I have this system below A2x=b
and solving for x with linsolve or A2\b2 gives me NaN and det(A2) is zero -- A2 is a singular square matrix
A2 = [1 -1 0 0 0 0 0 0;
0 1 1 0 0 0 0 0 ;
0 0 -1 1 0 0 0 0 ;
0 0 0 1 1 0 0 0 ;
0 0 0 0 1 -1 0 0;
0 0 0 0 0 1 1 0 ;
0 0 0 0 0 0 -1 1 ;
1 0 0 0 0 0 0 1 ;];
b2 = [0; 150; 20; 410; 180; 210; 80; 230];
x = pinv(A2)*b2
det(A2)
x2 = A2\b2
A2 * x
AB2 = [A2 b2];
rref(AB2)
But it has infinite solutions,right? since the rref(AB2) below gives me the reduced row echelon of - one free variable but there is a solution, infinite ones:
0 1 0 0 0 0 0 1 230
0 0 1 0 0 0 0 -1 -80
0 0 0 1 0 0 0 -1 -60
0 0 0 0 1 0 0 1 470
0 0 0 0 0 1 0 1 290
0 0 0 0 0 0 1 -1 -80
0 0 0 0 0 0 0 0 0
Why doesn't then linsolve and \ give me a solution vector? pinv() does, but that is not a solution but a pseudoinverse so the Ax =b doesn't give me exact b, but as good as it gets I guess. Am I understanding my reduced row echelon matrix wrong - is there no solution to this system??
You may understand better the behaviour of each built-in function checking its documentation. You almost have the answer to this apparently strange behaviour you observed.
First, you noticed the
det(A)is 0, so the matrix is singular. Actually, 'det(A)' is not the best way to check for matrix singularity. Thercondfunction mentions that:This indicates
A2is singular and inversion is not possible. The mldivide documentation indicates the behaviour of the\operator for singular matrices:Now, you also tried to compute the solution using the pinv function. Notice that the documentation on the
\operator also states that:So, even though the system has infinitely many solutions, the
pinvfunction returns the minimum norm least-squares solution (check it, for any other solution you may find, the norm of the vector is greater than the solution provided by the pinv function).The pinv function states that:
The reduced row echelon matrix is not wrong. Your system has infinitely many solutions.