mrgloom
mrgloom

Reputation: 21632

Understanding Matlab linsolve

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)?

Upvotes: 2

Views: 3574

Answers (1)

gire
gire

Reputation: 1105

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.

Upvotes: 2

Related Questions