plot(x, y, '*')
hold on
A = [x y ones(size(x))]
abc = -(A*A')\sum(A*[x'.^2 y'.^2], 2)
ox = -abc(1)/2
oy = -abc(2)/2
R = sqrt(ox^2+oy^2-abc(3))
plot(ox, oy, 'or')
t = linspace(0, 2*pi, 100)
plot(ox+R*cos(t), oy+R*sin(t))
点击按钮分别按照提示输入6个点的X和Y坐标 如果显示溢出的话应该是无法生成圆 因为运算中有一步会变成除以0出错的给你一组测试数据
(1,7)
(2,6)
(5,8)
(7,7)
(9,5)
(3,7)
以上都是整数 当然你也可以输入小数
Private Sub Command1_Click()
Dim i As Integer
Dim X(0 To 5) As Double
Dim Y(0 To 5) As Double
For i = 0 To 5
X(i) = InputBox("输入第" &Str(i + 1) &"点的X值")
Y(i) = InputBox("输入第" &Str(i + 1) &"点的Y值")
Next
Dim x1, y1, x2, y2, x3, y3, x1y1, x1y2, x2y1 As Double
For i = 0 To 5
x1 = x1 + X(i)
y1 = y1 + Y(i)
x2 = x2 + X(i) * X(i)
y2 = y2 + Y(i) * Y(i)
x3 = x3 + X(i) * X(i) * X(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)
Next
Dim C, D, E, G, H, N As Double
N = 6
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
Dim thea, theb, thec As Double
thea = (H * D - E * G) / (C * G - D * D)
theb = (H * C - E * D) / (D * D - G * C)
thec = -(thea * x1 + theb * y1 + x2 + y2) / N
Dim resultA, resultB, resultR As Double
resultA = thea / (-2)
resultB = theb / (-2)
resultR = (thea * thea + theb * theb - 4 * thec) ^ 0.5 / 2
MsgBox ("最小二乘法拟合圆为:(" &Str(resultA) &"," &Str(resultB) &") 半径为:" &Str(resultR))
End Sub
第一步,提供一组【X,Y】已知点第二步,根据已知点拟合圆的一般式方程,利用公式求出圆心和半径。即 用圆的基本方程x^2+y^2+Dx+Ey+F=0,来拟合出其系数D、E、F,求出圆心(-D/2,-E/2),半径0.5√(D^2+-E^2-4F)
第三步,根据圆的参数方程,求出x,y的点,描点plot(x,y,'r-'),得到拟合圆的图形
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)