Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
LeastSquareMethod Function for RealTime Data Logging
Public Class Form1
'================main=======================
Public dataAry7CH() As Double = {157.33, 429.5, 678.75, 962, 677.5, 428.25, 170}
Public dataAry2CH() As Double = {1578.222, 1576.923, 1575.8115, 1574.635, 1575.747, 1577.0365, 1578.1495}
Public dataAry3CH() As Double = {1784.326, 1784.27125, 1784.0455, 1783.832, 1784.016375, 1784.15575, 1784.378}
Public dataAry4CH() As Double = {1649.395, 1649.197375, 1649.642, 1649.305, 1649.399875, 1649.4845, 1648.976}
Public i, j, k As Integer
'===============================================================
'Least Square Method Function Parameters
'===============================================================
Private num(3) As Integer
Private sumX(3) As Double
Private sumY(3) As Double
Private sumX2(3) As Double
Private sumY2(3) As Double
Private sumXY(3) As Double
Private aveX As Double = 0
Private aveY As Double = 0
Private devX As Double = 0
Private devY As Double = 0
Private devXY As Double = 0
Private stdevX As Double = 0
Private stdevY As Double = 0
Private n, n_1 As Integer
Private result1(8) As Double ' 0:correl 1:slope 2:intercept 3:setN ,setN=1,2,3waku
Private result2(8) As Double ' 0:correl 1:slope 2:intercept 3:setN ,setN=1,2,3waku
Private result3(8) As Double ' 0:correl 1:slope 2:intercept 3:setN ,setN=1,2,3waku
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For i = 0 To 6
result1 = LSM(dataAry7CH(i), dataAry2CH(i), 0)
Debug.Print("2Ch-7CH:" + CStr(i) + "," + CStr(result1(0)) + "," + CStr(result1(1)) + "," + CStr(result1(2)) + "," + CStr(result1(3)) + "," + CStr(result1(4)) + "," + CStr(result1(5)) + "," + CStr(result1(6)) + "," + CStr(result1(7)))
result2 = LSM(dataAry7CH(i), dataAry3CH(i), 1)
Debug.Print("3Ch-7CH:" + CStr(i) + "," + CStr(result2(0)) + "," + CStr(result2(1)) + "," + CStr(result2(2)) + "," + CStr(result2(3)) + "," + CStr(result2(4)) + "," + CStr(result2(5)) + "," + CStr(result2(6)) + "," + CStr(result2(7)))
result3 = LSM(dataAry7CH(i), dataAry4CH(i), 2)
Debug.Print("4Ch-7CH:" + CStr(i) + "," + CStr(result3(0)) + "," + CStr(result3(1)) + "," + CStr(result3(2)) + "," + CStr(result3(3)) + "," + CStr(result3(4)) + "," + CStr(result3(5)) + "," + CStr(result3(6)) + "," + CStr(result3(7)))
Next i
End Sub
'===============================================================================
'=========================LSM Function==========================================
'================================================================================
Private Function LSM(ByVal x As Double, ByVal y As Double, ByVal setN As Integer) As Double()
Dim a(8) As Double 'correl,slope,intercept,n,aveX,aveY,stdevX,stcevY
Dim slope As Double
Dim correl As Double
Dim intercept As Double
num(setN) = num(setN) + 1
sumX(setN) += x
sumY(setN) += y
aveX = sumX(setN) / num(setN)
aveY = sumY(setN) / num(setN)
sumX2(setN) += (x - aveX) ^ 2
sumY2(setN) += (y - aveY) ^ 2
sumXY(setN) += (x - aveX) * (y - aveY)
devX = sumX2(setN) / num(setN)
devY = sumY2(setN) / num(setN)
devXY = sumXY(setN) / num(setN)
stdevX = Math.Sqrt(devX)
stdevY = Math.Sqrt(devY)
slope = devXY / devX
correl = devXY / (stdevX * stdevY)
intercept = aveY - slope * aveX
a(0) = correl
a(1) = slope
a(2) = intercept
a(3) = num(setN)
a(4) = aveX
a(5) = aveY
a(6) = stdevX
a(7) = stdevY
Return a
End Function
End Class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment