Need help with matlab question
1 view (last 30 days)
Show older comments
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?
6 Comments
Guillaume
on 8 Dec 2017
i have absolutely no knowledge on matlab
The problem with your question is nothing to do with matlab and everything to do with mathematics.
Mathematics tell you that
[3 3 * [X1 = [1
3 3] X2] 1]
has an infinity of solutions (all points on the line y=(1-3*x)/3) and that
[3 3 * [X1 = [1
3 3] X2] 0]
has no solution.
This is really basic linear algebra.
Accepted Answer
John D'Errico
on 8 Dec 2017
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.
2 Comments
John D'Errico
on 9 Dec 2017
Edited: John D'Errico
on 9 Dec 2017
You were probably asked to solve it as a way to introduce you to the problems when A is singular.
I showed you how to solve it, using pinv. It gives a single answer. It is just that the answer is not unique.
If B can be written as a linear combination of the columns of A, then the solution is not unique if A is singular.
If B cannot be written as a linear combination of the columns of A, and A is singular, then there is NO solution, nor can there ever be any exact solution for that problem.
The test that I showed, using rank, is the simple way to test which of those cases is true.
More Answers (1)
Birdman
on 8 Dec 2017
Look, now let's solve the question by hand and see what we will see. If you multiply A*x and equal to b, you will have two equations:
3*X1+3*X2=1;
3*X1+3*X2=1;
As you see, there are infinitely many solution to the problem, which results in no unique solution. This will happen when only there is a linear dependence between A's rows or columns, which in this case we see it. Therefore this problem has infinitely many solution. Do not search for a unique one. Also, take the determinant of A, which will be zero, which will prove that rows or columns are linearly dependent of A.
6 Comments
John D'Errico
on 8 Dec 2017
Edited: John D'Errico
on 8 Dec 2017
Read my answer, where I explain in some detail why that can never happen for singular A, and when a solution will exist, and why there will be infinitely many solutions.
See Also
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!