如何用VB实现最小二乘法拟合圆的演示程序:如输入6个点,x(i),y(i),得出拟合后的原方程,不用画图,
来源:学生作业帮助网 编辑:作业帮 时间:2024/11/15 18:13:50
如何用VB实现最小二乘法拟合圆的演示程序:如输入6个点,x(i),y(i),得出拟合后的原方程,不用画图,
如何用VB实现最小二乘法拟合圆的演示程序:如输入6个点,x(i),y(i),得出拟合后的原方程,不用画图,
如何用VB实现最小二乘法拟合圆的演示程序:如输入6个点,x(i),y(i),得出拟合后的原方程,不用画图,
点击按钮分别按照提示输入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