Clear Filters
Clear Filters

solving linear systems of equations

43 views (last 30 days)
Chloe St John
Chloe St John on 26 Jan 2019
Answered: John D'Errico on 8 May 2024
i want to solve the following linear systems:
a)
2y+3x-7z=6
3z+5x-y=10
-4x+y+z=4
b)
2y+3x-7z=6
3z+5x-y=10
-4y-6x+14z=-12
i wrote the following code:
clc
clear
A=[3 2 -7; 5 -1 3; -4 1 1]
B=[6;10;4]
%Ax=B, therefore x=B/A
x=B./A
x=linsolve(A,B)
C=[3 2 -7;5 -1 3;-6 -4 14]
D=[6;10;-12]
%if Cx=D, therefore x=D/C
x=D./C
x=linsolve(C,D)
and it works for the first but not the second, this comes up in the command window anyone know why?
Warning: Matrix is singular to working precision.
> In q8_2 (line 15)
x =
NaN
NaN
NaN
  1 Comment
Jan
Jan on 28 Jan 2019
Edited: Jan on 28 Jan 2019
@Chloe St John: You have removed 3 of your questions alraedy. This behavior is disliked in the forum, because you do not want to participate in the community but only to consume the help for free. In case that you try to remove your question again, here is a copy:
i want to solve the following linear systems:
a) 2y+3x-7z=6
3z+5x-y=10
-4x+y+z=4
b) 2y+3x-7z=6
3z+5x-y=10
-4y-6x+14z=-12
i wrote the following code:
clc
clear
A=[3 2 -7; 5 -1 3; -4 1 1]
B=[6;10;4]
%Ax=B, therefore x=B/A
x=B./A
x=linsolve(A,B)
C=[3 2 -7;5 -1 3;-6 -4 14]
D=[6;10;-12]
%if Cx=D, therefore x=D/C
x=D./C
x=linsolve(C,D)
and it works for the first but not the second, this comes up in the command window anyone know why?
Warning: Matrix is singular to working precision.
> In q8_2 (line 15)
x =
NaN
NaN
NaN

Sign in to comment.

Answers (2)

Torsten
Torsten on 28 Jan 2019
Edited: Torsten on 28 Jan 2019
The matrix C is singular.
Check whether the linear system C*x=D is solvable at all (Remember the rank condition rank (C ) = rank (C,D)).
If it is solvable, you can use x=pinv(C )*D to get a solution.
Note that x=B./A and x=D./C is wrong in your notation ; it must read x=A\B and x=C\D.
Best wishes
Torsten.

John D'Errico
John D'Errico on 8 May 2024
A long forgotten question and one with an answer that may not be appreciated by @Chloe St John, given that multiple questions by that poster were deleted after the fact. But I'll post an answer anyway, since I think the question is a valid one, and I think it never got a complete answer.
In the second case,
C=[3 2 -7;5 -1 3;-6 -4 14];
D=[6;10;-12];
You want to solve for a 3x1 vector X, such that
C*X == D
The common solution, when C is non-singular, is to use a tool like linsolve, or just backslash. But if we try backslash, for example
X = C\D
Warning: Matrix is singular to working precision.
X = 3x1
NaN NaN NaN
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
we get NaNs. This is because C is a singular matrix.
rank(C)
ans = 2
It has rank 2, but the matrix is 3x3. That tells us there are only 2 distinct pieces of information in that matrix, not 3. And that means, we cannot estimate (uniquely) 3 unknowns. Other tools are able to perform the task however. For example, lsqminnorm.
X0 = lsqminnorm(C,D)
X0 = 3x1
1.9991 -0.0418 -0.0123
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Is that a valid solution, solving the system?
C*X0 - D
ans = 3x1
0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
In fact, it is so. There is indeed a valid solution to the problem. However, there are infinitely many solutions. We can write them all in the general form
syms t
X_general = sym(X0) + t*null(sym(C))
X_general = 
Now we can verify the general result.
C*X_general - D
ans = 
So ANY vector of that form, for any value of the parameter t is a valid solution. And unfortunately, tools lilke backslash and linsolve don't have the capability to give you that result.
Finally, this problem did have a solution. We could have performed this test in advance, to know if an exact solution does exist. While the rank of C was 2, if we adjoiin the vector D to C, and then compute the rank again, if the rank is still 2, then an exact solution does exist, since D can then be written as a linear combination of the columns of C.
rank([C,D])
ans = 2
And since rank again returns 2, we see an exact solution must exist.

Categories

Find more on Graphics Performance 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!