matlab
2009-01-17 |||| 最小二乘法拟合圆公式推导及 matlab
2009-01-17
2009-01-17
matlab
2009-01-17
matlab 实现
最小二乘法(least squares analysis)是一种数学优化技术,它通过
最小化误差的平方和找到一组数据的最佳函数匹配。最小二乘法是用最
简的方法求得一些绝对不可知的真值,而令误差平方之和为最小。 最
小二乘法通常用于曲线拟合 (least squares fitting) 。
这里有拟合圆曲线 的公式推导过程和 vc 实现。
matlabmatlab
matlab
matlab 实现:
function [R,A,B]=circ(x,y,N)
x1 = 0;
x2 = 0;
x3 = 0;
y1 = 0;
y2 = 0;
y3 = 0;
x1y1 = 0;
x1y2 = 0;
x2y1 = 0;
for i = 1 : N
x1 = x1 + x(i);
x2 = x2 + x(i)*x(i);
x3 = x3 + x(i)*x(i)*x(i);
y1 = y1 + y(i);
y2 = y2 + y(i)*y(i);
y3 = y3 + y(i)*y(i)*y(i);
x1y1 = x1y1 + x(i)*y(i);
x1y2 = x1y2 + x(i)*y(i)*y(i);
x2y1 = x2y1 + x(i)*x(i)*y(i);
end
C = N * x2 - x1 * x1;
D = N * x1y1 - x1 * y1;
E = N * x3 + N * x1y2 - (x2 + y2) * x1;
G = N * y2 - y1 * y1;
H = N * x2y1 + N * y3 - (x2 + y2) * y1;
a = (H * D - E * G)/(C * G - D * D);
b = (H * C - E * D)/(D * D - G * C);
c = -(a * x1 + b * y1 + x2 + y2)/N;
A = a/(-2);
B = b/(-2);
R = sqrt(a * a + b * b - 4 * c)/2;
%x 坐标
%y 坐标