0

May I ask about the difference of solving for x in these 2 following ways in Matlab :

Way 1: x = A\b

Way 2: x = inv((A.').*A)*(A.'*b)

(p.s: the inverted matrix is invertible)

I think these two ways should give the same results but I can not achieve this matching. I want to apply the Least square fashion. However it includes many different types of matrix (transpose, invert and then multiple) and when writing on Matlab language, I've got confusion. Please would you help me point out the wrong things in way 2.

Thank you very much for your comments !

AlessioX
  • 3,167
  • 6
  • 24
  • 40
chappi
  • 63
  • 7
  • 3
    The docs on [`inv`](http://nl.mathworks.com/help/matlab/ref/inv.html) explicitly state: *`x = A\b` is computed differently than `x = inv(A)*b` and is recommended for solving systems of linear equations.* i.e. use `\` and not `inv` – Adriaan Apr 20 '16 at 18:04
  • 2
    Theoretically, both ways are the same in finding the solution but you are **highly encouraged to use the first way**. However, doing `inv((A.').*A)` is not correct. You have to matrix multiply, not element-wise multiply: `inv((A.')*A)`. In any case, using `inv` is known to be numerically unstable if the condition number for `A` is large, especially if you are solving linear systems. Either use the (1) first way or use (2) `pinv(A)*b` if you have an overdetermined or undetermined system of equations. – rayryeng Apr 20 '16 at 18:13

1 Answers1

1

The Least Square formula, which is

enter image description here

is spelled incorrectly. In the latter b<-->y whereas p<-->x.

Inside the inv() function the product between A.' and A is not an element-wise product.
Also, according to the PEMDAS rule, is incorrect to join A.' and b inside a bracket, giving such term a priority with respect to the former.

In conclusion, the Matlab formula for LS is:

x=inv((A.')*A)*(A.')*b;

Finally, you can as well simplify the above formula by means of the pinv() function, that evaluates the Moore-Penrose Pseudoinverse, that is inv((A.')*A)*(A.'). Indeed pinv(A) will lead to the same result as inv((A.')*A)*(A.') and therefore you can rewrite the LS solution as

x=pinv(A)*b;
AlessioX
  • 3,167
  • 6
  • 24
  • 40