Search code examples
matlabalgebraleast-squares

solving lower triangular matrix using least square fashion matlab


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 !


Solution

  • 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;