2

1.What is the difference between A\b and linsolve(A,b) (different algorithms?) ?

2.What is the difference solving A*x=b and A'*A*x=A'*b, which is more precise ?

Second equation goes from Least squares approximation

Simple matlab test code:

A=[1,2,3;4,5,6;7,8,9]
b=[1;2;3]

x1= A\b
x1 =

   -0.3333
    0.6667
         0
x2=linsolve(A,b)
x2 =

   -0.3333
    0.6667
         0
x3=linsolve(A'*A,A'*b)
x3 =

    0.2487
   -0.4974
    0.5820
x4=(A'*A)\(A'*b)
x4 =

   -0.8182
    1.6364
   -0.4848

reading linsolve documentation I found that

[X,R] = linsolve(A,B) solves the matrix equation AX = B and returns the reciprocal of the condition number of A if A is a square matrix, and the rank of A otherwise.

so using R we can test precision(2nd question)?

Amro
  • 123,847
  • 25
  • 243
  • 454
mrgloom
  • 20,061
  • 36
  • 171
  • 301
  • A'*A*x=A'*b are the normal equations for a least squares approximation. You would never want to try solving this with A'*A*x=A'*b. Use x1 = A\b as this will use something nice and stable like QR factorisation or maybe even SVD. For badly conditioned matrices you can introduce significant error just in the calculation of A'*A so dont use this approach – mathematician1975 Sep 26 '14 at 09:27
  • @mathematician1975 What if I have A=(M,n) n< – mrgloom Sep 26 '14 at 09:37
  • 1
    There is no need to premultiply by the A' matrix. The equation A'*A*x=A'*b is just the explicit equation for the solution of a least-squares system. You are dealing with a square system here but the principal is the same - there is simply no need to premultiply by A', it is inefficient, and even at the very best all you can hope for is not to exacerbate any ill-contiditoning in the system. – mathematician1975 Sep 26 '14 at 09:50
  • 1
    you might be interested in reading these: [How to implement Matlab's mldivide](http://stackoverflow.com/a/18553768/97160), [How does the MATLAB backslash operator solve Ax=b for square matrices?](http://scicomp.stackexchange.com/questions/1001/how-does-the-matlab-backslash-operator-solve-ax-b-for-square-matrices) – Amro Sep 26 '14 at 14:59

1 Answers1

2

Regarding your first question: one can consider mldivde (x = A\B) as a wrapper of the linsolve function. The function linsolve allows the user to specify information about the matrix A which can help Matlab to select a more appropriate (faster) algorithm to solve the system. Nevertheless, by using linsolve it is easy to screw up. Quoting from Matlab's documentation:

If A does not have the properties that you specify in opts, linsolve returns incorrect results and does not return an error message. If you are not sure whether A has the specified properties, use mldivide instead.

If you can assess with 100% of certainty the type of your matrix A while executing your algorithm, then go for linsolve. Otherwise use mldivide.

gire
  • 1,105
  • 1
  • 6
  • 16