# in matlab, what differences are between linsolve and mldivide?

in matlab, both linsolve and mldivide are used for solving a system of linear equations, in all of determined, overdetermined and underdetermined cases.

Reading their documents, I was wondering what differences are between them? Are they using almost the same algorithms of matrix factorization and triangularization in the three cases?

If A has the properties in opts, linsolve is faster than mldivide, because linsolve does not perform any tests to verify that A has the specified properties

Does mldivide perform the same tests to verify if A has the special properties? Or does mldivide just treat them as general case without the special properties?

Thanks!

Both of them are direct solver to solve linear systems (opposing to iterative solver).

mldivide does perform the tests for $A$ in solving $Ax = b$. Please see Allan's answer in this thread for more information. Also see MATLAB's help on mldivide algorithm here.

mldivide for square matrices: If A is symmetric and has real, positive diagonal elements, MATLAB attempts a Cholesky factorization. If the Cholesky factorization fails, MATLAB performs a symmetric, indefinite factorization. If A is upper Hessenberg, MATLAB uses Gaussian elimination to reduce the system to a triangular matrix. If A is square but is neither permuted triangular, symmetric and positive definite, or Hessenberg, then MATLAB performs a general triangular factorization using LU factorization with partial pivoting

linsolve for square matrices: LU factorization with partial pivoting

mldivide and linsolve for rectangular matrices: QR factorization

In linsolve as the help doc suggests in mathworks website, you could avoid the extra testing process (Allan used the word "overhead" in his answer) by using opts if and only if you know what $A$ is like in advance. For large problems, you could save some time. For example:

opts.POSDEF = true; linsolve(A,b,opts)


would return $x$ if you know $A$ is positive definite in advance. However, an incorrectly chosen opts leads to a wrong result.

If certain criteria are met, linsolve and mldivide do utilize the same factorization process. For example, for a dense positive definite system satisfying certain properties, or you have an overdetermined system and both perform least square fitting.

Moreover, linsolve could also perform symbolic computation. This is handy when you have a small underdetermined system which has infinite number of solutions. linsolve enables you to solve it symbolically, mldivide can not do that. However, if the variables are not declared symbolically, mldivide and linsolve would give you the same warning message "Matrix is singular to working precision."

Last but not least, linsolve does not support sparse systems like the following matrix (blue dot means non-zero entry). While mldivide could handle sparse systems robustly when the size is under 200k by 200k.