Why is there a difference in computing least squares although same symbolic operator \ is used?
7 views (last 30 days)
Show older comments
By reading the documentation and different answers to forum questions I am getting confused why some use cases of the \ operator (mldivide) compute least square solutions but others don't. For example, the documentation states that for a linear system Ax = b, x = A\b computes a least square solution if
- A is a rectangular m-by-n matrix with m ~= n, and B is a matrix with m rows, then A\B returns a least-squares solution to the system of equations A*x= B.
and so this is the case where x and B are vectors. But in the matrix case, A*X = B it is always explicitly stated that the symbolic operator \ does not compute least square solutions?
Some of my questions surrounding this:
- Why is there this difference?
- If I want a least square solution of AX = B, should I use X = lsqlin(A,B) instead?
- Can I vectorize AX = B to get vec(X) and new factor matrices C and D such that C*vec(X) = D (where D = vec(B)) and then perform vec(X) = D \ C to get a least square solution to my original matrix system?
3 Comments
Dyuman Joshi
on 3 Oct 2023
They are different functions that share the same name. The ** function is part of the Symbolic Math Toolbox.
You will find many functions that share the same name but have different functionality and are part of different Toolboxes. It's best to check the documentation to see if a function is built-in or not and how it operates.
Accepted Answer
Bruno Luong
on 3 Oct 2023
Edited: Bruno Luong
on 3 Oct 2023
"I'm guessing for my case only the documentation of page * matters? "
Yes
For numerical case and vector of RHS b
x = A\b
is always a least square solution, meaning x minimizes
norm(A*x - b,2)
In case rank(A) >= size(A,2) the solution is unique. lsqlin(A,b) is the same than A\b (they might differ only due to numerical error). So if you don't have constraint, no need to call lsqlin, rather use "\" (faster).
In case rank(A) < size(A,2) (== size(x,1)), i.e. underdetermine system there is more that 1 leastsquare solution, MATLAB selected the so-called "basis/basis solution" that has minimum of non-zeros elements (usually = rank(A)). lsqlin(A,b) might converge to different solution, but still a least-square one. Note that you can also use lsqminnorm that returns the least-square AND min(norm) solution meaning minimizing both
norm(A*x - b,2)
% and
norm(x,2)
In case rank(A) < size(A,2) AND rank(A) < size(A,1), the matrix is singular the above x is quite unstable and sometime it contains NaN and Inf.
For RHS with matrix B
X = A \ B
each xj := X(:,j) and bj := B(:,j) verifying
A * xj = bj
So just extrapolate what I told above for vector. Note that the pattern of non-zeros of xj depends on A and NOT on bj.
0 Comments
More Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!