8 楼
xiajiean [专家分:310] 发布于 2006-04-20 13:08:00
我是用消元法硬解4元1次方程的,不是用矩阵解的,计算过程很复杂,程序很大。不过我觉得已经很成功了。
数学方法的确很重要,解题过程会变得简单,程序也会简单。看来以后还是要多学一些数学对于编程也很有益处。
这个帖子我已经收下了
再次谢谢你
下面是vb具体程序,已经验证没有错误
Dim sngData(1, n) As Single 'n个 样本数据
Dim b(3) As Double '最小二乘法 3次方程拟合输出的4个常数项
'输入数据的预处理程序
For i = 0 To n
sngData(0, i) = xi
sngData(1, i) = yi
Next i
Function subAccQuotiety(n As Integer) 'n 样点数 ,
Dim dblCurveMid(1, 20) As Double '中间数据结果数组
Dim i As Integer
dblCurveMid(0, 0) = n
For i = 1 To n
dblCurveMid(0, 1) = dblCurveMid(0, 1) + sngData(0, i) '∑xi
dblCurveMid(0, 2) = dblCurveMid(0, 2) + sngData(0, i) ^ 2 '∑xi ^ 2
dblCurveMid(0, 3) = dblCurveMid(0, 3) + sngData(0, i) ^ 3 '∑xi ^ 3
dblCurveMid(0, 4) = dblCurveMid(0, 4) + sngData(0, i) ^ 4 '∑xi ^ 4
dblCurveMid(0, 5) = dblCurveMid(0, 5) + sngData(0, i) ^ 5 '∑xi ^ 5
dblCurveMid(0, 6) = dblCurveMid(0, 6) + sngData(0, i) ^ 6 '∑xi ^ 6
dblCurveMid(0, 7) = dblCurveMid(0, 7) + sngData(1, i) '∑yi
dblCurveMid(0, 8) = dblCurveMid(0, 8) + sngData(1, i) * sngData(0, i) '∑yixi
dblCurveMid(0, 9) = dblCurveMid(0, 9) + sngData(1, i) * sngData(0, i) ^ 2 '∑yixi ^ 2
dblCurveMid(0, 10) = dblCurveMid(0, 10) + sngData(1, i) * sngData(0, i) ^ 3 '∑yixi ^ 3
Next i
dblCurveMid(1, 0) = dblCurveMid(0, 0) * dblCurveMid(0, 4) - dblCurveMid(0, 1) * dblCurveMid(0, 3)
dblCurveMid(1, 1) = dblCurveMid(0, 1) * dblCurveMid(0, 4) - dblCurveMid(0, 2) * dblCurveMid(0, 3)
dblCurveMid(1, 2) = dblCurveMid(0, 2) * dblCurveMid(0, 4) - dblCurveMid(0, 3) * dblCurveMid(0, 3)
dblCurveMid(1, 3) = dblCurveMid(0, 7) * dblCurveMid(0, 4) - dblCurveMid(0, 8) * dblCurveMid(0, 3)
dblCurveMid(1, 4) = dblCurveMid(0, 0) * dblCurveMid(0, 5) - dblCurveMid(0, 2) * dblCurveMid(0, 3)
dblCurveMid(1, 5) = dblCurveMid(0, 1) * dblCurveMid(0, 5) - dblCurveMid(0, 3) * dblCurveMid(0, 3)
dblCurveMid(1, 6) = dblCurveMid(0, 2) * dblCurveMid(0, 5) - dblCurveMid(0, 4) * dblCurveMid(0, 3)
dblCurveMid(1, 7) = dblCurveMid(0, 7) * dblCurveMid(0, 5) - dblCurveMid(0, 9) * dblCurveMid(0, 3)
dblCurveMid(1, 8) = dblCurveMid(0, 0) * dblCurveMid(0, 6) - dblCurveMid(0, 3) * dblCurveMid(0, 3)
dblCurveMid(1, 9) = dblCurveMid(0, 1) * dblCurveMid(0, 6) - dblCurveMid(0, 4) * dblCurveMid(0, 3)
dblCurveMid(1, 10) = dblCurveMid(0, 2) * dblCurveMid(0, 6) - dblCurveMid(0, 5) * dblCurveMid(0, 3)
dblCurveMid(1, 11) = dblCurveMid(0, 7) * dblCurveMid(0, 6) - dblCurveMid(0, 10) * dblCurveMid(0, 3)
dblCurveMid(1, 12) = dblCurveMid(1, 0) * dblCurveMid(1, 6) - dblCurveMid(1, 4) * dblCurveMid(1, 2)
dblCurveMid(1, 13) = dblCurveMid(1, 1) * dblCurveMid(1, 6) - dblCurveMid(1, 5) * dblCurveMid(1, 2)
dblCurveMid(1, 14) = dblCurveMid(1, 3) * dblCurveMid(1, 6) - dblCurveMid(1, 7) * dblCurveMid(1, 2)
dblCurveMid(1, 15) = dblCurveMid(1, 0) * dblCurveMid(1, 10) - dblCurveMid(1, 8) * dblCurveMid(1, 2)
dblCurveMid(1, 16) = dblCurveMid(1, 1) * dblCurveMid(1, 10) - dblCurveMid(1, 9) * dblCurveMid(1, 2)
dblCurveMid(1, 17) = dblCurveMid(1, 3) * dblCurveMid(1, 10) - dblCurveMid(1, 11) * dblCurveMid(1, 2)
dblCurveMid(1, 18) = dblCurveMid(1, 12) * dblCurveMid(1, 16) - dblCurveMid(1, 15) * dblCurveMid(1, 13)
dblCurveMid(1, 19) = dblCurveMid(1, 14) * dblCurveMid(1, 16) - dblCurveMid(1, 17) * dblCurveMid(1, 13)
dblCurveMid(0, 20) = dblCurveMid(1, 19) / dblCurveMid(1, 18) '已经求得 B0
dblCurveMid(1, 18) = dblCurveMid(1, 13) * dblCurveMid(1, 15) - dblCurveMid(1, 16) * dblCurveMid(1, 12)
dblCurveMid(1, 19) = dblCurveMid(1, 14) * dblCurveMid(1, 15) - dblCurveMid(1, 17) * dblCurveMid(1, 12)
dblCurveMid(0, 19) = dblCurveMid(1, 19) / dblCurveMid(1, 18) '已经求得 B1
dblCurveMid(1, 0) = dblCurveMid(0, 1) * dblCurveMid(0, 1) - dblCurveMid(0, 0) * dblCurveMid(0, 2)
dblCurveMid(1, 1) = dblCurveMid(0, 1) * dblCurveMid(0, 2) - dblCurveMid(0, 0) * dblCurveMid(0, 3)
dblCurveMid(1, 2) = dblCurveMid(0, 1) * dblCurveMid(0, 3) - dblCurveMid(0, 0) * dblCurveMid(0, 4)
dblCurveMid(1, 3) = dblCurveMid(0, 7) * dblCurveMid(0, 1) - dblCurveMid(0, 8) * dblCurveMid(0, 0)
dblCurveMid(1, 4) = dblCurveMid(0, 2) * dblCurveMid(0, 1) - dblCurveMid(0, 0) * dblCurveMid(0, 3)
dblCurveMid(1, 5) = dblCurveMid(0, 2) * dblCurveMid(0, 2) - dblCurveMid(0, 0) * dblCurveMid(0, 4)
dblCurveMid(1, 6) = dblCurveMid(0, 2) * dblCurveMid(0, 3) - dblCurveMid(0, 0) * dblCurveMid(0, 5)
dblCurveMid(1, 7) = dblCurveMid(0, 7) * dblCurveMid(0, 2) - dblCurveMid(0, 9) * dblCurveMid(0, 0)
dblCurveMid(1, 8) = dblCurveMid(0, 3) * dblCurveMid(0, 1) - dblCurveMid(0, 0) * dblCurveMid(0, 4)
dblCurveMid(1, 9) = dblCurveMid(0, 3) * dblCurveMid(0, 2) - dblCurveMid(0, 0) * dblCurveMid(0, 5)
dblCurveMid(1, 10) = dblCurveMid(0, 3) * dblCurveMid(0, 3) - dblCurveMid(0, 0) * dblCurveMid(0, 6)
dblCurveMid(1, 11) = dblCurveMid(0, 7) * dblCurveMid(0, 3) - dblCurveMid(0, 10) * dblCurveMid(0, 0)
dblCurveMid(1, 12) = dblCurveMid(1, 1) * dblCurveMid(1, 4) - dblCurveMid(1, 0) * dblCurveMid(1, 5)
dblCurveMid(1, 13) = dblCurveMid(1, 2) * dblCurveMid(1, 4) - dblCurveMid(1, 0) * dblCurveMid(1, 6)
dblCurveMid(1, 14) = dblCurveMid(1, 3) * dblCurveMid(1, 4) - dblCurveMid(1, 0) * dblCurveMid(1, 7)
dblCurveMid(1, 15) = dblCurveMid(1, 1) * dblCurveMid(1, 8) - dblCurveMid(1, 0) * dblCurveMid(1, 9)
dblCurveMid(1, 16) = dblCurveMid(1, 2) * dblCurveMid(1, 8) - dblCurveMid(1, 0) * dblCurveMid(1, 10)
dblCurveMid(1, 17) = dblCurveMid(1, 3) * dblCurveMid(1, 8) - dblCurveMid(1, 0) * dblCurveMid(1, 11)
dblCurveMid(1, 18) = dblCurveMid(1, 12) * dblCurveMid(1, 16) - dblCurveMid(1, 15) * dblCurveMid(1, 13)
dblCurveMid(1, 19) = dblCurveMid(1, 14) * dblCurveMid(1, 16) - dblCurveMid(1, 17) * dblCurveMid(1, 13)
dblCurveMid(0, 18) = dblCurveMid(1, 19) / dblCurveMid(1, 18) '已经求得 B2
dblCurveMid(1, 18) = dblCurveMid(1, 13) * dblCurveMid(1, 15) - dblCurveMid(1, 12) * dblCurveMid(1, 16)
dblCurveMid(1, 19) = dblCurveMid(1, 14) * dblCurveMid(1, 15) - dblCurveMid(1, 17) * dblCurveMid(1, 12)
dblCurveMid(0, 17) = dblCurveMid(1, 19) / dblCurveMid(1, 18) '已经求得 B3
b(0) = dblCurveMid(0, 20)
b(1) = dblCurveMid(0, 19)
b(2) = dblCurveMid(0, 18)
b(3) = dblCurveMid(0, 17)
End Function