How to find which equation that have "Warning: Solution does not exist because the system is inconsistent"

I have big matrix and want to solve it with "\".
Ax=B so I use
[A,B]=equationsToMatrix(k,q)
x=A\B
but the matrix cannot be solved, the solution of x = Inf
How to know which one equation that makes x dont have solutions?

Answers (1)

If you get inf on output, there are few possibilities:
  • that there are infinite entries in the matrix (but these would typically lead to NaN)
  • that the matrix has a quite high condition number, leading to overflows in the calculation (NaN might well show up as well). This can include the case where the matrix is singular algebraically but the determinant is not exact zero due to floating point round-off
Example:
format long g
M = imag(hilbert(magic(5))) / 1e5
M = 5×5
1.0e+00 * -8.25829152282704e-05 7.13012616304923e-05 0.000102078097002245 -7.53174899482168e-05 -1.54789534562498e-05 7.85666869105459e-05 0.000102078097002245 -8.25829152282704e-05 -4.62557888280024e-05 -5.18060798565179e-05 7.13012616304923e-05 -5.18060798565179e-05 -3.89903635479488e-05 -5.18060798565179e-05 7.13012616304923e-05 -5.18060798565179e-05 -4.62557888280024e-05 -8.25829152282704e-05 0.000102078097002245 7.85666869105459e-05 -1.54789534562498e-05 -7.53174899482168e-05 0.000102078097002245 7.13012616304922e-05 -8.25829152282704e-05
rank(M)
ans =
4
cond(M)
ans =
3.42148903474857e+16
M\(1:5).'
Warning: Matrix is singular to working precision.
ans = 5×1
NaN NaN Inf Inf Inf
If you do not have actual infinite entries then there isn't "one equation" that makes it "not have a solution". Consider for example
[1 2 3
2 4 6
3 5 9]
ans = 3×3
1 2 3 2 4 6 3 5 9
Which "one equation" makes this not have an answer? You might at first say that it is the [2 4 6] because that is a constant multiple times an earlier row, but you could just as well say that the problem is the [1 2 3] for being a constant multiple of [2 4 6] .
When you use EquationsToMatrix() then you are working with systems of symbolic equations that are probably not inherently ordered. In such cases it could not be considered to be the "fault" of the second matrix that the system is not consistent; if you had happened to enter the equations in a different order then it would have been a different row "at fault".
It is fair to ask which rows are linearly dependent on earlier rows, understanding that does not mean that such rows are "wrong" (could be the other rows fault). I am not sure what a robust solution for that is, but it looks to me as if you can proceed to
[P,r] = rref(M.');
dependent_rows = setdiff(1:size(M,1), r)
dependent_rows =
5
This particular example happens to indicate that the last row is the first dependent row, but if you do something like
N = 20;
A = rand(N);
RRR = randperm(N,3)
RRR = 1×3
6 3 18
A(RRR(1),:) = A(RRR(2),:) * 5 - A(RRR(3),:) * 19;
[P,r] = rref(A.');
dependent_rows = setdiff(1:N, r)
dependent_rows =
18
dependent_rows == max(RRR)
ans = logical
1
We were successful in identifying a dependent row. We constructed row 6 as dependent on row 3 and 18, but mathematically that is equivalent to saying that row 18 is dependent on rows 3 and 6 -- which further emphasizes my point earlier that you have to be careful about which equation you blame.

7 Comments

This is the code for the matrix.
format rational
A_0=[-4 0 1;1 -8 2;0 4 -11]
A_0 =
-4 0 1 1 -8 2 0 4 -11
B=[2 1 4;3 1 0;4 0 2]
B =
2 1 4 3 1 0 4 0 2
A=[-15 2 3;2 -11 0;1 0 -14]
A =
-15 2 3 2 -11 0 1 0 -14
C=[1 2 0;0 1 4;2 3 2] %check if sum of every row must equal to 0
C =
1 2 0 0 1 4 2 3 2
O=zeros(3);
Q=[A_0 C O;
B A C;
O B A]
Q =
-4 0 1 1 2 0 0 0 0 1 -8 2 0 1 4 0 0 0 0 4 -11 2 3 2 0 0 0 2 1 4 -15 2 3 1 2 0 3 1 0 2 -11 0 0 1 4 4 0 2 1 0 -14 2 3 2 0 0 0 2 1 4 -15 2 3 0 0 0 3 1 0 2 -11 0 0 0 0 4 0 2 1 0 -14
sum(Q,2)
ans =
0 0 0 0 0 0 -3 -5 -7
R=zeros(3);
X=0;
Z=0;
Err=0;
for i=1:18
fprintf('i=%g',i);
R=((-C)-((R)^2)*B)*A^-1
%num2str(R,'%7.3f'); %I'm not using it because it may affect the result (?)
Err=R-X
if isequal(R,X)
Y=R
break
end
if Err<=10^(-5)
K=R
break
end
X=R
%num2str(X,'%7.3f');
end
i=1
R =
210/2221 442/2221 45/2221 72/2221 215/2221 650/2221 414/2221 681/2221 406/2221
Err =
210/2221 442/2221 45/2221 72/2221 215/2221 650/2221 414/2221 681/2221 406/2221
X =
210/2221 442/2221 45/2221 72/2221 215/2221 650/2221 414/2221 681/2221 406/2221
i=2
R =
1112/8829 566/2689 378/9089 22/245 133/1087 291/871 564/2165 350/1039 523/2231
Err =
328/10447 8/697 89/4173 488/8505 279/10919 83/2003 87/1174 123/4067 345/6683
X =
1112/8829 566/2689 378/9089 22/245 133/1087 291/871 564/2165 350/1039 523/2231
i=3
R =
191/1350 59/271 135/2431 230/1937 401/2954 347/964 437/1469 565/1596 1300/4859
Err =
179/11524 59/8166 137/9825 133/4595 230/17173 88/3403 235/6356 136/7931 135/4076
X =
191/1350 59/271 135/2431 230/1937 401/2954 347/964 437/1469 565/1596 1300/4859
i=4
R =
448/2949 455/2046 297/4582 287/2082 653/4525 853/2263 421/1307 637/1745 277/957
Err =
67/6421 134/28677 109/11738 323/16904 36/4205 145/8542 392/15915 152/13777 91/4155
X =
448/2949 455/2046 297/4582 287/2082 653/4525 853/2263 421/1307 637/1745 277/957
i=5
R =
495/3106 244/1081 201/2810 659/4352 317/2108 574/1475 161/474 773/2073 192/629
Err =
29/3891 9/2701 2340/348659 178/13111 23/3789 56/4583 181/10313 145/18479 273/17278
X =
495/3106 244/1081 201/2810 659/4352 317/2108 574/1475 161/474 773/2073 192/629
i=6
R =
361/2188 448/1963 134/1749 424/2623 427/2756 994/2495 409/1159 1489/3931 282/889
Err =
115/20456 51/20359 56/11013 166/16239 92/20197 81/8762 57/4309 135/22903 118/9863
X =
361/2188 448/1963 134/1749 424/2623 427/2756 994/2495 409/1159 1489/3931 282/889
i=7
R =
280/1653 1019/4427 446/5533 447/2635 701/4423 402/991 579/1594 2303/6007 566/1733
Err =
47/10686 35/17891 93/23296 94/11761 17/4782 62/8547 83/8022 27/5867 76/8093
X =
280/1653 1019/4427 446/5533 447/2635 701/4423 402/991 579/1594 2303/6007 566/1733
i=8
R =
221/1278 200/863 156/1861 1255/7128 96/595 909/2209 1066/2869 845/2183 664/1987
Err =
211/59648 65/41368 103/32001 5/778 102/35731 73/12484 149/17907 83/22457 59/7793
X =
221/1278 200/863 156/1861 1255/7128 96/595 909/2209 1066/2869 845/2183 664/1987
i=9
R =
438/2491 608/2609 211/2440 1083/5972 467/2853 684/1643 627/1657 371/951 337/990
Err =
53/18235 30/23261 26/9813 23/4356 16/6829 129/26801 282/41249 39/12856 85/13639
X =
438/2491 608/2609 211/2440 1083/5972 467/2853 684/1643 627/1657 371/951 337/990
i=10
R =
597/3349 269/1149 433/4882 1007/5421 3887/23466 1211/2881 1107/2882 876/2231 1022/2957
Err =
92/37873 63/58493 13/5862 31/7025 10/5111 40/9929 60/10501 26/10263 113/21663
X =
597/3349 269/1149 433/4882 1007/5421 3887/23466 1211/2881 1107/2882 876/2231 1022/2957
i=11
R =
416/2307 573/2438 197/2175 397/2095 1055/6306 1459/3443 352/905 1259/3189 377/1077
Err =
109/52947 31/33984 20/10629 5/1337 40/24139 178/52075 126/26021 57/26566 125/28243
X =
416/2307 573/2438 197/2175 397/2095 1055/6306 1459/3443 352/905 1259/3189 377/1077
i=12
R =
433/2378 403/1709 72/781 280/1453 616/3651 227/532 1721/4378 589/1485 3226/9117
Err =
37/20964 43/55013 27/16721 52/16219 25/17607 83/28296 35/8431 38/20669 24/6319
X =
433/2378 403/1709 72/781 280/1453 616/3651 227/532 1721/4378 589/1485 3226/9117
i=13
R =
251/1367 35/148 489/5225 891/4558 371/2183 834/1943 2233/5629 583/1464 3306/9257
Err =
45/29452 27/39919 33/23588 45/16213 98/79761 29/11411 67/18643 7/4400 172/52269
X =
251/1367 35/148 489/5225 891/4558 371/2183 834/1943 2233/5629 583/1464 3306/9257
i=14
R =
317/1714 133/561 433/4567 774/3911 1309/7654 1262/2925 479/1198 412/1031 1498/4161
Err =
72/53983 139/235512 25/20457 19/7842 38/35443 11/4955 79/25182 59/42500 9/3131
X =
317/1714 133/561 433/4567 774/3911 1309/7654 1262/2925 479/1198 412/1031 1498/4161
i=15
R =
3001/16124 249/1048 282/2941 1194/5969 211/1227 397/916 839/2084 289/721 1891/5216
Err =
9/7675 15/28916 43/39997 218/102339 46/48815 45/23042 9/3263 51/41798 123/48641
X =
3001/16124 249/1048 282/2941 1194/5969 211/1227 397/916 839/2084 289/721 1891/5216
i=16
R =
239/1277 279/1172 297/3067 379/1877 761/4404 2311/5311 322/795 799/1988 727/1993
Err =
38/36629 65/141672 113/118747 43/22817 41/49193 35/20247 26/10655 11/10193 147/65675
X =
239/1277 279/1172 297/3067 379/1877 761/4404 2311/5311 322/795 799/1988 727/1993
i=17
R =
243/1292 1979/8299 232/2375 725/3561 383/2207 231/529 147/361 477/1184 479/1306
Err =
70/75853 69/169105 23/27159 29/17299 22/29681 32/20801 36/16585 19/19797 42/21085
X =
243/1292 1979/8299 232/2375 725/3561 383/2207 231/529 147/361 477/1184 479/1306
i=18
R =
773/4092 1181/4945 120/1219 298/1453 447/2566 449/1025 1092/2669 1126/2789 1507/4089
Err =
51/61828 29/79531 34/44899 52/34703 16/24155 64/46525 73/37625 17/19821 65/36493
X =
773/4092 1181/4945 120/1219 298/1453 447/2566 449/1025 1092/2669 1126/2789 1507/4089
After getting R, i can get q1,q2,q3. But in this example, always pop up warning that the system is inconsistent. I try to search if the matrix has dependent row but the command cannot be done, another warning.
q=sym('q',[1 3])
q = 
I=eye(3);
e=ones(3,1);
w=q*(A_0+R*B)
w = 
%w=vpa(w,5)
a=q*inv(I-R)*e
a = 
%a=vpa(a,5)
aa=a-1
aa = 
%aa=vpa(aa,5)
j=solve(aa)
j = 
%j=vpa(j,5)
k=subs(w,sym('q1'),j)
k = 
%k=vpa(k,5)
size(k)
ans =
1 3
[C,S]=equationsToMatrix(k,q)
C = 
S = 
%C=vpa(C,5)
%S=vpa(S,5)
rank(C)
ans =
2
rank(S)
ans =
1
cond(C)
ans = 
rref(C)
ans = 
ji=C\S
Warning: Solution does not exist because the system is inconsistent.
ji = 
sum=0;
for i=1:3
sum = sum +ji(i);
end
sum
sum = 
size(sum);
q1=1-sum
q1 = 
The first column of the matrix C is the 3x1 null vector. Thus your system C*q = S has no solution.
I have example that all element on the first column is zero
ll=sym('l',[1 3])
ll = 
dd=[0 -2 2;0 -1 1;0 3/2 -3/2]
dd = 3×3
0 -2.0000 2.0000 0 -1.0000 1.0000 0 1.5000 -1.5000
ak=ll*[1;1;1];
akk=ak-1
akk = 
ja=solve(akk)
ja = 
ee=ll*dd
ee = 
ek=subs(ee,sym('l1'),ja)
ek = 
[DD,SS]=equationsToMatrix(ek,ll)
DD = 
SS = 
size(DD)
ans = 1×2
3 3
size(SS)
ans = 1×2
3 1
rank(DD)
ans = 1
rank(SS)
ans = 1
jak=DD\SS
Warning: Solution is not unique because the system is rank-deficient.
jak = 
summe=0
summe = 0
for i=1:3
summe = summe +jak(i);
end
summe
summe = 
2
size(summe)
ans = 1×2
1 1
l1=1-summe
l1 = 
For this example, i still get l1 to l3. I dont get the inconsistent warning.
Yes,
x + y = 2
2*x + 2*y = 4
has a solution, but
x + y = 2
2*x + 2*y = 5
has none.
So, my first matrix C is wrong so i get the warning of inconsistent.
the second matrix DD can be solved.
How can i know if after the iteration, the matrix C is ruined or because of what the matrix C is ruined?
if rank(C) = rank([C,S]), you can solve the system.
if rank(C) = rank([C,S]) -1, the system is inconsistent.

Sign in to comment.

Categories

Find more on Mathematics in Help Center and File Exchange

Asked:

on 4 Dec 2022

Commented:

on 27 Dec 2022

Community Treasure Hunt

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

Start Hunting!