Why matrix division returns different answers?
5 views (last 30 days)
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 = mldivide(A,b)
2- QR decomposition
[Q,R] = qr(A);
x = R\Q.'*b % which used matrix left division!
x = lscov(A,b)
4- Least squares (extra)
x = lsqr(A,b)
lsqr converged at iteration 2 to a solution with relative residual 4.7e-16.
5- Optimization (extra)
function f = objFun(x, A, b)
f = norm(b - A*x);
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)
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
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.
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
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.