Wrong solution to a linear equation, or why is A * (A \ B) not equal to B?

How is it possible that the result of A*(A\D) below is not equal to D ?

enter image description here

It should give D - here is an excerpt from the Octave documentation:

Systems of linear equations are ubiquitous in numerical analysis. to solve the set of linear equations Ax = b, use the left division operator, '\: x = A \ b

Below is the code for those who want to try:

 A = [1,1,1;0,0,0;2,1,2;2,1,2;3,5,6] D = [1;2;3;4;5] % A is of rank 3: rank(A) % therefore the system Ax=D has a unique solution x = A\D % but Octave has not given the good solution: A*x 

Someone tells me that Matlab gives exactly the same result.

EDIT 10/10/2012:. After reading the answers, let me indicate where I made a serious mistake: the requirement "A has rank 3, therefore the Ax = D system has a unique solution" is absolutely wrong! By the way, the above part of the documentation is quite confusing.

+4
source share
2 answers

A has 5 lines, and therefore D Both of them have 3 columns. Therefore, you have an overridden system of 5 equations with 3 variables. In most cases, this means that you cannot solve the equations exactly because you have too many limitations.

As soon as you do

 x = A\D; 

you get the least squares solution.

  0.8333 -1.5000 1.6667 

What is the solution? This is a solution that minimizes the sum of squared errors. Let me calculate the error:

  r = A*xD; totalError = sum( r.^2); 

This means that you cannot find x so that sum(sqr(A*xD)) has a smaller error.

A short note: in your case, you also have a string of zeros - which leads to the fact that the actual number of equations becomes 4

Let's look again at A*(A\D) :

 >> A* (A\D) ans = 1.0000 0 3.5000 3.5000 5.0000 

It looks familiar! Very close to [1; 2; 3; 4; 5]. The first and last lines are the same. The second value is zero because you put a string of zeros. In the 3rd and 4th lines you had exactly the same lines in A, but a different value in B, which corresponds to

 2*x+ 1*y + 2*z = 3; 2*x+ 1*y + 2*z = 4; 

And you have their average! This makes sense because the average is a value that minimizes the sum of the distances to 3 and 4.


Here is a simpler example: suppose you want to solve the following system of equations:

  x = 1; x = 2; 

Obviously, x cannot be 1 and 2 at the same time. A solution that minimizes the sum of the squared errors is 1.5

  A = [1;1]; b = [1;2]; A\b ans = 1.5000 
+7
source

Your system A overridden (A is rectangular), so you have n't solved your system exactly :

Rectangular matrices If A is rectangular, mldivide returns a least-squares solution. MATLAB solves overridden systems with QR factorization (see Qr). For an underdetermined system, MATLAB returns a solution with a maximum number of null elements.

+2
source

Source: https://habr.com/ru/post/1438291/


All Articles