左矩阵除以向量

Left matrix divide with vectors

Matlab 如何明确求解 c1 中最右边的方程,特别是 ((x-1)\y)

我很清楚当你使用矩阵时会发生什么,例如A\b(其中 A 是矩阵,b 是列向量),但是当您在两个具有相等行数的向量上使用反斜杠时会发生什么情况?

问题:

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

您正在做 [0;1;2]\[1;1;1]。本质上 x=0.6

的最小二乘解
[0;1;2]*x=[1;1;1]

您从 documentation 获得的案例如下:

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.

(具体来说,你有 m=3, n=1)。

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

x = A\B; % = 0.6

在代数上,很容易看出这是最小二乘最小化的解

% 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

我毫不怀疑 MATLAB 使用更复杂的算法得出相同的结论,但这以相当简单的方式列出了数学。


通过对 x 的各种值进行以下测试,您可以通过实验看到没有更好的解决方案...0.6 给出了最小的错误:

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