# Why matrix division returns different answers?

5 views (last 30 days)
Ahmad AlAttar on 7 Jul 2022
Commented: Bruno Luong on 28 Jul 2022
Hello fellow Matlabers,
So I have these two matrices:
A = [0.13 -0.022; 0.064 0.006; 0.132 -0.03; 0.012 -0.028];
b = [-0.0287 0.01355];
I want to solve for x where Ax = b. I tried different ways to compute x.
1- Left matrix division
x = A\b
x =
0
0
-0.1921
-0.2781
x = mldivide(A,b)
x =
0
0
-0.1921
-0.2781
2- QR decomposition
[Q,R] = qr(A);
x = R\Q.'*b % which used matrix left division!
x =
0
0
-0.1921
-0.2781
3- LSCOV
x = lscov(A,b)
x =
0
0
-0.1921
-0.2781
4- Least squares (extra)
x = lsqr(A,b)
lsqr converged at iteration 2 to a solution with relative residual 4.7e-16.
x =
-0.0898
0.0969
-0.1554
-0.2261
5- Optimization (extra)
function f = objFun(x, A, b)
f = norm(b - A*x);
end
x0 = [0.25 0.25 0.25 0.25]';
options = optimset('TolFun', 1e-15, 'TolX', 1e-15);
[x,fval,exitflag,output] = fmincon(@(x)objFun(x, A, b), x0, [],[],[],[],[],[],[],options)
x =
-0.1216
0.2686
-0.2192
-0.0960
Problem
I am trying to understand what happens behind the left division (step by step). When I simply do this, the answer is different:
x = pinv(A)*b
ans =
-0.0898
0.0969
-0.1554
-0.2261
Why isn't the answer the same as what I got for methods 1, 2, and 3? I need a step by step implementation of the matrix left division (without using the left division in the implementation again). I could not find any documentation about the implementation of this division. I really don't know how to arrive at the same answer manually. Any help would be much appriciated.
Thank you!
Stephen23 on 7 Jul 2022
Edited: Stephen23 on 7 Jul 2022

Bruno Luong on 7 Jul 2022
Edited: Bruno Luong on 7 Jul 2022
When you have a underdetermined linear system, meaning you "solve"
A*x = b
where A has non-zero kernel, or in other word
null(A)
returns non-empty set of vectors, you have many choice of x that gives the same map
b = A*x
The A\b use QR with permutation (the "permutation" matters in other example different thean yours ) to return the so called "basis solution", meaning rhe solution has most 0s, the non-zeros terms corresponds to independent columns of A, selected by QR decomposition with permutation.
The pinv however gives the least 2-norm solution, meaning the solution that minimizes norm(x,2) among all solutions that gives the same A*x. It's n afine space of dimension numvber of column of null(A)
If your system is overdetermined, meaning rank(A) < size(A,2), then the solution of
A*x = b
may not be reached, in this case the above equation is approximated by
x = argmin norm(A*x-b,2)
and this holds for A\b or pinv(A)*b.
NOTE that the linear sysyem of equations can be underdetermined and overdetermned at the same time.

Ahmad AlAttar on 28 Jul 2022
[Q,R,P] = qr(A); % QRP decomposition
rearrange_A = A*P; % rearrange matrix A
basis_A = rearrange_A(:,1:2); % extract left half of matrix
x = pinv(basis_A)*b;
Bruno Luong on 28 Jul 2022
How does this snip of code answers your question? It returns the result that is not even compatible with
A*x = b
with A that is fat like 2 x 4 in size.