dr_*_*_rk 8 algorithm matlab numerical curve-fitting least-squares
我有一组点(x,y),我需要找到使用MATLAB通过原点的最佳拟合线.
And*_*ein 14
总之:您的函数必须的形式y=ax+0,这使得polyfit无用.但您可以使用最小二乘法:
a = x(:)\y(:);
Run Code Online (Sandbox Code Playgroud)
说明:
你有n方程式和一个a需要找到的变量:
a*x1 = y1;
a*x2 = y2;
...
a*xn = yn;
Run Code Online (Sandbox Code Playgroud)
操作员\找到最小二乘解.
或者,您可以手动找到解决方案:
a = (x'*x) \ (x'*y);
Run Code Online (Sandbox Code Playgroud)
或者在伪代码中:
(x1*y1 + x2*y2 + ... xn*yn)
a = ----------------------------
(x1*x1 + x2*x2 + ... xn*xn)
Run Code Online (Sandbox Code Playgroud)
如果您不使用Matlab,这很有用 - 例如在C代码中.
示例和代码段:

function FindLSSolution()
a = 2.5;
x = rand(100,1)*10;
y = a*x + randn(100,1);
figure;scatter(x,y);
A = x(:)\y(:);
hold on;plot(x, A*x,'g');
end
Run Code Online (Sandbox Code Playgroud)