Sub VV() 'This program is used to calculate vector based topological change 'Author: Jing "Eric" Du 'April 14, 2014 Dim Num As Integer, Num_of_input As Integer, Num_of_output As Integer, p As Integer, q As Integer, i As Integer, j As Integer, k As Long Dim INPUTp As Variant Dim INPUTn As Variant Dim INPUTc As Variant Dim INPUTdiff As Variant Dim OUTPUTp As Variant Dim OUTPUTn As Variant Dim OUTPUTc As Variant Dim OUTPUTdiff As Variant Dim MATRIXi As Variant Dim MATRIXo As Variant Dim RESULTi As Single Dim RESULTi1 As Variant Dim RESULTi2 As Variant Dim RESULTi3 As Variant Dim RESULTo As Single Dim RESULTo1 As Variant Dim RESULTo2 As Variant Dim RESULTo3 As Variant Dim RESULTicum As Double Dim RESULTocum As Double Dim RESULTiavg As Double Dim RESULToavg As Double Dim RESULT As Double Dim FINALR As Double Dim MATRIXRange As Range ' Input parameters Num_of_input = InputBox("Please enter number of input variables", "Number of Input") Num_of_output = InputBox("Please enter number of output variables", "Number of Output") Num = InputBox("Please enter number of Data Points", "Number of Data points") ' Initialize what need to be initialized Centroid = 0 RESULT = 0 RESULTicum = 0 RESULTocum = 0 k = 0 ' Calculate covirance Set MATRIXRange = Range(Cells(2, 1), Cells(Num + 1, Num_of_input + Num_of_output)) For i = 1 To Num_of_input For j = 1 To Num_of_input Cells(i, 10 + j).Value = Application.WorksheetFunction.Covar(MATRIXRange.Columns(i), MATRIXRange.Columns(j)) Next j Next i Dim Inputmatrix As Range Set Inputmatrix = Range(Cells(1, 11), Cells(Num_of_input, 10 + Num_of_input)) MATRIXi = Application.WorksheetFunction.MInverse(Inputmatrix) For i = 1 To Num_of_output For j = 1 To Num_of_output Cells(Num_of_input + i, 10 + j).Value = Application.WorksheetFunction.Covar(MATRIXRange.Columns(i + Num_of_input), MATRIXRange.Columns(j + Num_of_input)) Next j Next i Dim Outputmatrix As Range Set Outputmatrix = Range(Cells(Num_of_input + 1, 11), Cells(Num_of_input + Num_of_output, 10 + Num_of_output)) MATRIXo = Application.WorksheetFunction.MInverse(Outputmatrix) ' ReDim the arrays ReDim INPUTp(1 To Num_of_input) As Double ReDim INPUTn(1 To Num_of_input) As Double ReDim INPUTc(1 To Num_of_input) As Double ReDim INPUTdiff(1 To Num_of_input) As Double ReDim OUTPUTp(1 To Num_of_output) As Double ReDim OUTPUTn(1 To Num_of_output) As Double ReDim OUTPUTc(1 To Num_of_output) As Double ReDim OUTPUTdiff(1 To Num_of_output) As Double ' Main loop For i = 2 To Num + 1 For j = i + 1 To Num ' Load input and output arrays For p = 1 To Num_of_input INPUTp(p) = Cells(i, p) INPUTn(p) = Cells(j, p) Next For q = 1 To Num_of_output OUTPUTp(q) = Cells(i, q + Num_of_input) OUTPUTn(q) = Cells(j, q + Num_of_input) Next ' Calculate RESULT RESULTi1 = Application.WorksheetFunction.MMult(Application.WorksheetFunction.MMult(INPUTp, MATRIXi), Application.WorksheetFunction.Transpose(INPUTn)) RESULTi2 = Application.WorksheetFunction.MMult(Application.WorksheetFunction.MMult(INPUTp, MATRIXi), Application.WorksheetFunction.Transpose(INPUTp)) RESULTi3 = Application.WorksheetFunction.MMult(Application.WorksheetFunction.MMult(INPUTn, MATRIXi), Application.WorksheetFunction.Transpose(INPUTn)) RESULTi = RESULTi1(1) / ((RESULTi2(1) * RESULTi3(1)) ^ (1 / 2)) RESULTicum = RESULTicum + Application.WorksheetFunction.Degrees(Application.WorksheetFunction.Acos(RESULTi)) RESULTo1 = Application.WorksheetFunction.MMult(Application.WorksheetFunction.MMult(OUTPUTp, MATRIXo), Application.WorksheetFunction.Transpose(OUTPUTn)) RESULTo2 = Application.WorksheetFunction.MMult(Application.WorksheetFunction.MMult(OUTPUTp, MATRIXo), Application.WorksheetFunction.Transpose(OUTPUTp)) RESULTo3 = Application.WorksheetFunction.MMult(Application.WorksheetFunction.MMult(OUTPUTn, MATRIXo), Application.WorksheetFunction.Transpose(OUTPUTn)) RESULTo = RESULTo1(1) / ((RESULTo2(1) * RESULTo3(1)) ^ (1 / 2)) RESULTocum = RESULTocum + Application.WorksheetFunction.Degrees(Application.WorksheetFunction.Acos(RESULTo)) RESULT = RESULT + (Application.WorksheetFunction.Degrees(Application.WorksheetFunction.Acos(RESULTo)) - Application.WorksheetFunction.Degrees(Application.WorksheetFunction.Acos(RESULTi))) ^ 2 ' k++ k = k + 1 Next j Next i ' Calculate final result FINALR = RESULT ^ (1 / 2) / k RESULTiavg = RESULTicum / k RESULToavg = RESULTocum / k Cells(1, "AA") = "Number of Data points" Cells(1, "AB").Value = Num Cells(2, "AA") = "Number of Simulations" Cells(2, "AB").Value = k Cells(3, "AA") = "Average distance before model" Cells(3, "AB").Value = RESULTiavg Cells(4, "AA") = "Average distance after model" Cells(4, "AB").Value = RESULToavg Cells(5, "AA") = "Tv" Cells(5, "AB").Value = FINALR MsgBox "Number of Data points: " & Num & vbNewLine & "Number of Simulations: " & k & vbNewLine & "Average distance before model: " & RESULTiavg & vbNewLine & "Average distance after model: " & RESULToavg & vbNewLine & "Tv: " & FINALR, , "CId Result by Eric Du" End Sub