欢迎来到天天文库
浏览记录
ID:33387541
大小:199.00 KB
页数:6页
时间:2019-02-25
《最小二乘法圆拟合.doc》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、最小二乘法拟合圆公式推导及vc实现[r] 最小二乘法(leastsquaresanalysis)是一种数学优化技术,它通过最小化误差的平方和找到一组数据的最佳函数匹配。最小二乘法是用最简的方法求得一些绝对不可知的真值,而令误差平方之和为最小。最小二乘法通常用于曲线拟合(leastsquaresfitting)。这里有拟合圆曲线的公式推导过程和vc实现。此处使用平方差与最小二乘法差的平方不一样,但是仍然具有实用估计价值,并且可以化简公式。VC实现的代码:C++类void CViewActionIma
2、geTool::LeastSquaresFitting(){ if (m_nNum<3) { return; } int i=0; double X1=0; double Y1=0; double X2=0; double Y2=0; double X3=0; double Y3=0; double X1Y1=0; double X1Y2=0; double X2Y1=0; for (i=0;i3、+) { X1 = X1 + m_points[i].x;//使用对象数组 Y1 = Y1 + m_points[i].y; X2 = X2 + m_points[i].x*m_points[i].x; Y2 = Y2 + m_points[i].y*m_points[i].y; X3 = X3 + m_points[i].x*m_points[i].x*m_points[i].x; Y3 = Y3 + m_points[4、i].y*m_points[i].y*m_points[i].y; X1Y1 = X1Y1 + m_points[i].x*m_points[i].y; X1Y2 = X1Y2 + m_points[i].x*m_points[i].y*m_points[i].y; X2Y1 = X2Y1 + m_points[i].x*m_points[i].x*m_points[i].y; } double C,D,E,G,H,N; double a,b,c; 5、 N = m_nNum; 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; double A,B,R; 6、 A = a/(-2); B = b/(-2); R = sqrt(a*a+b*b-4*c)/2; m_fCenterX = A; m_fCenterY = B; m_fRadius = R; return;}
3、+) { X1 = X1 + m_points[i].x;//使用对象数组 Y1 = Y1 + m_points[i].y; X2 = X2 + m_points[i].x*m_points[i].x; Y2 = Y2 + m_points[i].y*m_points[i].y; X3 = X3 + m_points[i].x*m_points[i].x*m_points[i].x; Y3 = Y3 + m_points[
4、i].y*m_points[i].y*m_points[i].y; X1Y1 = X1Y1 + m_points[i].x*m_points[i].y; X1Y2 = X1Y2 + m_points[i].x*m_points[i].y*m_points[i].y; X2Y1 = X2Y1 + m_points[i].x*m_points[i].x*m_points[i].y; } double C,D,E,G,H,N; double a,b,c;
5、 N = m_nNum; 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; double A,B,R;
6、 A = a/(-2); B = b/(-2); R = sqrt(a*a+b*b-4*c)/2; m_fCenterX = A; m_fCenterY = B; m_fRadius = R; return;}
此文档下载收益归作者所有