EMIL NILSSON
EMIL NILSSON

Reputation: 33

Left matrix divide with vectors

How explicitly does Matlab solve the rightmost equation in c1, specifically ((x-1)\y)?

I am well aware what happens when you use a matrix, e.g. A\b (where A is a matrix and b is a column vector), but what happens when you use backslash on two vectors with equal rows?

The problem:

x = (1:3)';
y = ones(3,1);
c1 = ((x-1)\y) % why does this =0.6?

Upvotes: 3

Views: 183

Answers (1)

Wolfie
Wolfie

Reputation: 30101

You're doing [0;1;2]\[1;1;1]. Essentially x=0.6 is the least-squares solution to

[0;1;2]*x=[1;1;1]

The case you have from the documentation is the following:

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.

(Specifically, you have m=3, n=1).

A = (1:3).' - 1; % = [0;1;2]
B = ones(3,1);   % = [1;1;1]

x = A\B; % = 0.6

Algebraically, it's easy to see this is the solution to the least-squares minimisation

% Calculate least squares
leastSquares = sum( ((A*x) - B).^2 )
             = sum( ([0;1;2]*x - [1;1;1]).^2 )
             = sum( [-1; x-1; 2x-1].^2 )
             = 1 + (x-1)^2 + (2x-1)^2
             = 1 + x^2 - 2*x + 1 + 4*x^2 - 4*x + 1
             = 5*x^2 - 6*x + 3
% Minimum least squares -> derivative = 0
d(leastSquares)/dx = 10*x - 6 = 0
              10*x = 6
                 x = 0.6

I have no doubt that MATLAB uses a more sophisticated algorithm to come to the same conclusion, but this lays out the mathematics in a fairly plain way.


You can see experimentally that there is no better solution by testing the following for various values of x... 0.6 gives the smallest error:

sum( ([0;1;2]*x - [1;1;1]).^2 ) 

Upvotes: 3

Related Questions