MATLAB: Need help with matlab question

MATLAB

i need to solve a linear system of equation Ax = b; involving any singular matrix, A i have tried doing:
>> A=[3 3;3 3]
A =
3 3
3 3
>> B=[1;1]
B =
1
1
>> X=linsolve(A,B)
but i jsut get the response "Warning: Matrix is singular to working precision."
where am i going wrong?

Best Answer

Where you are going wrong is in your understanding of the linear algebra.
You are trying to solve the problem A*x=B, when A is singular. Suppose this is a scalar problem? The equivalent would then be to solve the problem
A*x = B
but where A is ZERO.
You would normally try to solve it (for scalar A and B) as
x = B/A
That is fine if A is non-zero. But in this case, we are saying that A is zero. So there is no solution. x is undefined.
The same thing happens when you have a matrix A that is singular. You cannot use linsolve, backslash, or inv. They all get upset, because of the effective divide by zero. That singular matrix is there.
So what can you do? Does the problem A*x=B have a solution for singular A? For example
A = ones(2);
x = [1;3];
b = A*x
b =
4
4
So in this case, if B is the vector [4;4], we might hope to find a solution. Can we recover x as [1;3] from that problem?
NO!
Sorry, but you cannot recover x uniquely as [1;3] from that problem, even if we could solve it. First, how can we solve it at all? Is there anything you can do?
One thing you can do is to use pinv, as such:
xhat = pinv(A)*B
xhat =
2
2
If we check, we see that this is indeed a solution.
A*xhat
ans =
4
4
It does indeed solve the problem we posed. It just did not recover the original vector x that I used, when I created B. In fact, there are either infinitely many solutions to this problem, or there will be NO solution at all.
As long as B can be represented as a linear combination of the columns of A then a solution exists, and so there will be infinitely many solutions. You can test for that using
rank(A) == rank([A,B])
ans =
logical
1
So if the above test is true then infinitely many solutions for a singular matrix A will exist. If it is false, then no true solution exists.
In our test case, I used pinv. But pinv returns only one of infinitely many solutions. Since A is a 2x2 matrix, with rank 1, we can write the entire set of solutions as
syms t
X = pinv(A)*B + t*null(A);
Thus, for some unknown parameter t, the entire family of solutions is represented as:
vpa(X,16)
ans =
2.0 - 0.7071067811865475*t
0.7071067811865475*t + 2.0
And when t=sqrt(2), we have...
vpa(subs(X,t,sqrt(2)),16)
ans =
1.0
3.0
Which is where we started.
But change B slightly...
B = [3;5]
B =
3
5
rank(A) == rank([A,B])
ans =
logical
0
So if we make B the vector [3;5], then no solution will ever exist. No solution to this problem, with A=ones(2) can possibly exist.