您现在的位置:首页 >> 其它区 >> 最小二乘法算法程序

最小二乘法算法程序

时间:2014/11/8 10:00:46 点击:

  核心提示:最小二乘法算法程序下载地址 下载地址1:本地下载 下载地址2:百度云盘下载程序界面程序源码Dim x(1000), y(1000) As Double Dim n As IntegerPrivate...

最小二乘法算法程序下载地址
下载地址1:本地下载
下载地址2:百度云盘下载


程序界面
最小二乘法程序

程序源码
Dim x(1000), y(1000) As Double
Dim n As Integer

Private Sub Command1_Click()
 Dim averx, avery As Double
 Dim sumx, sumy, sumxx, sumxy, sumyy As Double
 Dim lxx, lyy, lxy As Double
 Dim i As Integer
 Dim a, b, r As Double
 
 Call Command2_Click
 
 '对求和参数进行初始化
 sumx = 0
 sumy = 0
 sumxx = 0
 sumxy = 0
 sumyy = 0
 
 '求和
 For i = 1 To n
  sumx = sumx + x(i)
  sumy = sumy + y(i)
  sumxx = sumxx + x(i) ^ 2
  sumxy = sumxy + x(i) * y(i)
  sumyy = sumyy + y(i) ^ 2
 Next i
 
 '求均值
 averx = sumx / n
 avery = sumy / n
 
 '求LXX,LXY,LYY
 lxx = sumxx - sumx ^ 2 / n
 lxy = sumxy - sumx * sumy / n
 lyy = sumyy - sumy ^ 2 / n
 
 b = lxy / lxx
 a = avery - b * averx
 r = lxy / Sqr(lxx * lyy)
 
 '结果输出
 Label2(0).Caption = a
 
 If Abs(b) < 1 Then
  If b > 0 Then
    Label2(1).Caption = "0" & b
  ElseIf b < 0 Then
    Label2(1).Caption = "-0" & Abs(b)
  End If
 End If
 
 Text1.Text = n
 Label4.Caption = r

End Sub

 

'从外部读取数据的函数
Private Sub Command2_Click()
 Dim i As Integer

Open App.Path + "\xdata.txt" For Input As #7
  i = 1
 Do
  Input #7, x(i)
  If x(i) = 0.000001 Then
   n = i - 1
   i = 10000
  End If
  i = i + 1
  Loop Until i > 1000
 Close #7
 
 Open App.Path + "\ydata.txt" For Input As #7
  i = 1
 Do
  Input #7, y(i)
  If y(i) = 0.000001 Then
   n = i - 1
   i = 10000
  End If
   i = i + 1
  Loop Until i > 1000
 Close #7
 
End Sub

作者:Steven160422 来源:原创
  • CAppChem(www.cappchem.com) © 2018 版权所有 All Rights Reserved.
  • 本站有部分信息内容来自网络,如有侵权行为,请留言联系本站。
  • 鄂ICP备13005487号-1  公网安备鄂公网安备 42010602000540号
  • Powered by CAppChem V3.0sp1